JavaInterview JavaInterview
首页
指南
分类
标签
归档
  • CSDN (opens new window)
  • 文档集合 (opens new window)
  • 系统架构 (opens new window)
  • 微信号 (opens new window)
  • 公众号 (opens new window)

『Java面试+Java学习』
首页
指南
分类
标签
归档
  • CSDN (opens new window)
  • 文档集合 (opens new window)
  • 系统架构 (opens new window)
  • 微信号 (opens new window)
  • 公众号 (opens new window)
  • 指南
  • 简历

  • Java

  • 面试

  • 算法

  • algorithm
  • leetcode
JavaInterview.cn
2022-07-16
目录

找不同Java

文章发布较早,内容可能过时,阅读注意甄别。

# 题目

给定两个字符串 s 和 t ,它们只包含小写字母。

字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。

请找出在 t 中被添加的字母。

示例 1:

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

示例 2:

输入:s = "", t = "y"
输出:"y"

提示:

  • 0 <= s.length <= 1000
  • t.length == s.length + 1
  • s 和 t 只包含小写字母

# 思路

思路:因为两个字符串中的字符 除了一个添加的 其他字符都是出现两次的 可以利用异或运算 一直异或 最后只会留下一个只出现奇次数的字符

# 解法

class Solution {
//    思路:因为两个字符串中的字符 除了一个添加的 其他字符都是出现两次的 可以利用异或运算 一直异或 最后只会留下一个只出现奇次数的字符


    public char findTheDifference(String s, String t) {
        int res = 0;

        for (char ch : s.toCharArray()){
            res ^= ch;
        }
        for (char ch : t.toCharArray()){
            res ^= ch;
        }
        return (char) res;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 总结

  • 分析出几种情况,然后分别对各个情况实现
微信 支付宝
最近更新
01
1686. 石子游戏VI Java
08-18
02
1688. 比赛中的配对次数 Java
08-18
03
1687. 从仓库到码头运输箱子 Java
08-18
更多文章>
Theme by Vdoing | Copyright © 2019-2025 JavaInterview.cn
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式