TreeviewCopyright © aleen42 all right reserved, powered by aleen42

389. 找不同

https://leetcode-cn.com/problems/find-the-difference/

Java

/*
 * @Author: Goog Tech
 * @Date: 2020-08-24 15:42:39
 * @LastEditTime: 2020-08-24 15:43:17
 * @Description: https://leetcode-cn.com/problems/find-the-difference/
 * @FilePath: \leetcode-googtech\#389. Find the Difference\Solution.java
 * @WebSite: https://algorithm.show/
 */

class Solution {
    // 解题思路: 首先将字符串转换为字符数组,然后求和做差,即可得到新插入的字符
    public char findTheDifference(String s, String t) {
        int sum = 0;
        for(char c : t.toCharArray()) sum += c;
        for(char c : s.toCharArray()) sum -= c;
        return (char)sum;
    }
}

Python

'''
Author: Goog Tech
Date: 2020-08-24 15:42:44
LastEditTime: 2020-08-24 15:43:35
Description: https://leetcode-cn.com/problems/find-the-difference/
FilePath: \leetcode-googtech\#389. Find the Difference\Solution.py
WebSite: https://algorithm.show/
'''

class Solution(object):
    # 解题思路: 遍历字符串,通过比较字符出现的次数即可得到新插入的字符
    def findTheDifference(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: str
        """
        for i in range(len(t)):
            if t.count(t[i]) != s.count(t[i]):
                return t[i]
Copyright © GoogTech 2021 all right reserved,powered by GitbookLast update time : 2021-09-15 01:55:05

results matching ""

    No results matching ""