Problem
Xiao Ming is going to help companies buy fruit. Give a codeList, which is loaded with the fruit he bought. Give a shoppingCart, which is loaded with target fruit. We need to check if the order in the codeList matches the order in the shoppingCart. Note that only the sum of the items in all linked lists in the codeList add up to less than or equal to the sum of items in the shoppingcart may return 1. In addition, the item in codeList may be "anything", which can match with any fruit.
NoticeThe number of fruits in codeList and the number of fruits in shppingCart are both less than 2000.
ExampleGiven codeList = [["apple", "apple"],["orange", "banana", "orange"]],, shoppingCart = ["orange", "apple", "apple", "orange", "banana", "orange"], return 1.
Explanation:
Because the order in the codeList matches the fruit in the shoppingCart except for the first orange.
Given codeList = [["orange", "banana", "orange"],["apple", "apple"]], shoppingCart = ["orange", "apple", "apple", "orange", "banana", "orange"], return 0.
Explanation:
Because the order in the codeList doesn"t match the shoppingCart.
Given codeList = [["apple", "apple"],["orange", "anything", "orange"]], shoppingCart = ["orange", "apple", "apple", "orange", "mango", "orange"], return 1.
Explanation:
anything matches mango, so codeList can match the fruit of shoppingCart.Solution
public class Solution { /** * @param codeList: The codeList * @param shoppingCart: The shoppingCart * @return: The answer */ public int buyFruits(List> codeList, List
shoppingCart) { // Write your code here List mingList = new ArrayList<>(); for (List list: codeList) { for (String str: list) { mingList.add(str); } } if (mingList.size() > shoppingCart.size()) return 0; for (int i = 0; i <= shoppingCart.size()-mingList.size(); i++) { for (int j = 0; j < mingList.size(); j++) { String cur = mingList.get(j); if (cur.equals(shoppingCart.get(i+j)) || cur.equals("anything")) { if (j == mingList.size()-1) return 1; else continue; } else { break; } } } return 0; } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/69441.html
摘要:列表是編程中使用頻率極高的數(shù)據(jù)結(jié)構(gòu),由一系列按特定順序排列的元素組成,用表示,逗號分隔元素,類似中的數(shù)組。由于列表包含多個元素,所以通常命名為復(fù)數(shù)形式,如,等。使用切片裁剪獲取子列表使用列表名裁剪獲取對應(yīng)索引區(qū)間的子列。 前言: 好久不見,突然發(fā)覺好久沒寫博客了,最近迷上了 Python 無法自拔,了解了一下,Python 簡單易學(xué),尤其是接觸過 java 的人,入門 Python 更...
摘要:例如,和是非常著名的數(shù)據(jù)科學(xué)支持包。是用進(jìn)行科學(xué)計算的基礎(chǔ)包。意味著是維數(shù)組。下面的代碼得到二維數(shù)組的,返回的元組中的第一個元素是行數(shù),第二個元素是列數(shù)。 翻譯:瘋狂的技術(shù)宅原文:https://towardsdatascience.co... Python 數(shù)據(jù)類型 在 Python 中有許多數(shù)據(jù)類型。最常見的是float(浮點型),int(整型),str(字符串),bool(布爾...
摘要:元組是有序且不可更改或不可修改不可變的集合。不允許重復(fù)成員。列表是有序且可修改可變的不同數(shù)據(jù)類型的集合。避免上述問題的一種方法是使用。計數(shù)橙色年齡,,,,,,,打印年齡。語法反轉(zhuǎn)水果香蕉,橙色,芒果,檸檬水果。按字母順序排序,年齡。 ...
摘要:過濾掉等于的數(shù)組元素返回一個新數(shù)組,數(shù)組中的元素為原始數(shù)組元素調(diào)用函數(shù)處理后的值數(shù)組元素平方用于檢測數(shù)組中的元素是否滿足指定條件如果有一個元素滿足條件,則表達(dá)式返回剩余的元素不會再執(zhí)行檢測如果沒有滿足條件的元素,則返回。 數(shù)組常用方法 創(chuàng)建數(shù)組 var fruits = [Apple, Banana]; console.log(fruits.length); 通過索引訪問數(shù)組元素 v...
閱讀 1315·2023-04-26 01:28
閱讀 2065·2021-11-08 13:28
閱讀 2316·2021-10-12 10:17
閱讀 2280·2021-09-28 09:46
閱讀 4141·2021-09-09 09:33
閱讀 3719·2021-09-04 16:40
閱讀 1077·2019-08-29 15:21
閱讀 2689·2019-08-26 17:17