LeetCode-383. Ransom Note (Java)
来源:互联网 发布:程序员学英语有用吗? 编辑:程序博客网 时间:2024/05/16 01:42
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
------------------------------------------------------------------------------------------------------------------------------------------------------------
题意
有两个字符串,前一个字符串ransom可以由后一个字符串magazine拼凑而成,另外magazine中的每一个字母只能用一次。这样的话,只需要
判断magazine中包含的每个字母的数量与ransom中每个字母数量的关系。如果magazine中包含的某一个字母数量小于ransom中那个字母的数量,
则返回false,即ransom不能组成,反之则返回true;
解题思路
我能想到的思路就是用map统计每个字母的数量,以字母为key,数量为value。然后循环判断ransom的每个字母在magazine中的数量。返回结果。
代码
public class Solution { public boolean canConstruct(String ransomNote, String magazine) { char[] ransomNoteArray = ransomNote.toCharArray(); char[] magazineArray = magazine.toCharArray(); Map<Character,Integer> ransomNoteMap = new HashMap(); Map<Character,Integer> magazineMap = new HashMap(); boolean flag = true; if(ransomNote.equals("")){ return true; } for(int i = 0 ; i<ransomNoteArray.length;i++){ if(ransomNoteMap.containsKey(ransomNoteArray[i])){ ransomNoteMap.put(ransomNoteArray[i],(Integer)ransomNoteMap.get(ransomNoteArray[i])+1); }else{ ransomNoteMap.put(ransomNoteArray[i],1); } } for(int j =0;j<magazineArray.length;j++){ if(magazineMap.containsKey(magazineArray[j])){ magazineMap.put(magazineArray[j],(Integer)magazineMap.get(magazineArray[j])+1); }else{ magazineMap.put(magazineArray[j],1); } } for(Map.Entry<Character,Integer> entry : ransomNoteMap.entrySet()){ if(!magazineMap.containsKey(entry.getKey()) || magazineMap.get(entry.getKey()) < entry.getValue()){ flag = false; } } return flag; }}
缺点很明显,就是耗时比较长。另外一个方法,则耗时比较短,也更加简练。
public class Solution { public boolean canConstruct(String ransomNote, String magazine) { int[] count = new int['a' + 26]; char[] m = magazine.toCharArray(); char[] r = ransomNote.toCharArray(); for(char c : m) count[c] ++; for(char c : r){ if(--count[c] < 0) return false; } return true; }}
这个方法是先遍历magazine,以字母为新数组的索引,以出现的次数为数组的值。然后再遍历ransom,根据ransom中的值去新数组count中去找
并对count的值减一,然后判断count的值是否大于0。
思路是一样的,就是实现的方法差别,这种方法已经出现了三次了。
- leetcode-383. Ransom Note java
- LeetCode-383. Ransom Note (Java)
- [LeetCode] Ransom Note (java)
- leetcode-383. Ransom Note
- [leetcode] 383. Ransom Note
- LeetCode 383. Ransom Note
- leetcode 383. Ransom Note
- leetcode 383. Ransom Note
- 383.[LeetCode]Ransom Note
- leetcode 383. Ransom Note
- leetcode 383. Ransom Note
- Leetcode 383. Ransom Note
- 【leetcode】383. Ransom Note
- Leetcode 383. Ransom Note
- LeetCode 383. Ransom Note
- [LeetCode]383. Ransom Note
- LeetCode 383. Ransom Note
- [LeetCode]--383. Ransom Note
- Null value was assigned to a property of primitive type setter of
- debian7.1请使用 apt-cdrom,通过它就可以让 APT 能识别该盘片。apt-get upgdate 不能被用来加入新的盘片。
- v540 检测出现的问题
- python3 pip常用命令
- cs本科可做的事
- LeetCode-383. Ransom Note (Java)
- Android Studio极光推送配置
- MFC子线程中更新控件内容的两种办法
- Ubuntu系统下为Python安装Scipy模块
- vi配置
- 前序遍历、中序遍历恢复二叉树
- sourceInsight4 破解笔记
- HTTP协议与AJP协议
- GDAL中新手常犯的error LNK2019: 无法解析的外部符号的错误