1、题目

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.

please:

Input:
s = "a"
t = "aa"
Output:
a

2、代码实现

public class Solution {public static char findTheDifference(String s, String t) {if (s == null || t.length() == 0) return t.charAt(0);if (t == null || t.length() == 0) return s.charAt(0);if (s == null && t == null)return 0;int[] a = new int[30];char[] tChars = t.toCharArray();char[] sChars = s.toCharArray();int sLength = s.length();int tLength = t.length();if (sLength > tLength) {for (int i = 0; i < sChars.length; i++) {if (a[sChars[i] - 97] != 0)a[sChars[i] - 97] = ++(a[sChars[i] - 97]);elsea[sChars[i] - 97] = 2;}for (int i = 0; i < tChars.length; i++) {a[tChars[i] - 97] = --(a[tChars[i] - 97]); }} else {for (int i = 0; i < tChars.length; i++) {if (a[tChars[i] - 97] != 0)a[tChars[i] - 97] = ++(a[tChars[i] - 97]);elsea[tChars[i] - 97] = 2;}for (int i = 0; i < sChars.length; i++) {a[sChars[i] - 97] = --(a[sChars[i] - 97]); }}for (int i = 0; i < 30; i ++) {if (a[i] >= 2) {return (char) (i + 97);} }return 0;}
}

3、总结

看到2个字符串对比,我么可以先转化为字符数组,下表从A - 65 活着  a - 95  开始,也就是从下表0开始,然后要注意2个字符串里面可能包含同样的元素有几个的情况,相同就往上加,另外一个就减,但是他们最多相差1个字符,所以,我们可可以肯定,比我们一开始设置的大,也就是3,然后如果没有重复的数据,那么一样的就为1,肯定有一个为2.

LeetCode之Find the Difference相关推荐

  1. LeetCode 389. Find the Difference

    题目: Given two strings s and t which consist of only lowercase letters. String t is generated by rand ...

  2. Leetcode 389. Find the Difference 找不同 解题报告

    1 解题思想 有两个字符串S和T,T是S打乱后多加一个字符,要找出这个不同的字符来. 嗯,这个问题其实就是异或,除了T多的那个字符外,都可以在异或中消除,不多说,很简单 2 原题 Given two ...

  3. Leetcode题解(超赞!!!)

    我是技术搬运工,好东西当然要和大家分享啦.原文地址 算法思想 二分查找 二分查找思想简单,但是在实现时有一些需要注意的细节: 在计算 mid 时不能使用 mid = (l + h) / 2 这种方式, ...

  4. LeetCode——Find the Difference

    LeetCode--Find the Difference Question Given two strings s and t which consist of only lowercase let ...

  5. C#LeetCode刷题之#530-二叉搜索树的最小绝对差(Minimum Absolute Difference in BST)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4123 访问. 给定一个所有节点为非负值的二叉搜索树,求树中任意两 ...

  6. LeetCode Find the Difference

    题意:给出两个字符串 s,t,t是由s字符串打散组成,然后再添加个字符. 思路:因为字符串中可能有重复的字符,所以不能用set来做.用hashmap来做,遍历t字符串时,统计对应字符的个数,然后遍历字 ...

  7. C#LeetCode刷题之#389-找不同(Find the Difference)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4062 访问. 给定两个字符串 s 和 t,它们只包含小写字母. ...

  8. 你面试稳了!通关LeetCode刷题完整攻略,省时又高效

    关注上方"深度学习技术前沿",选择"星标公众号", 资源干货,第一时间送达! 作者:穷码农 来源:https://zhuanlan.zhihu.com/p/10 ...

  9. LeetCode Construct the Rectangle

    原题链接在这里:https://leetcode.com/problems/construct-the-rectangle/ 题目: For a web developer, it is very i ...

最新文章

  1. Oracle执行计划突变诊断之统计信息收集问题
  2. 最新版IDEA常用配置指南,打造你的最酷IDE
  3. Linux下为文件增加列的shell脚本
  4. 五、jvm垃圾回收3(几种垃圾收集器)
  5. 技术宝典 | NeCodeGen:基于 clang 的源到源转译工具
  6. 初学者学习Python,掌握这些实用小技巧能快速入门!
  7. Spring Data REST 远程代码执行漏洞(CVE-2017-8046)分析与复现
  8. c语言全排列库函数,几种全排列的算法(C语言实现)
  9. 获取手机本地的图片或者照相机照像的图片 为头像
  10. 相机裁剪旋转_感受大画幅相机随心所欲的景深控制
  11. IDEA快速升级模块版本号
  12. windows映射网络驱动器方法
  13. 计算机黑屏或死机怎么办,激光打标机电脑黑屏或死机时怎么办?
  14. html 属于mvvm框架,vue.js是mvvm框架吗
  15. YOLOv4中常见CV学术名词说明(三){CSP/WRC/SAT}
  16. 利用手机模拟器进行apk抓包分析
  17. 小米平板4刷recovery教程_小米平板2中文Recovery刷机教程
  18. 七夕将至,20行js代码给女友做个卡通P图微信机器人
  19. 前端面试题集锦——JavaScript
  20. 三维空间两直线/线段最短距离、线段计算算法

热门文章

  1. Dapr牵手.NET学习笔记:Actor小试
  2. 网络协议,没有想象中那么难
  3. Microsoft宣布将停止支持多个 .NET Framework版本
  4. 315曝光不良奸商 对企业不能罚酒三杯
  5. Xamarin.Forms: 无限滚动的ListView(懒加载方式)
  6. 数据结构与算法专题——第十二题 Trie树
  7. WordPress 已过时?创始人与新架构拥护者开战
  8. 跟我一起学.NetCore之熟悉的接口权限验证不能少(Jwt)
  9. 虚虚实实,亦假亦真的 ValueTuple,绝对能眩晕你
  10. api接口返回动态的json格式?我太难了,尝试一下 linq to json