码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode 389. Find the Difference


    You are given two strings s and t.

    String t is generated by random shuffling string s and then add one more letter at a random position.

    Return the letter that was added to t.

    Example 1:

    Input: s = "abcd", t = "abcde"
    Output: "e"
    Explanation: 'e' is the letter that was added.
    

    Example 2:

    Input: s = "", t = "y"
    Output: "y"
    

    Constraints:

    • 0 <= s.length <= 1000
    • t.length == s.length + 1
    • s and t consist of lowercase English letters.

    就还是非常常规的alphabetical string的问题,一下就想到用数组计算count然后++ --看最后< 0的就行。稍微被坑了一小下,count数组的index是char,内容是count。以及要记一下java类型转换的语法是(type)var。

    1. class Solution {
    2. public char findTheDifference(String s, String t) {
    3. int[] count = new int[26];
    4. for (char c : s.toCharArray()) {
    5. count[c - 'a']++;
    6. }
    7. for (char c : t.toCharArray()) {
    8. count[c - 'a']--;
    9. }
    10. for (int i = 0; i < count.length; i++) {
    11. if (count[i] < 0) {
    12. return (char)('a' + i);
    13. }
    14. }
    15. return 0;
    16. }
    17. }

    然后看了解答,各路大神各显神通。

    可以直接用一个int变量charCode记录两个string的char index之和,s里的charCode--,t里的charCode++,最后相互抵消以后剩下的就是多的。空间复杂度就降到了O(1)。

    1. class Solution {
    2. public char findTheDifference(String s, String t) {
    3. int charCode = t.charAt(t.length() - 1);
    4. for (int i = 0; i < s.length(); i++) {
    5. charCode -= s.charAt(i);
    6. charCode += t.charAt(i);
    7. }
    8. return (char)charCode;
    9. }
    10. }

    比如也又来了XOR位操作。

    1. class Solution {
    2. public char findTheDifference(String s, String t) {
    3. int result = 0;
    4. for (char c : s.toCharArray()) {
    5. result ^= c;
    6. }
    7. for (char c : t.toCharArray()) {
    8. result ^= c;
    9. }
    10. return (char)result;
    11. }
    12. }

  • 相关阅读:
    PBlaze6 6530系列企业级SSD通过PCI-SIG兼容性测试
    OpenCV官方教程中文版 —— 图像梯度
    北邮22级信通院数电:Verilog-FPGA(1)实验一“跑通第一个例程” 过程中遇到的常见问题与解决方案汇总(持续更新中)
    RNN/LSTM (一) 实践案例
    BGP笔记3
    【实战】手把手教你从 0 到 1 搭建一套 RocketMQ 集群
    双向ip语音对讲音柱
    XGBoost的原理、公式推导
    【蓝桥杯国赛真题06】python绘制菱形圆环 蓝桥杯青少年组python编程 蓝桥杯国赛真题解析
    chatgpt赋能python:Python文件备份:保障数据安全,高效便捷的备份方案
  • 原文地址:https://blog.csdn.net/qq_37333947/article/details/127787835
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号