Java实现 LeetCode 389 找不同
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 找不同的更多相关文章
- Java实现 LeetCode 719 找出第 k 小的距离对(二分搜索法+二分猜数字)
719. 找出第 k 小的距离对 给定一个整数数组,返回所有数对之间的第 k 个最小距离.一对 (A, B) 的距离被定义为 A 和 B 之间的绝对差值. 示例 1: 输入: nums = [1,3, ...
- Java实现 LeetCode 513 找树左下角的值
513. 找树左下角的值 给定一个二叉树,在树的最后一行找到最左边的值. 示例 1: 输入: 2 / \ 1 3 输出: 1 示例 2: 输入: 1 / \ 2 3 / / \ 4 5 6 / 7 输 ...
- LeetCode 389——找不同
1. 题目 2. 解答 2.1. 方法一 将 s 和 t 转化为 Python 的列表,然后遍历列表 s 的元素,将它们从列表 t 中删除,最后列表 t 中会余下一个元素,即为所求. class So ...
- 【LeetCode】389.找不同
389.找不同 知识点:哈希表.抵消思想: 题目描述 给定两个字符串 s 和 t,它们只包含小写字母. 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母. 请找出在 t 中被添加的字母. ...
- 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 ...
- 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. ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- Linux中链接的概念
一,软链接 touch f1 创建符号链接,两个文件inode不同 ln -s f1 f3 二,硬链接 touch f1 创建硬链接, 两个文件inode相同 ln f1 f2 硬链接和软链接,最大 ...
- java ->IO流_commons类
commons-IO 导入classpath 加入classpath的第三方jar包内的class文件才能在项目中使用 1.创建lib文件夹 2.将commons-io.jar拷贝到lib文件夹 3. ...
- python mysql数据库基本操作方法
实现:使用Python实现用户登录,如果用户存在(数据库表中存在)则登录成功(假设该用户已在数据库中) import pymysql username = input('输入用户名:').strip( ...
- Rasa init报错:AttributeError: type object 'Callable' has no attribute '_abc_registry'
错误:Rasa init --no-prompt 报错 原因:Python升级到3.7后会遇到该问题 解决:pip uninstall typing
- IDEA插件记录
IDEA个性化设置 1. 开发工具 Free MyBatis plugin 作用:可以快速的在mybatis 的mapper 文件和xml文件中快速切换 Lombok 作用:为POJO类添加@Data ...
- C:习题2
C 语言中的数据类型主要有哪些? C 语言为什么要规定对所有用到的变量“先定义后使用”?这样做有什么好处? 1. 编译系统会根据定义为变量分配内存空间,分配空间的大小与数据类型有关 2. 系统可以根据 ...
- Vue零基础入门记录
在2020年这个开局不利的年份毕业,实习工作都很难得.最近来到一家单位,为了减小开支实习生过来了的话前端后端都要写.用Vue和ElementUI做界面.以前的前端vue了解还停留在new一个Vue实例 ...
- 通用css 常用
复选框自定义样式input[type="checkbox"] { position: relative; width: 0.75rem; height: 0.75rem; back ...
- JS轮播图带序号小点和左右按钮
轮播图作为前端比较简易的动画,使用非常频繁,这里记录以便使用 此轮播图为最简易自动播放,非无缝,但有按钮,有序号跳转小点 想看全套轮播图可以查看我的分类轮播图全套 html布局 <div sty ...
- multipart_formdata
import requests def sendImg(img_path, img_name, img_type='image/jpeg'): """ :param im ...