博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LintCode] Buy Fruits
阅读量:5901 次
发布时间:2019-06-19

本文共 2389 字,大约阅读时间需要 7 分钟。

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.

Notice

The number of fruits in codeList and the number of fruits in shppingCart are both less than 2000.

Example

Given 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://yiesx.baihongyu.com/

你可能感兴趣的文章
linux文件描述符
查看>>
传值引用和调用引用的区别
查看>>
hyper-v 无线网连接
查看>>
Python3.7.1学习(六)RabbitMQ在Windows环境下的安装
查看>>
Windows下memcached的安装配置
查看>>
ubuntu: firefox+flashplay
查看>>
常见的海量数据处理方法
查看>>
web.xml 中CharacterEncodingFilter类的学习
查看>>
贪吃蛇逻辑代码
查看>>
实现c协程
查看>>
ASP.NET视频教程 手把手教你做企业论坛网站 视频教程
查看>>
[LeetCode] Meeting Rooms II
查看>>
从Swift学习iOS开发的路线指引
查看>>
Scribes:小型文本编辑器,支持远程编辑
查看>>
ssh 安装笔记
查看>>
游戏音效下载网站大全
查看>>
实验五
查看>>
3-继承
查看>>
海归千千万 为何再无钱学森
查看>>
vue2.0 仿手机新闻站(六)详情页制作
查看>>