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. 多线程高并发编程(8) -- Fork/Join源码分析

    一.概念 Fork/Join就是将一个大任务分解(fork)成许多个独立的小任务,然后多线程并行去处理这些小任务,每个小任务处理完得到结果再进行合并(join)得到最终的结果. 流程:任务继承Recu ...

  2. 【matlab 基础篇 01】快速开始第一个程序(详细图文+文末资源)

    快速入门matlab,系统地整理一遍,如何你和我一样是一个新手,那么此文很适合你: 文章目录 1 软件安装 2 打开软件 3 编写程序 3.1 基础步骤 3.2 添加PATH 3.3 命令行模式 4 ...

  3. Liunx常用操作(五)-如何查询文档中的冒号与引号

    liunx下面有如下一段包含json格式的文档 一.单查所有冒号: .txt | grep [:] 结果如下: 二.单查所有引号: 这里需要转义 .txt | grep [\"] 三.gre ...

  4. 设计模式之GOF23建造者模式

    组件很多,装配顺序不定 本质: 1,分离了对象子组件的单独构造(Builder负责)和装配(Director负责),从而可以构造出复杂的对象,这个模式适用于某个对象的构建过程复杂的情况下使用 2,实现 ...

  5. Linux --如何新增一块硬盘并自动挂载

    1. 虚拟机添加硬盘 2.  分区 fdisk /dev/sdb 3. 格式化 mkfs -t ext4 /dev/sdb1 将刚刚创建的盘格式化成 ext4格式 4. 挂载 先创建一个目录,/hom ...

  6. js面试题(转)

    https://segmentfault.com/a/1190000015288700 1 介绍JavaScript的基本数据类型 Number.String .Boolean .Null.Undef ...

  7. Docker之从零开始制作docker镜像

    以前学习docker是直接docker pull命令直接拉取Linux中已有镜像,并创建容器,添加应用程序,但是docker镜像一开始是怎么来的呢?下面将从零开始介绍整个docker镜像的制作过程(初 ...

  8. 全网最详细最好懂 PyTorch CNN案例分析 识别手写数字

    先来看一下这是什么任务.就是给你手写数组的图片,然后识别这是什么数字: dataset 首先先来看PyTorch的dataset类: 我已经在从零学习pytorch 第2课 Dataset类讲解了什么 ...

  9. Python PIL Image图片显示系列

    1. PIL Image图片显示 在使用PIL函数中的Image方法读取图片时,对于图片的shape,可能有不少宝宝存在疑惑.是什么疑惑了?就是image = Image.open(image_pat ...

  10. MyBatis In的使用

    http://blog.csdn.net/unei66/article/details/17792503 MyBatis In的使用 标签: mybatisin 2014-01-03 16:23 74 ...