您的位置:首页 > Web前端

LeetCode oj 389. Find the Difference(Hash)

2016-09-29 23:08 513 查看


389. Find the Difference

Question
Editorial Solution
My Submissions

Total Accepted: 22851
Total Submissions: 46129
Difficulty: Easy

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,t比s多了一个字符,问这个字符是什么。

可以用数组做Hash,也可以用HaspMap,但是java的Map数组我调了好久都不对,还是C++好用感觉= =+,最后还是用数组模拟的

public class Solution {
public char findTheDifference(String s, String t) {
int a[] = new int [26];
int len_s = s.length();
int len_t = t.length();
for(int i=0;i<len_s;i++){
a[s.charAt(i)-'a']++;
}
for(int i=0;i<len_t;i++){
a[t.charAt(i)-'a']--;
if(a[t.charAt(i)-'a'] == -1){
return t.charAt(i);
}
}
return ' ';
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  389. Find the Differ