LeetCode之Ransom Note
发布日期:2021-06-29 14:08:09
浏览次数:2
分类:技术文章
本文共 1376 字,大约阅读时间需要 4 分钟。
1、题目
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.
Each letter in the magazine string can only be used once in your ransom note.
Note:
You may assume that both strings contain only lowercase letters.canConstruct("a", "b") -> falsecanConstruct("aa", "ab") -> falsecanConstruct("aa", "aab") -> true
2、代码实现
public class Solution { public boolean canConstruct(String ransomNote, String magazine) { if (magazine == null) return false; if (ransomNote == null) return false; if (ransomNote.length() == 0 && magazine.length() == 0) return true; Listlist = new ArrayList (); for (char c : magazine.toCharArray()) { list.add(Character.valueOf(c)); } if (ransomNote.length() == magazine.length()) { for (int i = 0; i < ransomNote.length(); i++) { if (!list.remove(Character.valueOf(ransomNote.charAt(i)))) { return false; } } return true; } else { for (int i = 0; i < ransomNote.length(); i++) { if (!list.remove(Character.valueOf(ransomNote.charAt(i)))) { return false; } } if (list.size() > 0) { return true; } } return false; }}
转载地址:https://chenyu.blog.csdn.net/article/details/68485900 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2024年04月04日 12时54分44秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
开源项目|RT-Thread 软件包应用作品:小闹钟
2019-04-29
在 RT-Thread Studio 上使用 RT-Thread Nano
2019-04-29
开源项目|软件包应用作品:通用物联网系统平台
2019-04-29
单片机里面的CPU使用率是什么鬼?
2019-04-29
推荐一个优质Linux技术公众号-作者都是一线Linux代码贡献者们哦
2019-04-29
RT-Thread 编程风格指南
2019-04-29
95后高校电子教师,软硬兼修有趣有料!
2019-04-29
使用 STM32 通用 Bootloader ,让 OTA 更加 Easy
2019-04-29
Cache 的基本概念与工作原理
2019-04-29
Android程序员必备!面试一路绿灯Offer拿到手软,Android面试题及解析
2019-04-29
Android程序员的春天!12个View绘制流程高频面试题,分享PDF高清版
2019-04-29
深入浅出Android开发!你会的还只有初级工程师的技术吗?一线互联网公司面经总结
2019-04-29
深度剖析原理!超全Android中高级面试复习大纲,含BATJM大厂
2019-04-29
温故而知新!Android开发者该学习哪些东西提高竞争力?成功入职阿里
2019-04-29
火爆知乎的Android面试题-Android-App的设计架构经验谈,大厂内部资料
2019-04-29
看完直接怼产品经理!Android多进程从头讲到尾,跳槽薪资翻倍
2019-04-29
快速从入门到精通!面试的时候突然遇到答不上的问题怎么办?已拿到offer
2019-04-29