383. Ransom Note
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
代码如下:
public class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
if(ransomNote.length()<=magazine.length())
{
int i=0;
while(i<ransomNote.length())
{
int index=magazine.indexOf(ransomNote.charAt(i));
if(index!=-1)
{
magazine= magazine.replaceFirst(magazine.substring(index,index+1),"");
i++;
}
else return false;
}
}
else return false; return true;
}
}
383. Ransom Note的更多相关文章
- 383. Ransom Note【easy】
383. Ransom Note[easy] Given an arbitrary ransom note string and another string containing letters f ...
- leetcode 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all th ...
- leetcode修炼之路——383. Ransom Note
题目是这样的 Given an arbitrary ransom note string and another string containing letters from a ...
- 14. leetcode 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the magazines, ...
- [LeetCode&Python] Problem 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the magazines, ...
- Java [Leetcode 383]Ransom Note
题目描述: Given an arbitrary ransom note string and another string containing letters from al ...
- 383. Ransom Note 在字典数组中查找笔记数组
[抄题]: Given an arbitrary ransom note string and another string containing letters from all the magaz ...
- LeetCode: 383 Ransom Note(easy)
题目: Given an arbitrary ransom note string and another string containing letters from all the magazin ...
- 【LeetCode】383. Ransom Note 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
随机推荐
- Js笔试题之千分位格式化
用js实现如下功能,将给定的数字转化成千分位的格式,如把“10000”转化成“10,000”,并考虑到性能方面的因素. 一.首先想到的办法,将数字转换为字符串(toString())再打散成数组(sp ...
- 学习KMP算法的一点小心得
KMP算法应用于 在一篇有n个字母的文档中 查找某个想要查找的长度为m的单词:暴力枚举:从文档的前m个字母和单词对比,然后是第2到m+1个,然后是第3到m+2个:这样算法复杂度最坏就达到了O(m*n) ...
- ScrollView中嵌入ListView,GridView冲突的解决(让ListView全显示出来)
ScrollView中嵌入原生ListView或GridView,会出现ListView,GridView显示不全的问题. 解决方法:重新构造一个ListView或GridView,重写OnMeasu ...
- Android google map 两点之间的距离
在Android google map中,有时候会碰到计算两地的距离,下面的辅助类就可以帮助你计算距离: public class DistanceHelper { /** Names for the ...
- android asyncTask 详解
只看http://www.cnblogs.com/xiaoluo501395377/p/3430542.html 足以
- redis 详解
什么是redis? redis 是一个基于内存的高性能key-value数据库. (有空再补充,有理解错误或不足欢迎指正) Reids的特点 Redis本质上是一个Key-Value类型的内存数据库, ...
- rpm软件包管理/yum软件管理
绝大数开源软件都是公布源代码的,源代码一般被打包为tar.gz归档压缩文件,然后手工编译为二进制可执行文件 ./configure 检查编译环境/相关库文件/配置参数,生成makefile make ...
- 《Head First设计模式(中文版)》
<Head First设计模式(中文版)>共有14章,每章都介绍了几个设计模式,完整地涵盖了四人组版本全部23个设计模式.前言先介绍<Head First设计模式(中文版)>的 ...
- hdu 1025 dp 最长上升子序列
//Accepted 4372 KB 140 ms //dp 最长上升子序列 nlogn #include <cstdio> #include <cstring> #inclu ...
- IOS图片缩放
1.自动缩放到指定大小 + (UIImage *)thumbnailWithImage:(UIImage *)image size:(CGSize)asize { UIImage *newimage; ...