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; } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/69441.html
摘要:列表是編程中使用頻率極高的數據結構,由一系列按特定順序排列的元素組成,用表示,逗號分隔元素,類似中的數組。由于列表包含多個元素,所以通常命名為復數形式,如,等。使用切片裁剪獲取子列表使用列表名裁剪獲取對應索引區間的子列。 前言: 好久不見,突然發覺好久沒寫博客了,最近迷上了 Python 無法自拔,了解了一下,Python 簡單易學,尤其是接觸過 java 的人,入門 Python 更...
摘要:例如,和是非常著名的數據科學支持包。是用進行科學計算的基礎包。意味著是維數組。下面的代碼得到二維數組的,返回的元組中的第一個元素是行數,第二個元素是列數。 翻譯:瘋狂的技術宅原文:https://towardsdatascience.co... Python 數據類型 在 Python 中有許多數據類型。最常見的是float(浮點型),int(整型),str(字符串),bool(布爾...
摘要:元組是有序且不可更改或不可修改不可變的集合。不允許重復成員。列表是有序且可修改可變的不同數據類型的集合。避免上述問題的一種方法是使用。計數橙色年齡,,,,,,,打印年齡。語法反轉水果香蕉,橙色,芒果,檸檬水果。按字母順序排序,年齡。 ...
摘要:過濾掉等于的數組元素返回一個新數組,數組中的元素為原始數組元素調用函數處理后的值數組元素平方用于檢測數組中的元素是否滿足指定條件如果有一個元素滿足條件,則表達式返回剩余的元素不會再執行檢測如果沒有滿足條件的元素,則返回。 數組常用方法 創建數組 var fruits = [Apple, Banana]; console.log(fruits.length); 通過索引訪問數組元素 v...
閱讀 1325·2023-04-26 01:28
閱讀 2083·2021-11-08 13:28
閱讀 2329·2021-10-12 10:17
閱讀 2306·2021-09-28 09:46
閱讀 4153·2021-09-09 09:33
閱讀 3733·2021-09-04 16:40
閱讀 1117·2019-08-29 15:21
閱讀 2699·2019-08-26 17:17