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 题目分析:第二个字符串中,是否包含第一个字符串中出现的所有字符(重复的算出现的次数),第二个字符串中的每一个字符可以使用一次。 这个想起来挺简单:判断第一个字符串中每一个字符出现的次数与第二个字符串中每一个字符串出现的次数进行比较。 借鉴他人思路(只考虑小写的英文字母):
public static boolean canConstruct(String ransomNote, String magazine) { if (ransomNote.length() > magazine.length()) {
return false;
} int[] a = new int[26];// 最多有26个字母
int[] b = new int[26]; for (int i = 0; i < ransomNote.length(); i++) {
a[ransomNote.charAt(i) - 'a']++;// 进行字符个数的判断,如果已经存在了,就++(这里最重要)
} for (int i = 0; i < magazine.length(); i++) {
b[magazine.charAt(i) - 'a']++;
} for (int i = 0; i < a.length; i++) {
if (a[i] > b[i]) {// 判断第一个数组中的每一个是否有大于第二个数组的值
return false;
}
} return true;
}
代码很简单,关键是思路!思路!思路!
重要的事说三遍哈哈^_^
leetcode修炼之路——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 解题报告(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 all th ...
- 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 ...
- LeetCode: 383 Ransom Note(easy)
题目: Given an arbitrary ransom note string and another string containing letters from all the magazin ...
- 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all th ...
- 383. Ransom Note 在字典数组中查找笔记数组
[抄题]: Given an arbitrary ransom note string and another string containing letters from all the magaz ...
随机推荐
- 又是干货,这个春节感觉吃饱了。各方面---RHCS集群理论
什么是集群? 集群是一组(>2)相互独立的,通过高速网络互联的计算机组成的集合.群集一般可以分为科学集群,负载均衡集群,高可用性集群三大类. 科学集群是并行计算的基础.它对外就好象一 ...
- c#:for循环;穷举,迭代 练习
一)穷举 1. 第x种买法:羽毛球拍xx个,羽毛球xx个,水xx瓶 2. 单位给发了一张150元购物卡,拿着到超市买三类洗化用品. 洗发水15元,香皂2元,牙刷5元.求刚好花完150元,有多少 ...
- zz将 VSTO 插件部署给所有用户
原文:zz将 VSTO 插件部署给所有用户 注:本文原作者 Misha Shneerson 是 VSTO 团队的工程师.原文可以在下列地址找到:http://blogs.msdn.com/mshnee ...
- 给java中的System.getProperty添加新的key value对
由于系统被格了,所以,现在的java项目配置不对,代码里面的配置类调用了一个System.getProperty("env")发现找不到该变量的值,以前一直能找到的. 其实就是以前 ...
- AOP举例子
切面类TestAspect package com.spring.aop; /** * 切面 * */ public class TestAspect { public void doAfter(Jo ...
- 物理卷操作命令:pvcreate,pvscan,pvdisplay.卷组操作命令:vgcreate,vgdisplay.
新硬盘创建LVM系统过程. 物理卷操作命令:pvcreate,pvscan,pvdisplay. 卷组操作命令:vgcreate,vgdisplay. 逻辑卷操作命令:lvcreate,lvdispl ...
- HDU 4283 You Are the One
题意:给定n(n<=100)个人,每个人有个固定的屌丝值D. 起初这些人是站成一行,当第i个人第j个去面试的时候他的值是 Di*j. 要求所有人面试之后 这些值加起来是最小的. 队伍站成一行(其 ...
- 【转】Chrome保存mhtml网页文件的方法 – 无需任何插件,完美!
原文网址:http://www.ihacksoft.com/chrome-save-mht.html 在 Chrome 地址栏中键入“chrome://flags”,回车,这是一个 Chrome 的功 ...
- MySQL客户端执行外部sql文件命令
客户端 source d:\bbs.sql 或者 \. d:\bbs.sql
- bithrtree
#include "stdio.h" #include "stdlib.h" #define OK 1 #define ERROR 0 typedef char ...