LeetCode关于找不同的解法
2018-02-26 22:27
267 查看
第一次去LeetCode上写代码,果然自己的算法能力需要多多练习了。
Given two strings s and t which consist of only lowercase letters.String t is generated by random shuffling string s and then add one more letter at a random position.Find the letter that was added in t.Example:Input:
s = "abcd"
t = "abcde"
Output:
e
Explanation:
'e' is the letter that was added.给定两个字符串s和t,它们只包含小写字母。字符串t由随机洗牌字符串s生成,然后在随机位置添加一个字母。找到在t中添加的字母。有个比较简单的思路就是将字符串都转化为char类型的数组,然后一一比对,发现不同的就是那个后来加入的字符串,在这之前一定要先对字符串数组进行整理,不然的话是不能进行比较的代码如下char [] s_arr=s.toCharArray();//将字符串转化为char类型数组
char[] t_arr=t.toCharArray();
Arrays.sort(s_arr);//对字符串进行整理
Arrays.sort(t_arr);
for(int i=0;i<s_arr.length;i++){
if(s_arr[i]!=t_arr[i]){
return t_arr[i];
}else continue;
}
return t_arr[t_arr.length-1];但是还有一个更好的解决方法就是使用XOR(即异或操作),将两个字符串中所有的字符进行“异或”操作,最后剩下的那个字符就是我们要的结果,这是因为在“异或”过程中,两个相同字符会变成0,所以“异或”到最后就等价于0和目标字符进行“异或”,结果还是目标字符,直接输出即可,代码如下:public char findTheDifference_B(String s, String t) {
char c = 0;
for (int i = 0; i < s.length(); ++i) {
c ^= s.charAt(i);
}
for (int i = 0; i < t.length(); ++i) {
c ^= t.charAt(i);
}
return c;
}原文:http://blog.csdn.net/zsy112371/article/details/52355075
Given two strings s and t which consist of only lowercase letters.String t is generated by random shuffling string s and then add one more letter at a random position.Find the letter that was added in t.Example:Input:
s = "abcd"
t = "abcde"
Output:
e
Explanation:
'e' is the letter that was added.给定两个字符串s和t,它们只包含小写字母。字符串t由随机洗牌字符串s生成,然后在随机位置添加一个字母。找到在t中添加的字母。有个比较简单的思路就是将字符串都转化为char类型的数组,然后一一比对,发现不同的就是那个后来加入的字符串,在这之前一定要先对字符串数组进行整理,不然的话是不能进行比较的代码如下char [] s_arr=s.toCharArray();//将字符串转化为char类型数组
char[] t_arr=t.toCharArray();
Arrays.sort(s_arr);//对字符串进行整理
Arrays.sort(t_arr);
for(int i=0;i<s_arr.length;i++){
if(s_arr[i]!=t_arr[i]){
return t_arr[i];
}else continue;
}
return t_arr[t_arr.length-1];但是还有一个更好的解决方法就是使用XOR(即异或操作),将两个字符串中所有的字符进行“异或”操作,最后剩下的那个字符就是我们要的结果,这是因为在“异或”过程中,两个相同字符会变成0,所以“异或”到最后就等价于0和目标字符进行“异或”,结果还是目标字符,直接输出即可,代码如下:public char findTheDifference_B(String s, String t) {
char c = 0;
for (int i = 0; i < s.length(); ++i) {
c ^= s.charAt(i);
}
for (int i = 0; i < t.length(); ++i) {
c ^= t.charAt(i);
}
return c;
}原文:http://blog.csdn.net/zsy112371/article/details/52355075
相关文章推荐
- leetcode 387. First Unique Character in a String 不同解法
- [Leetcode]找到出现不同次数的数字(通用解法)
- LeetCode - 389. Find the Difference - 三种不同解法 - ( C++ ) - 解题报告
- leetcode中TwoSum的不同解法
- 关于leetcode第三题的巧妙解法
- 关于java和c++中 i=i++结果不同的分析
- leetcode:Find Minimum in Rotated Sorted Array II 菜鸟解法
- 关于三个数之和为0的进阶解法V1
- 【LeetCode】132. Palindrome Partitioning II 基于动态规划DP、C++、Java的分析及解法
- [LeetCode系列] 从中序遍历和后序遍历序列构造二叉树(迭代解法)
- [LeetCode] Distinct Subsequences 不同的子序列
- 关于K-fold cross validation 下不同的K的选择的疑惑?
- Leetcode:Same Tree 菜鸟解法
- 关于substring、substr、slice的不同
- 关于POI解析Excel文件(03和07版本不同)的问题
- Leetcode:Linked List Cycle 菜鸟解法
- 关于postgresql和oracle对于换行处理的不同之处
- leetcode [Contains Duplicate II]//待整理多种解法
- leetcode[Image Smoother]//待整理多种解法
- leetcode 217. Contains Duplicat hash解法及相关hashmap与hashset问题