Find the Difference问题及解法
2017-04-11 08:54
399 查看
问题描述:
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.
示例:
问题分析:
题目要求出两个字符串之间唯一一个不同的字符。可以用位运算,也可以用hashtable。下面是我用hashtable的解法:
class Solution {
public:
char findTheDifference(string s, string t) {
vector<int> mapping(256,0);
char res = ' ';
for(int i = 0; i < s.length(); i++)
{
mapping[s[i]]++;
}
for(int i = 0; i < t.length(); i++)
{
if(mapping[t[i]]-- == 0)
{
res = t[i];
break;
}
}
return res;
}
};
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.
示例:
Input: s = "abcd" t = "abcde" Output: e Explanation: 'e' is the letter that was added.
问题分析:
题目要求出两个字符串之间唯一一个不同的字符。可以用位运算,也可以用hashtable。下面是我用hashtable的解法:
class Solution {
public:
char findTheDifference(string s, string t) {
vector<int> mapping(256,0);
char res = ' ';
for(int i = 0; i < s.length(); i++)
{
mapping[s[i]]++;
}
for(int i = 0; i < t.length(); i++)
{
if(mapping[t[i]]-- == 0)
{
res = t[i];
break;
}
}
return res;
}
};
相关文章推荐
- LeetCode - 389. Find the Difference - 三种不同解法 - ( C++ ) - 解题报告
- leetcode[Find the Difference]//待整理多种解法
- leetcode之389. Find the Difference(C++解法)
- 389. Find the Difference的C++解法
- leetcode题目: Find the Difference 的C语言解法
- Find the Duplicate Number问题及解法
- 在Linux上安装netcdf4 ,解决"Can't find or link to the hdf5 library"的问题
- Can not find a java.io.InputStream with the name [downloadFile] in the invocation stack问题解决
- 当启动文档转换负载平衡器服务时出现如下问题The system cannot find the file specified的解决方案
- .net中创建目录的问题(未找到路径X的一部分|Could not find a part of the path)
- linux内核编译中的各种问题:No rule to make target `menuconfig'&&Unable to find the Ncurses libraries
- 解决在安装VMware tools提示“Unable to find the database file /etc/vmware-tools/location”的问题
- hdu 3885 Find the Difference
- Find the difference between two days in days, months and years
- Android问题集锦之九:Unable to find a 'userdata.img' file for ABI armeabi to copy into the AVD folder.
- 解决qt版本不匹配问题:Can’t find the Qt version that’s associated with this project
- find the safest road - 最短路径问题(floyd算法)
- To find the differences between 2 Excel sheets in the same file
- Eclipse中执行Ant脚本出现Could not find the main class的问题及解
- Eclipse中执行Ant出现Could not find the main class的问题及解