码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【每日一题Day49】LC1805字符串中不同整数的数目 | 双指针+哈希表


    字符串中不同整数的数目【LC1805】

    You are given a string word that consists of digits and lowercase English letters.

    You will replace every non-digit character with a space. For example, "a123bc34d8ef34" will become " 123 34 8 34". Notice that you are left with some integers that are separated by at least one space: "123", "34", "8", and "34".

    Return the number of different integers after performing the replacement operations on word.

    Two integers are considered different if their decimal representations without any leading zeros are different.

    终于有简单题了,类似的题还挺多,总结可以看这里

    • 思路:使用双指针定位字符串中整数的起始位置和结束位置,去除前导0后,将该整数放入哈希表中,最后返回哈希表的大小即可。

      • 如果左指针为字母,那么右移左指针,左指针为整数的起始位置
      • 如果右指针为数字,那么右移右指针,右指针为整数的结束位置+1
    • 实现

      class Solution {
          public int numDifferentIntegers(String word) {
              Set<String> set = new HashSet<>();
              int len = word.length();
              int l = 0;        
              int r = 0;
              while (l < len && r < len){
                  // 找到左边界
                  while (l < len && word.charAt(l) >= 'a' && word.charAt(l) <= 'z'){
                      l++;
                  }
                  if (l == len){
                      break;
                  }
                  // 找到右边界
                  r = l;
                  while ( r < len && word.charAt(r) >= '0' && word.charAt(r) <= '9') {
                      r++;
                  }
                  // 去除前导0
                  while (l != r && word.charAt(l) == '0'){
                      l++;
                  }
                  String num = word.substring(l, r);
                  set.add(num);
                  l = r + 1;
              }
              return set.size();
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 复杂度

        • 时间复杂度: O ( n ) O(n) O(n)
        • 空间复杂度: O ( n ) O(n) O(n)
  • 相关阅读:
    机器学习在无线信道建模中的应用现状与展望
    Linux crash调试(一)
    JVM虚拟机:通过日志学习PS+PO垃圾回收器
    【鸿蒙 HarmonyOS 4.0】通知
    计算机毕业设计 基于协同过滤算法的白酒销售系统的设计与实现 Javaweb项目 Java实战项目 前后端分离 文档报告 代码讲解 安装调试
    Semantic Kernel入门系列:利用Handlebars创建Prompts functions
    【探索微软 Edge:现代化的网页浏览体验】
    开源供应链管理系统 多供应商批发管理系统方案及源码输出
    【视频】机器学习交叉验证CV原理及R语言主成分PCA回归分析犯罪率|数据共享
    Collection集合 迭代器 增强for List集合 LinkedList集合详解
  • 原文地址:https://blog.csdn.net/Tikitian/article/details/128205295
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | 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号