每天一道LeetCode--389. Find the Difference

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产生的,只比s多一个字母。题目的目的就是要找出这个多出的字母。

解法

解法一:这种解法比较直接,建立以个map对应字符串s,key为字符,value就是该字符出现的次数。首先遍历字符串s,来建立这个map,然后再遍历字符串t。对t中出现的每个字符,都从map中减一,当value值小于0时,则说明该字符就是多出的字符。代码我就不列出来了,这种解法和第二种类似,大家看了第二种解法就明白了。

解法二:第一种解法的map有点太重了,而且涉及到Character和char,int和Integer之间的转换,我也不喜欢。所以,我就用了另一种很类似的方法——int数组来代替map。因为字符串只有小写字母,也就是只有26中可能,那么建立一个int[16]的数组即可,索引就是字符char-‘a‘,而数组值就是字符出现在字符串s中的次数。说白了,和解法二思路一致。代码如下:

public char findTheDifference(String s, String t) {
    int[] nums = new int[26];
    for (int i = 0; i < s.length(); i++) {
        char ch = s.charAt(i);
        ++nums[ch - ‘a‘];
    }
    char ret = ‘a‘;
    for (int i = 0; i < t.length(); i++) {
        char ch = t.charAt(i);
        --nums[ch - ‘a‘];
        if (nums[ch - ‘a‘] < 0) {
            ret = ch;
            break;
        }
    }
    return ret;
}

解法三:由于字符串t只比字符串s多了一个字符,那么直接用t中所有字符值的和减去字符串s中字符值的和即可。

public char findTheDifference(String s, String t) {
    int ret = 0;
    for (int i = 0; i < s.length(); i++) {
        ret -= (int)s.charAt(i);
    }
    for (int i = 0; i < t.length(); i++) {
        ret += (int)t.charAt(i);
    }
    return (char)ret;
}

解法四:另外一种思路,既然字符串t只比字符串s多了一个字符,也就是说大部分字符都是相同的。那么,我们可以使用异或的方式,来找出这个不同的值。

public char findTheDifference(String s, String t) {
    int ret = 0;
    for (int i = 0; i < s.length(); i++) {
        ret ^= s.charAt(i);
    }
    for (int i = 0; i < t.length(); i++) {
        ret ^= t.charAt(i);
    }
    return (char)ret;
}

以上内容均参考自:LeetCode】389 Find the Difference(java)

时间: 2024-08-02 02:50:25

每天一道LeetCode--389. Find the Difference的相关文章

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

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 = "ab

Leetcode 389 Find the Difference

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 = "ab

【一天一道LeetCode】#219. Contains Duplicate II

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j]and the differen

【一天一道LeetCode】#290. Word Pattern

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a pattern and a string str, find if str follows the same pattern. Here follow means a full match, such that there is a bijection between a letter in pa

【一天一道LeetCode】#342. Power of Four

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Given num = 16, return true. Given num = 5, return false. F

【一天一道LeetCode】#232. Implement Queue using Stacks

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Implement the following operations of a queue using stacks. push(x) – Push element x to the back of queue. pop() – Removes the element from in front of queue

【一天一道LeetCode】#22. Generate Parentheses

一天一道LeetCode (一)题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "(

【一天一道LeetCode】#85. Maximal Rectangle

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. (二)解题 题目大意:给定一个二值矩阵,计算矩阵里面包含1的所有子矩阵的最大面

【一天一道LeetCode】#82. Remove Duplicates from Sorted List II

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Given 1->2->3-&

【一天一道LeetCode】#89. Gray Code

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of