Java [Leetcode 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
解题思路:
开26个数组存入magzine字母的数目,每个位置存对应字母的数目;
对ransom字符串来说,每读一个字母,则将对应位置的字母数目减一,如果某个字母数目小于0了,则表明字母不够用,从而返回false;否则返回true。
代码如下:
public class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
int[] ran_array = new int[26];
for(int i = 0; i < magazine.length(); i++){
ran_array[magazine.charAt(i) - 'a']++;
} for(int i = 0; i < ransomNote.length(); i++){
if(--ran_array[ransomNote.charAt(i) - 'a'] < 0)
return false;
} return true; }
}
Java [Leetcode 383]Ransom Note的更多相关文章
- leetcode 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all th ...
- 14. leetcode 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the magazines, ...
- LeetCode: 383 Ransom Note(easy)
题目: Given an arbitrary ransom note string and another string containing letters from all the magazin ...
- 383. Ransom Note【easy】
383. Ransom Note[easy] Given an arbitrary ransom note string and another string containing letters f ...
- 【LeetCode】383. Ransom Note 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- leetcode修炼之路——383. Ransom Note
题目是这样的 Given an arbitrary ransom note string and another string containing letters from a ...
- [LeetCode&Python] Problem 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the magazines, ...
- 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all th ...
- [LeetCode] 383. Ransom Note_Easy tag: Hash Table
Given an arbitrary ransom note string and another string containing letters from all the magazines, ...
随机推荐
- application/x-www-form-urlencoded和multipart/form-data
我们在提交表单的时候,form表单参数中会有一个enctype的参数. EncType表明提交数据的格式,用 Enctype 属性指定将数据发到服务器时浏览器使用的编码类型. enctype指定了H ...
- Spring 之自动化装配 bean 尝试
[Spring之自动化装配bean尝试] 1.添加dependencies如下所示(不是每一个都用得到 <dependencies> <dependency> <grou ...
- Routing in ASP.NET Web API
Why is HttpGet required only for some actions? https://stackoverflow.com/questions/28068868/why-is-h ...
- THINKPHP模版控制循环输出
<volist name="data" id="vo"> <div class="case1_01 flexslider" ...
- Ubuntu 16 安装redis客户端
https://snapcraft.io/redis-desktop-manager sudo snap install redis-desktop-manager 很好用! 支持模糊过滤,两边加星号 ...
- Java实现简单网页抓取
需求说明:使用Java抓取网页信息,并以字符串的形式返回. 使用Java代码实现: package net.ibuluo.spider.util; import java.io.IOException ...
- 运行jar_测试代码
1.Eclipse 将 工程 导出成 jar Eclipse --> Export... --> 界面中树状图形中选择" Java下的'JAR file' "(不知道这 ...
- Spark- 使用第三方依赖解析IP地址
使用 github上已有的开源项目1)git clone https://github.com/wzhe06/ipdatabase.git 2)编译下载的项目: mvn clean package- ...
- bower 安装
安装Yeomannpm install --global yo搭建一个web应用脚手架,你将需要安装generator-webapp生成器:npm install -g generator-webap ...
- 地图Legend控件的使用
http://support.esrichina-bj.cn/2011/0318/1150.html