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; } }
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/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...
阅读 1281·2023-04-26 01:28
阅读 2046·2021-11-08 13:28
阅读 2299·2021-10-12 10:17
阅读 2225·2021-09-28 09:46
阅读 4113·2021-09-09 09:33
阅读 3697·2021-09-04 16:40
阅读 975·2019-08-29 15:21
阅读 2661·2019-08-26 17:17