【leetcode】【Easy】【389. Find the Difference】【string】【bit manipulation】
来源:互联网 发布:wps office for mac 编辑:程序博客网 时间:2024/05/16 05:38
problem link
code: code1和code3效率比较高。
code1:将string变为char数组比第二种直接操作string使用charAt方法要快很多
public class Solution { public char findTheDifference(String s, String t) { int sumS=0; int sumT=0; int i=0; char[] charS=s.toCharArray(); char[] charT=t.toCharArray(); for(;i<s.length();i++){ sumS+=(int)charS[i]; sumT+=(int)charT[i]; } sumT+=(int)charT[i]; return (char)(sumT-sumS); }}
code2:
public class Solution { public char findTheDifference(String s, String t) { int sumS=0; int sumT=0; int i=0; for(;i<s.length();i++){ sumS+=(int)s.charAt(i); sumT+=(int)t.charAt(i); } sumT+=t.charAt(i); return (char)(sumT-sumS); }}code3:效率较高的一种解法。位操作,异或是经常用到的(相同为0,相异为1,与0异或为本身)
public class Solution { public char findTheDifference(String s, String t) {int n = t.length();char c = t.charAt(n - 1);for (int i = 0; i < n - 1; ++i) {c ^= s.charAt(i);c ^= t.charAt(i);}return c;}}
0 0
- 【leetcode】【Easy】【389. Find the Difference】【string】【bit manipulation】
- Leetcode 389. Find the Difference (Easy) (cpp)
- 389.leetcode Find the Difference(easy)[字符串处理]
- 389. Find the Difference [easy] (Python)
- 389. Find the Difference 难度:easy
- 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
- 389.[LeetCode]Find the Difference
- Linux(Centos7)yum安装最新redis
- union和struct对齐大小问题
- Python学习笔记-1、Python3的urlib.request和bs4.BeautifulSoup使用
- 脚本语言Lua简单入门学习笔记
- centos下启动jmeter-server报错解决方法
- 【leetcode】【Easy】【389. Find the Difference】【string】【bit manipulation】
- iOS之Block报错:capturing self strongly in this block is likely to lead to a retain cycle
- cshi
- Unity UGUI之Canvas总结
- Android自定义控件,有关于matrix超详细使用教程及进阶
- VS2005(VC++)远程调试方法搜集
- Exception in thread "main" org.hibernate.TransactionException: Transaction not successfully started解
- codeforces 348 C. Subset Sums (暴力+技巧)
- Redux Thunk