Leetcode 389(Java)

    xiaoxiao2021-04-17  40

    Total Accepted: 62128 Total Submissions: 120935 Difficulty: Easy Contributor: LeetCode 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.

    因为只多加入了一个字母,所以两个字符串所有字母的ascII码值的差值转换为char类型即为对应的字母。 在这里要注意char类型与int类型的转换问题,AC码如下: public class Solution { public char findTheDifference(String s, String t) { int codeascii= (int)t.charAt(t.length()-1); for(int i=0;i<s.length();i++){ codeascii -= (int)s.charAt(i); codeascii += (int)t.charAt(i); } return (char)codeascii; } }

    转载请注明原文地址: https://ju.6miu.com/read-674314.html

    最新回复(0)