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:
e Explanation:
'e' is the letter that was added.

本题最直观的思路为排序后对s和t逐位进行比较。故得以下代码:

 class Solution
{
public:
char findTheDifference(string s, string t)
{
sort(s.begin(), s.end());
sort(t.begin(), t.end());
for(int i = ; i < s.size(); i++)
{
if(s[i] == t[i])
{
continue;
}
else
{
return t[i];
}
}
return t[t.size() - ];
}
};

由于排序复杂度过高,于是查询别的解法发现,可以建立一个字母表,s出现一次字母表对应位置+1,t中出现一次对应位置-1。那么只在t中出现的字母的位置为-1。代码如下:

 public char findTheDifference(String s, String t) {
if(s == null || s.length() == )
return t.charAt();
int[] letters = new int[];
for(int i = ; i < s.length(); i++){
int sPosition = s.charAt(i) - 'a';
int tPosition = t.charAt(i) - 'a';
letters[sPosition]++;
letters[tPosition]--;
}
int tPosition = t.charAt(t.length()-) - 'a';
letters[tPosition]--;
char res = 'a';
for(int i = ; i < letters.length; i++){
if(letters[i] == -){
res+= i;
break;
}
}
return res;
}

LeetCode 389. 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 random s ...

  2. 9. leetcode 389. Find the Difference

    Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...

  3. LeetCode 389 Find the Difference 解题报告

    题目要求 Given two strings s and t which consist of only lowercase letters. String t is generated by ran ...

  4. LeetCode - 389. Find the Difference - 三种不同解法 - ( C++ ) - 解题报告

    1.题目大意 Given two strings s and t which consist of only lowercase letters. String t is generated by r ...

  5. LeetCode: 389 Find the Difference(easy)

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

  6. LeetCode之389. Find the Difference

    -------------------------------------------------- 先计算每个字母的出现次数然后减去,最后剩下的那一个就是后来添加的了. AC代码: public c ...

  7. 【LeetCode】389. Find the Difference 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:字典统计次数 方法二:异或 方法三:排序 日 ...

  8. 【LeetCode】389 Find the Difference(java)

    原题 Given two strings s and t which consist of only lowercase letters. String t is generated by rando ...

  9. [LeetCode&Python] Problem 389. Find the Difference

    Given two strings s and t which consist of only lowercase letters. String t is generated by random s ...

随机推荐

  1. CSS Sprites优缺点

    利用CSS Sprites能很好地减少网页的http请求,从而大大的提高页面的性能,这也是CSS Sprites最大的优点,也是其被广泛传播和应用的主要原因: CSS Sprites能减少图片的字节, ...

  2. jvm--2.类加载机制

    3.JVM类加载机制 (1)类加载机制 虚拟机把描述类的数据从Class文件,用ClassLoader ,加载到内存,并对数据进行校验.转换解析和初始化,最终形成虚拟机直接使用的java类型, 这就是 ...

  3. 仅用aspx文件实现Ajax调用后台cs程序。(实例)

    仅用aspx文件实现Ajax调用后台cs无刷新程序.(实例) 两个文件:aaa.aspx 和aaa.aspx.cs 一.aaa.aspx <script type="text/java ...

  4. Spring Boot with Spring Data JPA (1) - Concept

    What's Spring Data JPA? According to Pivotal, Spring Data JPA, part of the larger Spring Data family ...

  5. 理解Angular中的$apply()以及$digest()

    $apply()和$digest()在AngularJS中是两个核心概念,但是有时候它们又让人困惑.而为了了解AngularJS的工作方式,首先需要了解$apply()和$digest()是如何工作的 ...

  6. 键盘按钮keyCode大全

    字母和数字键的键码值(keyCode) 按键 键码 按键 键码 按键 键码 按键 键码 A 65 J 74 S 83 1 49 B 66 K 75 T 84 2 50 C 67 L 76 U 85 3 ...

  7. 在Oracle中恢复被DROP掉的表

    在Oracle中可能不小心会DROP掉一个表,如果没有定期做备份的话,将会带来很大的麻烦.如果有的情况下,每天的数据都很重要,而定期备份的周期又稍长,情况恐怕也不容乐观!以前只知道Windows有个回 ...

  8. 让一个图片在div中居中(四种方法)

    第一种方法: <div class="title"> <div class="flag"></div> <div cl ...

  9. StringUtils中 isNotEmpty 和isNotBlank的区别

    isNotEmpty : 判断某字符串是否非空 StringUtils.isNotEmpty(null) = false StringUtils.isNotEmpty("") = ...

  10. PEtools

    // PETools.cpp : Defines the entry point for the console application.// #include "stdafx.h" ...