LeetCode之Ransom Note
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") -> false
canConstruct("aa", "ab") -> false
canConstruct("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;List<Character> list = new ArrayList<Character>();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;}
}
LeetCode之Ransom Note相关推荐
- Leetcode 383 Ransom Note
lc383 Ransom Note 两个for 第一个记录sourse字符串每种字母出现次数 第二个看现有字母是否能够填满target 1 class Solution { 2 public bool ...
- LeetCode 383. Ransom Note
题目 : Given an arbitrary ransom note string and another string containing letters from all the magazi ...
- leetcode 383. Ransom Note(赎金票据)
Given two strings ransomNote and magazine, return true if ransomNote can be constructed by using the ...
- C#LeetCode刷题之#383-赎金信(Ransom Note)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3937 访问. 给定一个赎金信 (ransom) 字符串和一个杂志 ...
- LeetCode:383. Ransom Note
051104 题目 Given an arbitrary ransom note string and another string containing letters from all the m ...
- [LeetCode] NO.383 Ransom Note
[题目] Given an arbitrary ransom note string and another string containing letters from all ...
- Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the ...
- LeetCode383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the ...
- LeetCode Ransom Note(字符串)
题意:给出两个字符串s1,s2 ,看s1是否可以由s2中的字符组成 具体代码如下: public class Solution {public boolean canConstruct(String ...
最新文章
- 第一章 基础设施,1.3 阿里视频云ApsaraVideo是怎样让4000万人同时狂欢的(作者:蔡华)...
- Windows10选择文件打开方式没有始终允许的解决方案
- django项目开发1:搭建虚拟环境
- django 学习 (三) 模板变量
- create-react-app第一步
- Spring Cloud(二):服务注册与发现 Eureka【Finchley 版】
- lenneth -- 基于koa2 的web极简框架
- PS2022新增功能简介
- 用单片机控制直流电机
- 用python的tkinter包设计一个随机点菜器
- 高德离线地图vue-amap的api文档(1):创建地图,撒点等等
- 确实有必要好好学英语
- GBase 8s 产品简介
- liunx下rdesktop无法使用,安装remmina
- Legolas工业自动化平台案例 —— 水源地自动化监控系统
- C++课后习题(一)
- JIra+Python+Pyechart 通过分析jira数据生成图表并展示,出具质量可视化的测试报告
- 阿里 菜鸟网络(一面)
- 前端招商银行一面面经
- C语言详解系列——综合练习,猜数字小游戏
热门文章
- 巧用ActionFilterAttribute实现API日志的记录
- 站在巨人的肩膀,2020我在使用和涉及到的开源项目
- 如何在 Asp.Net Core 中 管理敏感数据
- Docker:恢复对开源项目的无限制访问
- [Mvp.Blazor] 动态路由与钩子函数
- ASP.NET Core 反向代理部署知多少
- 【译】gRPC vs HTTP APIs
- Abp v0.18.0 新版本: MVC Module 启动模板
- 基于.NET Standard的分布式自增ID算法--美团点评LeafSegment
- asp.net core结合Gitlab-CI实现自动化部署