Find the Difference(leetcode389)
2019独角兽企业重金招聘Python工程师标准>>>
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.
Example:
Input: s = "abcd" t = "abcde"Output: eExplanation: 'e' is the letter that was added.
//既然都是小写字母 那么用之前的数组也是解决的 //这种方法与使用map是相同的 public static char findTheDifference(String s, String t) {int[] arr = new int[26];for (int i = 0; i < t.length(); i++) {arr[t.charAt(i) - 'a']++;}for (int i = 0; i < s.length(); i++) {--arr[s.charAt(i)-'a'];}for (int i =0;i< arr.length;i++){if(arr[i]>0){return (char)(i+'a');}}return ' '; }
//原来还可以这么写呀 public static char findTheDifference2(String s, String t) {char c = 0;for (int i = 0; i < s.length(); ++i) {c ^= s.charAt(i);}for (int i = 0; i < t.length(); ++i) {c ^= t.charAt(i);}return c; }
//写的看上去简略一点 少循环一次 public static char findTheDifference3(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; }
git:https://github.com/woshiyexinjie/leetcode-xin
转载于:https://my.oschina.net/u/2277632/blog/2999403
Find the Difference(leetcode389)相关推荐
- ADPRL - 近似动态规划和强化学习 - Note 10 - 蒙特卡洛法和时序差分学习及其实例 (Monte Carlo and Temporal Difference)
Note 10 蒙特卡洛法和时序差分学习 Monte Carlo and Temporal Difference 蒙特卡洛法和时序差分学习 Note 10 蒙特卡洛法和时序差分学习 Monte Car ...
- Linux: what's the difference between /proc/self and /proc/$$?
我在Stack Overflow上提了一个问题:linux - what's the difference between /proc/self and /proc/$$? - Stack Overf ...
- python 集合set 的三大方法intersection union difference来处理文氏图
TODO - 练习:A或B,但不能同时包含 编写一个函数,将两个集合(set_a和set_b)作为输入,并返回一个新的集合,其中包含set_a或set_b中的元素,但不包含两者兼有的元素. 在上面的文 ...
- Difference between prop and attr in different version of jquery
jQuery <1.9 $('#inputId').attr('readonly', true); jQuery 1.9+ $('#inputId').prop('readonly', true ...
- What’s the Difference between a Hue, Tint, Shade and Tone ?
What's the Difference between a Hue, Tint, Shade and Tone ? Ask Yourself first- What's the differenc ...
- G面经prepare: Set Intersection Set Difference
求两个sorted数组的intersection e.g. [1,2,3,4,5],[2,4,6] 结果是[2,4] difference 类似merge, 分小于等于大于三种情况,然后时间O(m+n ...
- LeetCode——Find the Difference
LeetCode--Find the Difference Question Given two strings s and t which consist of only lowercase let ...
- python 并集union, 交集intersection, 差集difference, 对称差集symmetric_difference
python的集合set和其他语言类似,是一个无序不重复元素集, 可用于消除重复元素. 支持union(联合), intersection(交), difference(差)和sysmmetric d ...
- 理解一条语句:SELECT difference(sum(value)) FROM mq_enqueue WHERE channel =~ /ActiveMQ_TEST/ AND $tim...
最近使用grafana在查询InfluxDB中,用到了这一条语句 SELECT difference(sum("value")) FROM "mq_enqueue&quo ...
最新文章
- MYSQL日期 字符串 时间戳互转
- golang 执行命令 设置超时
- python处理表格-python如何处理表格?
- oracle 怎么创建类型,ORACLE—002:Create之创建类型
- Mybatisの常见面试题
- php判断字段重复,删除完全重复和部分关键字段重复的记录
- 《『若水新闻』客户端开发教程》——15代码编写(7)
- react+spring 记录跨域问题的解决方法
- Bzoj 2154: Crash的数字表格(积性函数)
- 计算机网络信息安全保密制度,档案馆计算机网络系统和信息安全保密制度
- 蜗牛连珠五子棋改进版
- 2021年中国互联网安全行业发展状况及发展趋势展望分析[图]
- python利用tcp搭建小的聊天室带文件传输
- java抽象类存在的意义
- MoveIt!入门教程-简介
- 2014 acm亚洲区域赛(北京)总结
- 5-2 图书价格汇总
- svchost.exe占用过高CPU和内存的解决方案
- sap进阶系列(16):第一篇:财务总览之全面预算管理
- ffmpeg源码简析(十二)FFMPEG中的主要结构体总结
热门文章
- React之createRef
- d0...while循环结构讲解
- linux的trace文件路径,linux系统分析工具之Blktrace(十)
- html文字竖直书写,css 文字竖直居中的写法和图片垂直居中代码(图文)
- 虚拟机随服务器启动,VMware Server中虚拟机随宿主机自动启动
- java mysql ssl警告_连接到MySQL数据库时有关SSL连接的警告
- php 读取 stl 文件,科学网—[转载]【源码】读取ASCII STL文件的STLReader - 刘春静的博文...
- 剑指offer python实现_剑指Offer第2题详解(附Python、Java代码实现)
- java order()_java.util.Collections.reverseOrder()
- java中已定义类型car_Java 8 习惯用语(8):Java 知道您的类型