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. Java三大特征:封装 继承 多态

    内部类:成员内部类.静态内部类.方法内部类.匿名内部类. 内部类:定义在另外一个类里面的类,与之对应,包含内部类的外部类被称为外部类. 内部类的作用:(1)内部类提供了更好的封装,可以把内部类隐藏在外 ...

  2. [hdu3507 Print Article]斜率优化dp入门

    题意:需要打印n个正整数,1个数要么单独打印要么和前面一个数一起打印,1次打印1组数的代价为这组数的和的平方加上常数M.求最小代价. 思路:如果令dp[i]为打印前i个数的最小代价,那么有 dp[i] ...

  3. ":-"的用法举例

    例子1: #!/bin/bash A=aaa C=${A:-bbb} echo $C 输出结果:aaa 例子2: #!/bin/bash A="" C=${A:-bbb} echo ...

  4. Lottery

    0x01 修改金币到8位,才能买东西 robots.txt中发现.git泄露 下载附件,得到源码 审计api.php 我们传入的值与随机生成的值进行比较, 按照相同的个数,得到不同的钱 if($num ...

  5. Spring + Struts + Hibernate + Bootstrap-table 实现分页和增删改查

    1.bootstrap界面效果图: 2.Teacher实体类 public class Teacher { private int id; private String name; private S ...

  6. python字符串str

    字符串str常用操作方法(都会产生新的数据) 1.取值: (1)索引:s[0] (2)切片:s[起始索引:结束索引:步长] 起始索引为0,可以省略 s最后一个索引可以取-1 结束索引省略,默认取到最后 ...

  7. BZOJ1017 树形DP

    1017: [JSOI2008]魔兽地图DotR Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 2308  Solved: 919[Submit][S ...

  8. 带你学够浪:Go语言基础系列 - 8分钟学基础语法

    对于一般的语言使用者来说 ,20% 的语言特性就能够满足 80% 的使用需求,剩下在使用中掌握. 基于这一理论,Go 基础系列的文章不会刻意追求面面俱到,但该有知识点都会覆盖,目的是带你快跑赶上 Go ...

  9. Ubuntu 18.04上交叉编译华硕路由器RT-AC88U的梅林384.15版本

    研究了半天官方wiki上的文档,最后却找到这个一键脚本.手动试了一下,丝般顺滑,成功编译出RT-AC88U的梅林固件384.15版本.也可以编译其他路由器型号的固件. 不过这个脚本比较暴力,上来就把/ ...

  10. MySQL创建用户,并设置指定访问数据库

    一.创建用户并授权 1. 登录mysql mysql -u root -q输入密码2. 创建数据库(已有数据库就不需要建立) create database newDB;//以创建newDB为例3. ...