leetcode 389. Find the Difference 牛人用异或 或者 求和 解决,很简单。

原创 2016年08月30日 22:45:35

389. Find the Difference

 
 My Submissions
  • Total Accepted: 7465
  • Total Submissions: 14609
  • 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.










我的解法:利用hashmap进行记录
public class Solution {
public char findTheDifference(String s, String t){
    Map map=new HashMap<Character,Integer>();
    for(int i=0;i<s.length();i++){
        if(!map.containsKey(s.charAt(i)))
            map.put(s.charAt(i),1);
        else{
            int temp = (int)map.get(s.charAt(i));
            map.put(s.charAt(i),++temp);
        }
    }
    for(int a=0;a<t.length();a++){
        if(!map.containsKey(t.charAt(a))){
            return t.charAt(a);
        }
        else{
            int temp = (int)map.get(t.charAt(a));
            temp--;
            if(temp<0)return t.charAt(a);
            map.put(t.charAt(a),temp);
        }
    }
    return ' ';
    }
}

牛人解法:
java 用异或:
public char findTheDifference_B(String s, String t) {
	char c = 0;
	for (int i = 0; i < s.length(); ++i) {
		c ^= s.charAt(i);
	}
	for (int i = 0; i < t.length(); ++i) {
		c ^= t.charAt(i);
	}
	return c;
}

或者用求和,c:
char findTheDifference(char* s, char* t) {
    int sum1=0,sum2=0;
    for(;*s;s++)
        sum1+=*s;
    for(;*t;t++)
        sum2+=*t;
    return sum2-sum1;
}





版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

leetcode 389 Find the Difference

Problem: 给两个字符串s, t。t是由s打乱顺序后在随机一个位置加了一个字符构成,所有字符均为小写,求这个字符是什么?...

leetcode_389 Find the Difference

题目分析给定两个字符串s和t,都只包含小写字母,字符串t是由字符串s打乱顺序且在额外的随机位置上添加一个字符组成,请找出这个字符。 解题思路1)利用一个数组,先统计s字符串中每个字符出现的次数,然后统...

[leetcode] 389. Find the Difference 解题报告

题目链接: https://leetcode.com/problems/find-the-difference/ Given two strings s and t which consis...

LeetCode 389. Find the Difference

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

Leetcode_389_Find the Difference

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

LeetCode389. Find the Difference简单

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

LeetCode笔记:389. Find the Difference

在两个字符串中找出唯一多出的那个字母

LeetCode 389. Find the Difference

题意两个字符串s,t,都是小写字母,t由s中的字符和新增的一个字母随机排列组合而成,找出新增的字符!思路最直接的思路是HashMap,根据字符串s建立一个HashMap,key是字母,value是字母...

leetcode389. Find the Difference

389. Find the DifferenceGiven two strings s and t which consist of only lowercase letters.String t i...

leetcode算法——389. Find the Difference(基于JAVA)

Given two strings s and t which consist of only lowercase letters. String t is generated by random s...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode 389. Find the Difference 牛人用异或 或者 求和 解决,很简单。
举报原因:
原因补充:

(最多只允许输入30个字)