当前位置:网站首页>The number of different integers in the character string of [3 questions (3) per day]
The number of different integers in the character string of [3 questions (3) per day]
2022-06-28 16:58:00 【Programmed ape without hair loss 2】
subject :
Give you a string word , The string consists of numbers and lowercase letters .
Please replace each character that is not a number with a space . for example ,“a123bc34d8ef34” Will become " 123 34 8 34" . Be careful , The remaining integers are ( Adjacent to each other with at least one space ):“123”、“34”、“8” and “34” .
Return to right word Formed after replacement Different The number of integers .
Only if two integers Without leading zeros The decimal representation of is different , I think these two integers are also different .
Example 1:
Input :word = “a123bc34d8ef34”
Output :3
explain : Different integers are “123”、“34” and “8” . Be careful ,“34” Count only once .
Example 2:
Input :word = “leet1234code234”
Output :2
Example 3:
Input :word = “a1b01c001”
Output :1
explain :“1”、“01” and “001” Treat as a decimal representation of the same integer , Because leading zeros are ignored when comparing decimal values .
Tips :
1 <= word.length <= 1000
word It's made up of numbers and lowercase letters
Ideas :
Use regular handle word Proceed alphabetically split, The resulting array is empty or numeric , Then remove the leading from the number 0
java Code :
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; // Processing leading zeros , Because thinking about ‘00000’ The situation of , So go to the last one before the last one
while (words[i].charAt(j)=='0'&&j<words[i].length()-1){
j++;
}
set.add(words[i].substring(j));
}
return set.size();
}
}
边栏推荐
- 【TcaplusDB知识库】TcaplusDB技术支持介绍
- On the design principle of price discount in SAP software
- 关于接口测试自动化的总结与思考
- Research on master's thesis writing
- BOM in JS
- 12 SQL optimization schemes summarized by old drivers (very practical)
- 如何在网站上安装 WordPress
- How to query the last data according to multiple indexes to achieve the effect of SQL order by desc limit 1?
- Cross cluster deployment of helm applications using karmada
- Practice of curve replacing CEPH in Netease cloud music
猜你喜欢
NOIP普及组2006-2018初赛 2019 CSP-J1 2020 CSP-J1 完善程序题
使用Karmada实现Helm应用的跨集群部署
This simple little function saves 213 hours for our production research team in half a year
Potplayer plays Baidu cloud disk video
【TcaplusDB知识库】TcaplusDB技术支持介绍
[tcapulusdb knowledge base] tcapulusdb technical support introduction
Design details of the full stack CRM development tool webclient UI workbench
免费、强大、高颜值的笔记软件评测: OneNote、Heptabase、氢图、FlowUs
Practice of curve replacing CEPH in Netease cloud music
Curve 替换 Ceph 在网易云音乐的实践
随机推荐
With high concurrency, high availability and elastic expansion, Tianyi cloud escorts Enterprise Cloud business
Potplayer play Baidu Cloud disk video
【尚硅谷与腾讯云官方合作】硅谷课堂项目视频发布
Solve the problem that subcomponents will not be destroyed through setTimeout
PID control details [easy to understand]
np tips: random 创建随机矩阵 sample = np.random.random([19, 64 , 64, 3])
MATLB|可视化学习(plot和bar)
This simple little function saves 213 hours for our production research team in half a year
编写自己的 WordPress 模板
General solution of island problems and DFS framework
Design details of the full stack CRM development tool webclient UI workbench
基于Krack的网络攻击「建议收藏」
NOIP1998-2018 CSP-S2 2019 2021提高组解题报告与视频
【世界海洋日】TcaplusDB号召你一同保护海洋生物多样性
【Redis】2021/01/31 Redis的简单归纳 No.01
软件快速交付真的需要以安全为代价吗?
Flex layout
基数排序——【常见排序法(2/8)】
Noip2011-2018 problem solving report of improvement group
Gartner announces five privacy trends from today to 2024