小白笔记--------------leetcode(383. Ransom Note)
2016-11-10 21:17
429 查看
用c语言处理超时了。。。。是我没找到正确的方法。
bool canConstruct(char* ransomNote, char* magazine) {
int i,j,k,count = 0;
int f[strlen(magazine)];
for(k = 0; k < strlen(magazine);k++){
f[k] = 0;
}
for(i = 0;i < strlen(ransomNote);i++){
for(j = 0;j < strlen(magazine);j++){
if(ransomNote[i] == magazine[j] && f[j] == 0){
f[j] = 1;
count++;
break;
}
}
}
if(count == strlen(ransomNote)){
return true;
}else{
return false;
}
}
后来看到别人用java写的,一个简单的ArrayList的contains方法就解决了,一脸懵逼。
public class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
if(magazine == null) {
return false;
}
if(ransomNote == null) {
return true;
}
List<Character> list = new ArrayList<>();
for(Character ch: magazine.toCharArray()) {
list.add(ch);
}
for(Character ch : ransomNote.toCharArray()) {
if(list.contains(ch)) {
list.remove(ch);
} else {
return false;
}
}
return true;
}
}
bool canConstruct(char* ransomNote, char* magazine) {
int i,j,k,count = 0;
int f[strlen(magazine)];
for(k = 0; k < strlen(magazine);k++){
f[k] = 0;
}
for(i = 0;i < strlen(ransomNote);i++){
for(j = 0;j < strlen(magazine);j++){
if(ransomNote[i] == magazine[j] && f[j] == 0){
f[j] = 1;
count++;
break;
}
}
}
if(count == strlen(ransomNote)){
return true;
}else{
return false;
}
}
后来看到别人用java写的,一个简单的ArrayList的contains方法就解决了,一脸懵逼。
public class Solution {
public boolean canConstruct(String ransomNote, String magazine) {
if(magazine == null) {
return false;
}
if(ransomNote == null) {
return true;
}
List<Character> list = new ArrayList<>();
for(Character ch: magazine.toCharArray()) {
list.add(ch);
}
for(Character ch : ransomNote.toCharArray()) {
if(list.contains(ch)) {
list.remove(ch);
} else {
return false;
}
}
return true;
}
}
相关文章推荐
- LeetCode笔记:383. Ransom Note
- leetcode-383. Ransom Note
- <LeetCode OJ> 383. Ransom Note
- LeetCode 383. Ransom Note
- LeetCode 383. Ransom Note
- 小白笔记------------leetcode(204. Count Primeszhe)
- LeetCode之383. Ransom Note
- leetcode 383. Ransom Note 解题报告
- 【leetcode】383. Ransom Note
- LeetCode 383. Ransom Note 题解(C++)
- LeetCode—383. Ransom Note
- Leetcode - String - 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 (Easy) (cpp)
- [LeetCode]383. Ransom Note
- [LeetCode]--383. Ransom Note