LeetCode之Ransom Note
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                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.
?
 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的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Bit Manipulation ——
- 下一篇: LeetCode之Add Digits
