LeetCode每日一题: 找不同(No.389)

栏目: 编程工具 · 发布时间: 6年前

给定两个字符串 s  t,它们只包含小写字母。
字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。
请找出在 t 中被添加的字母。
复制代码

示例:

输入:
s = "abcd"
t = "abcde"
输出:
e
解释:
'e' 是那个被添加的字母。
复制代码

思考:

这道题将st两个字符串转成字符数组,然后将每个char转成int求和,然后相减,将结果再转回char就是添加的字符。
复制代码

实现:

class Solution {
    public char findTheDifference(String s, String t) {
        char[] schars = s.toCharArray();
        char[] tchars = t.toCharArray();
        int sSum = 0;
        int tSum = 0;
        for (int count = 0; count < s.length(); count++) {
            sSum += Integer.valueOf(schars[count]);
        }
        for (int count = 0; count < t.length(); count++) {
            tSum += Integer.valueOf(tchars[count]);
        }
        return (char) (tSum - sSum);
    }
}复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web Applications (Hacking Exposed)

Web Applications (Hacking Exposed)

Joel Scambray、Mike Shema / McGraw-Hill Osborne Media / 2002-06-19 / USD 49.99

Get in-depth coverage of Web application platforms and their vulnerabilities, presented the same popular format as the international bestseller, Hacking Exposed. Covering hacking scenarios across diff......一起来看看 《Web Applications (Hacking Exposed)》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具