LintCode【简单】55. 比较字符串 。代码及思路
2018-02-05 22:51
288 查看
lintcode的页面总是刷不出来,好不容易刷出来提交一直在pending,一刷新网页又出不来了,不知道有没有人和我情况一样,我很抓狂啊现在!!!
注意事项
在 A 中出现的 B 字符串里的字符不需要连续或者有序。
样例
给出 A =
给出 A =
返回
所以我的想法是遍历B组,双重循环,每次都遍历A组,如果找到有一样的了,就把A组的数改成1,这样下次再循环的时候就解决了上述问题。
需注意:A,B为空的时候。
代码:
class Solution {
public:
/*
* @param A: A string
* @param B: A string
* @return: if string A contains all of the characters in B return true else return false
*/
bool compareStrings(string &A, string &B) {
// write your code here
int i, j;
bool ismatch = false;
if(B.length() == 0) return true;
for(i = 0; i < B.length(); i++){
for(j = 0; j < A.length(); j++){
if(A[j] == B[i]){
A[j] = '1';
ismatch = true;
break;
}
else{
ismatch = false;
}
}
if(!ismatch) return false;
}
if(ismatch) return true;
}
};
题目要求:
比较两个字符串A和B,确定A中是否包含B中所有的字符。字符串A和B中的字符都是 大写字母注意事项
在 A 中出现的 B 字符串里的字符不需要连续或者有序。
样例
给出 A =
"ABCD"B =
"ACD",返回
true
给出 A =
"ABCD"B =
"AABC",
返回
false
思路:
根据他样例给出的,当B里有两个“A”,A里只有一个的时候,返回的是false,这就说明只认可了前一个A存在于A组里,而第二个A相当于其他字符。所以我的想法是遍历B组,双重循环,每次都遍历A组,如果找到有一样的了,就把A组的数改成1,这样下次再循环的时候就解决了上述问题。
需注意:A,B为空的时候。
代码:
class Solution {
public:
/*
* @param A: A string
* @param B: A string
* @return: if string A contains all of the characters in B return true else return false
*/
bool compareStrings(string &A, string &B) {
// write your code here
int i, j;
bool ismatch = false;
if(B.length() == 0) return true;
for(i = 0; i < B.length(); i++){
for(j = 0; j < A.length(); j++){
if(A[j] == B[i]){
A[j] = '1';
ismatch = true;
break;
}
else{
ismatch = false;
}
}
if(!ismatch) return false;
}
if(ismatch) return true;
}
};
相关文章推荐
- LintCode【简单】53. 翻转字符串 。代码及思路
- LintCode【简单】8. 旋转字符串 。代码及思路
- LintCode【简单】13. 字符串查找 。代码及思路
- LintCode【简单】80. 中位数。代码及思路
- LintCode【简单】167. 链表求和 。代码及思路
- LintCode 55. 比较字符串
- 问题1:java中没有实现这种“byte a = 0xB2 --> String b = “B2””转换的简单实现需要自己实现。 答:自己编写的转换函数,思路将byte的高低4位分开,分别转换为对应的字符然后合成返回的字符串。 java 代码 1.
- LintCode【简单】60. 搜索插入位置 。代码及思路
- LintCode【简单】35. 翻转链表 。代码及思路
- LintCode【简单】44. 最小子数组 。代码及思路
- LintCode【简单】46. 主元素 。代码及思路
- LintCode | 55. 比较字符串
- LintCode【简单】50. 数组剔除元素后的乘积 。代码及思路
- LintCode【简单】6. 合并排序数组 II,9. Fizz Buzz 问题。代码及思路 ——【vector用法】
- LintCode【简单】2. 尾部的零 。代码及思路
- LintCode【简单】14. 二分查找 。代码及思路
- LintCode【简单】96. 链表划分。代码及思路
- LintCode【简单】64. 合并排序数组。代码及思路
- LintCode【简单】82. 落单的数。代码及思路
- LintCode【简单】100. 删除排序数组中的重复数字。代码及思路