389. Find the Difference的C++解法
来源:互联网 发布:淘宝通知降权了怎么办 编辑:程序博客网 时间:2024/05/21 14:52
要考虑到统计重复字母的个数,比如s里有3个a,t里面有4个a,所以多出来的是a。我的解决办法就是找到一个删除一个,要使用迭代器。
class Solution {public:char findTheDifference(string s, string t) {int i;for (i = 0; i < t.length(); i++){int flag = 1;string::iterator it;for (it = s.begin(); it != s.end(); ++it){if (*it == t[i]){s.erase(it);flag = 0;break;}}if (flag==1) return t[i];}}};
看了下最优解发现又忽略了XOR的作用!!!这种只多出一个来的用XOR解决起来超级简单啊!!我是真的傻!!!
最优解算法:
class Solution {public: char findTheDifference(string s, string t) { char r=0; for(char c:s) r ^=c; for(char c:t) r ^=c; return r; }};
0 0
- 389. Find the Difference的C++解法
- leetcode之389. Find the Difference(C++解法)
- Find the Difference问题及解法
- 389. Find the Difference(C语言)
- 389. Find the Difference
- 389. Find the Difference
- 389. Find the Difference
- 389. Find the Difference
- 389. Find the Difference
- 389. Find the Difference
- 389. Find the Difference
- 389. Find the Difference
- 389. Find the Difference
- 389. Find the Difference*
- 389. Find the Difference
- 389. Find the Difference
- 389. Find the Difference
- 389. Find the Difference
- PHP 中 9 大缓存技术总结
- css清除浮动 clear float
- libevent源码深度剖析9(集成定时器事件)
- 设置EditText光标到输入末尾只需要几行代码
- 递推递归练习J数学黑洞
- 389. Find the Difference的C++解法
- c++函数重载
- Jquery选择器(二)
- MapReduce编程(三) 排序
- CentOS系统下如何设置mysql每天自动备份_Mysql
- javascript表格操作的快捷方法
- sqlserver不能直接create table as select ......
- iOS监控:卡顿检测
- 小米风控实践