leetcode-383. Ransom Note
2016-08-11 15:21
369 查看
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.
总结:这个题目也是比较简单,但是花了我不少时间,由于使用白板写代码,有一个地方的括号打错了。逻辑一直觉得没问题,多次想打开IDE调试哪里出bug了,最终还是找出来了地方。时间复杂度为O(m) + O(n);m和n分别为两个字符串的长度。
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
import java.util.HashMap; public class Solution { public boolean canConstruct(String ransomNote, String magazine) { boolean flag = true; HashMap<Character,Integer> m_t = new HashMap<Character,Integer>(); for(int i = 0 ; i < magazine.length(); i ++){ if( null != m_t.get(magazine.charAt(i))) m_t.put(magazine.charAt(i),m_t.get(magazine.charAt(i)) + 1); else m_t.put(magazine.charAt(i),1); } for(int i = 0 ; i < ransomNote.length(); i ++){ if( null == m_t.get(ransomNote.charAt(i)) || 0 == m_t.get(ransomNote.charAt(i))){ flag = false; break; }else m_t.put(ransomNote.charAt(i),m_t.get(ransomNote.charAt(i)) - 1 ); } return flag; } }
总结:这个题目也是比较简单,但是花了我不少时间,由于使用白板写代码,有一个地方的括号打错了。逻辑一直觉得没问题,多次想打开IDE调试哪里出bug了,最终还是找出来了地方。时间复杂度为O(m) + O(n);m和n分别为两个字符串的长度。
相关文章推荐
- [leetcode]: 383. Ransom Note
- LeetCode 383. Ransom Note
- leetcode 383. Ransom Note
- LeetCode-Easy部分中标签为String 383. Ransom Note
- LeetCode 383. Ransom Note
- LeetCode 383. Ransom Note
- 【leetcode】383. Ransom Note 恐吓信
- 383. Ransom Note [LeetCode]
- LeetCode | 383. Ransom Note
- Leetcode 383. Ransom Note
- leetcode(85).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
- LeetCode 383. Ransom Note