当前位置:网站首页>【每日3题(3)】符串中不同整数的数目
【每日3题(3)】符串中不同整数的数目
2022-06-28 16:38:00 【程序猿不脱发2】
题目:
给你一个字符串 word ,该字符串由数字和小写英文字母组成。
请你用空格替换每个不是数字的字符。例如,“a123bc34d8ef34” 将会变成 " 123 34 8 34" 。注意,剩下的这些整数为(相邻彼此至少有一个空格隔开):“123”、“34”、“8” 和 “34” 。
返回对 word 完成替换后形成的 不同 整数的数目。
只有当两个整数的 不含前导零 的十进制表示不同, 才认为这两个整数也不同。
示例 1:
输入:word = “a123bc34d8ef34”
输出:3
解释:不同的整数有 “123”、“34” 和 “8” 。注意,“34” 只计数一次。
示例 2:
输入:word = “leet1234code234”
输出:2
示例 3:
输入:word = “a1b01c001”
输出:1
解释:“1”、“01” 和 “001” 视为同一个整数的十进制表示,因为在比较十进制值时会忽略前导零的存在。
提示:
1 <= word.length <= 1000
word 由数字和小写英文字母组成
思路:
用正则把word按字母进行split, 得到的数组是空或者数字,再对数字去掉前导0
java代码:
class Solution {
public int numDifferentIntegers(String word) {
String[] words = word.split("[a-z]+");
Set<String> set = new HashSet<>();
for(int i=0;i<words.length;i++)
{
if(words[i].length()==0) {
continue;
}
int j =0; //对前导零的处理,因为考虑‘00000’的情况,所以到最后一位的前一位即可
while (words[i].charAt(j)=='0'&&j<words[i].length()-1){
j++;
}
set.add(words[i].substring(j));
}
return set.size();
}
}
边栏推荐
- After the first failure, AMEC rushed to the Hong Kong stock exchange for the second time, and the financial principal changed frequently
- 如何在网站上安装 WordPress
- Practice of curve replacing CEPH in Netease cloud music
- 【TcaplusDB知识库】查看业务密码
- Cardinality sorting - common sorting method (2/8)
- 7-用户输入和while循环
- 如何清除 WordPress 中的缓存
- visio 使用
- 软件快速交付真的需要以安全为代价吗?
- 【Hot100】1. Sum of two numbers
猜你喜欢

【TcaplusDB知识库】TcaplusDB技术支持介绍

NOIP普及组2006-2018初赛 2019 CSP-J1 2020 CSP-J1 完善程序题

The first place on the list - brake by wire "new cycle", the market competitiveness of local suppliers is TOP10
![[tcapulusdb knowledge base] Introduction to tcapulusdb restrictions](/img/d3/27f09f7f5ab8e27d1ab87a35a9c0f3.png)
[tcapulusdb knowledge base] Introduction to tcapulusdb restrictions

Curve 替换 Ceph 在网易云音乐的实践

批量修改指定字符文件名 bat脚本

这个简单的小功能,半年为我们产研团队省下213个小时

天翼云Web应用防火墙(边缘云版)通过首批可信认证

10.Hystrix断路器

Design details of the full stack CRM development tool webclient UI workbench
随机推荐
The future of platform as code is kubernetes extension
高并发、高可用、弹性扩展,天翼云护航企业云上业务
Cloud sports, 360 ° witnessing speed and passion
10.Hystrix断路器
Azure Kinect Microsoft camera unity development summary
免费、强大、高颜值的笔记软件评测: OneNote、Heptabase、氢图、FlowUs
这个简单的小功能,半年为我们产研团队省下213个小时
FS2K人脸素描属性识别
[redis] a brief summary of redis on January 31, 2021 No.01
offsetwidth\clientwidth\scrollwidth
使用 Open Connector 进行 HubSpot 和 SAP 系统的集成工作
Noip1998-2018 csp-s2 2019 2021 improvement group problem solving report and video
R 编程语言 - 简介
Lenet5 training model of convolutional neural network for machine learning
Curve 替换 Ceph 在网易云音乐的实践
QQ出现大规模盗号,为什么会这样?就没有解决方法了吗?
China energy integration and Tianyi cloud create an "energy brain"
【208】基于AccessToken方式实现API设计
如何让你的 WordPress 网站更安全
NOIP普及组2006-2018初赛 2019 CSP-J1 2020 CSP-J1 完善程序题