389. 找不同

给定两个字符串 s 和 t,它们只包含小写字母。

字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。

请找出在 t 中被添加的字母。

示例:

输入:

s = “abcd”

t = “abcde”

输出:

e

解释:

‘e’ 是那个被添加的字母。

class Solution {
// public char findTheDifference(String s, String t) {
// char res = t.charAt(t.length()-1);
// for(int i=0; i<s.length(); i++){
// res ^= s.charAt(i);
// res ^= t.charAt(i);
// }
// return res;
// }
public char findTheDifference(String s, String t) {
char[] ss = s.toCharArray();
char[] tt = t.toCharArray();
char res = tt[tt.length - 1];
for(int i=0; i<ss.length; i++){
res += tt[i] - ss[i];
}
return res;
}
}

Java实现 LeetCode 389 找不同的更多相关文章

  1. Java实现 LeetCode 719 找出第 k 小的距离对(二分搜索法+二分猜数字)

    719. 找出第 k 小的距离对 给定一个整数数组,返回所有数对之间的第 k 个最小距离.一对 (A, B) 的距离被定义为 A 和 B 之间的绝对差值. 示例 1: 输入: nums = [1,3, ...

  2. Java实现 LeetCode 513 找树左下角的值

    513. 找树左下角的值 给定一个二叉树,在树的最后一行找到最左边的值. 示例 1: 输入: 2 / \ 1 3 输出: 1 示例 2: 输入: 1 / \ 2 3 / / \ 4 5 6 / 7 输 ...

  3. LeetCode 389——找不同

    1. 题目 2. 解答 2.1. 方法一 将 s 和 t 转化为 Python 的列表,然后遍历列表 s 的元素,将它们从列表 t 中删除,最后列表 t 中会余下一个元素,即为所求. class So ...

  4. 【LeetCode】389.找不同

    389.找不同 知识点:哈希表.抵消思想: 题目描述 给定两个字符串 s 和 t,它们只包含小写字母. 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母. 请找出在 t 中被添加的字母. ...

  5. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  6. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  7. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  8. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  9. Java for LeetCode 210 Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...

随机推荐

  1. mysql批量导入删除

    批量导入 <insert id="extractOrderBaseHis">INSERT INTO `odr_order_base_his`(`order_base_i ...

  2. 2018-06-24 js BOM对象

    BOM对象: Browser Object Model 即浏览器对象模型: 包含: window:窗口对象 alert();//警示框 confirm();//确认框 prompt();//输入提示框 ...

  3. 2018-06-19 js DOM对象

    DOM对象: Doucument Object Model即文档对象 DOM对象的操作: 1.找元素 返回元素对象: var obj=document.getElementById();//通过Id查 ...

  4. apache httpd 不记录head 的请求。

    比如说用了阿里云的slb ,然后他会检测服务器是否正常运行,通常会发大量的HEAD 请求过来. 这样有规则的请求是可以屏蔽的.步骤如下: 在 <Directory />中添加 SetEnv ...

  5. 【python接口自动化】- openpyxl读取excel数据

    前言:目前我们进行测试时用于存储测试数据的软件几乎都是excel,excel方便存储和管理数据,读取数据时也比较清晰,测试时我们需要从excel从读取测试数据,结束后还需把测试结果写入到excel中, ...

  6. Vue Element-UI使用第三方icon图标(转)

    转载自:https://www.jianshu.com/p/59dd28f0b9c9 1.打开阿里icon,注册 >登录>图标管理>我的项目   2.新建项目 3. 添加icon到项 ...

  7. vue中使用mixins

    Mixins (混合或混入)——定义的是一个对象 1.概念:一种分发Vue组件可复用功能的非常灵活的方式.混入对象可以包含任意组件选项(组件选项:data.watch.computed.methods ...

  8. Django路由之url分组(命名)匹配

    分组(命名)匹配 urls.py路由配置文件中: urlspatterns中想捕获正则表达式匹配的结果用来出传递给views.py视图函数文件使用,需要用到分组匹配,或者使用第三个参数python字典 ...

  9. vue + typescript,定义全局变量或者方法

    众所周知,在 vue中,如果想定义一个全局变量的方法很简单,直接在 vue的原型上挂载属性或者方法即可. 但是,加上了typescript之后, Vue.prototype.$xxx = xxx  这 ...

  10. MYSQL的DOUBLE WRITE双写

    期待未来超高速大容量的固态硬盘普及时,只需要CHECKPOINT,而不再需要各种各样的BUFFER,CACHE了 DOUBLE WRITE 在InnoDB将BP中的Dirty Page刷(flush) ...