leetcode 389 Find the Difference
来源:互联网 发布:2017网络规划设计师 编辑:程序博客网 时间:2024/05/18 15:53
Problem:
给两个字符串s, t。t是由s打乱顺序后在随机一个位置加了一个字符构成,所有字符均为小写,求这个字符是什么?
Solution:
1. 将t字符串和s字符串异或,最后留下的就是那个不同的字符。
2. 将两个字符串排序后,输出相同位置不同字符的那个字符即可。
notes:
1. foreach的用法:
for(type name : array)
//Solution1class Solution {public: char findTheDifference(string s, string t) { char x = 0; for(char c : s+t) x ^= c; return x; }};//Solution2class Solution {public: char findTheDifference(string s, string t) { sort(s.begin(), s.end()); sort(t.begin(), t.end()); for(int i = 0; i < s.size(); i++) { if(s[i] != t[i]) return t[i]; } return t[t.size()-1]; }};
0 0
- LeetCode 389 Find the Difference
- Leetcode 389 Find the Difference
- Leetcode 389 Find the Difference
- LeetCode[389]Find the Difference
- LeetCode 389:Find the Difference
- Leetcode 389 Find the Difference
- [LeetCode-389]Find the Difference
- Leetcode 389 Find the Difference
- leetcode-389-find the difference
- leetcode 389 Find the Difference
- Leetcode #389 Find the Difference
- Leetcode 389 Find the Difference
- LeetCode.389 Find the Difference
- Leetcode:Find the Difference
- Find the Difference --LeetCode
- LeetCode Find the Difference
- [LeetCode] Find the Difference
- [Leetcode] Find the Difference
- Android 控件学习(极慢更新)
- iOS中谓词的使用(一)
- topcoder SRM 517 div1 AdjacentSwaps 分析与题解
- javascript基础 AJAX简单demo 2017年1月29日
- tcpio5种io模型
- leetcode 389 Find the Difference
- Atcoder#53
- 如何激活webstorm
- BZOJ 4515 [Sdoi2016]游戏
- 链表中环的入口结点(单链表)
- 略论错误提示的人性化
- mysql update select用法实例
- NYOJ 746 整数划分(四)详解 (区间DP)
- 简单工厂模式(Simple Factory Pattern)