389. Find the Difference (hash&异或)
来源:互联网 发布:五金行业erp软件报价 编辑:程序博客网 时间:2024/06/06 14:47
https://leetcode.com/problems/find-the-difference/discuss/
题目:求新增加的字母
解法一:直接统计
class Solution {public: char findTheDifference(string s, string t) { int diff[26]={0}; for(int x=0;x<s.length();x++) diff[s[x]-'a']++,diff[t[x]-'a']--; diff[t[t.length()-1]-'a']--; for(int x=0;x<26;x++) if(diff[x]) return 'a'+x; }};
解法二: 异或
class Solution {public: char findTheDifference(string s, string t) { int diff=0; for(int x=0;x<s.length();x++) diff^=(s[x]-'a'),diff^=(t[x]-'a'); diff^=(t[t.length()-1]-'a'); return 'a'+diff; }};或class Solution {public: char findTheDifference(string s, string t) { char diff=0; for(int x=0;x<s.length();x++) diff^=s[x],diff^=t[x]; diff^=t[t.length()-1]; return diff; }};
阅读全文
0 0
- 389. Find the Difference (hash&异或)
- LeetCode 389. Find the Difference(异或)
- LeetCode oj 389. Find the Difference(Hash)
- 389. Find the Difference-hash map/XOR
- LeetCode 389 Find the Difference (异或)
- Leetcode#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
- 等不及iPhone 8 ,你可以先看看这一场发布会
- 按键处理及外部中断
- 网信办发布新规,以后建群也要实名制了!
- Facebook打算重新摸进中国市场,现在正悄悄地寻找办公场地
- 为了让手表更好用,苹果找人先“戴”了7年半
- 389. Find the Difference (hash&异或)
- JS复习 -- 函数柯里化
- jedate时间插件
- Ubuntu 16.04下deb包的安装及常用命令
- LINUX_C编程实战-第十章《进程间通信》-管道
- matlab添加当前文件夹及子文件夹到路径
- 栈和队列 构造数组的MaxTree
- Windows10下 opencv2.4.9+dlib19.7+Visual Studio 2017 配置教程
- 博客首记