当前位置:网站首页>LeetCode · Question of the Day · 1417. Reformatting String · Simulation
LeetCode · Question of the Day · 1417. Reformatting String · Simulation
2022-08-11 10:33:00 【Xiao Xun wants to become stronger】
Link: https://leetcode.cn/problems/reformat-the-string/solution/by-xun-ge-v-aazm/
Source: LeetCode
Copyright belongs to the author.For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.
Title
Example 
Thoughts
Solution ideas
According to the meaning of the question, we need to reconstruct a string, which must be followed by letters and numbers, and numbers should be followed by letters.
So only when the difference between the number of characters and the number of digits in the string is less than 1, it can be constructed
I can't think of it through simple analog construction. When there are more characters or numbers, we must start from the one with more characters. Because there is one more, we must start and put one at the end to meet the requirements
It is more convenient to use double pointers during construction
Code comments are super detailed
Code
char * reformat(char * s){int len = strlen(s);// Find the length of the stringint sum = 0;for(int i = 0; i < len; i++)//traverse the string to find the length of the number{char ch = s[i];if (isdigit(ch)) {//Determine whether the character is a digital functionsum++;}}int summ = len - sum;if(abs(summ - sum) > 1)//Judge the difference{return "";}int logo = summ > sum ? 1 : 0;for(int i = 0, j = 0; i < len; i++){if(logo == 1)//There are many letters, the letters are constructed first{if(!isdigit(s[j]) && j == 0)//The beginning is a letter, no additional processing is required{j++;continue;}}else//There are many numbers, the numbers are constructed first{if(isdigit(s[j]) && j == 0)//The beginning is a number, no additional processing is required{j++;continue;}}//If the beginning does not meet the requirements, it is actually the same as the usual construction process.//When the elements pointed to by the two pointers are the same, the slow pointer stays at the current position, and the fast pointer finds a different element and exchanges it with itif( (isdigit(s[j]) && isdigit(s[i])) || (!isdigit(s[j]) && !isdigit(s[i])) ){continue;}char ch = s[i];s[i] = s[j];s[j] = ch;j += 2;}return s;}Author: xun-ge-vLink: https://leetcode.cn/problems/reformat-the-string/solution/by-xun-ge-v-aazm/Source: LeetCodeCopyright belongs to the author.For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.边栏推荐
猜你喜欢

保证金监控中心保证期货开户和交易记录

Deploying Robot Vision Models Using Raspberry Pi and OAK Camera

Primavera Unifier advanced formula usage sharing

Unity shader test execution time

The mathematical knowledge required for neural networks, the mathematical foundation of neural networks

Word小技巧之图表实现自动编号和更新

解决 Pocess finished with exit code 1 Class not found 和 Command line is too long. Shorten the command

Primavera Unifier - AEM Form Designer Essentials

Database indexes and their underlying data structures

How to improve the efficiency of telecommuting during the current epidemic, sharing telecommuting tools
随机推荐
idea plugin autofill setter
NT 内核函数原型大全
卷积神经网络梯度消失,神经网络中梯度的概念
为什么有些人不喜欢出身底层的人?
困扰所有SAP顾问多年的问题终于解决了
Segmentation Learning (loss and Evaluation)
[Building a 2D rasterized map using SLAM technology]
【luogu CF1427F】Boring Card Game(贪心)(性质)
杰理AC632N蓝牙芯片iokey使用解析(通用MCU版)
MySQL表sql语句增删查改_修改_删除
神经网络需要的数学知识,神经网络的数学基础
企业展厅制作要具备的六大功能
一站式PCBA组装加工有哪些环节?
神经网络图怎么分析,画神经网络结构图
【luogu CF1286E】Fedya the Potter Strikes Back(字符串)(KMP)(势能分析)(线段树)
数据库的索引和其底层数据结构
使用.NET简单实现一个Redis的高性能克隆版(七-完结)
MySQL表sql语句增删查改_增加
【分享】PPT还能做成这样?你一定没见过
网络流行简笔画图片大全,关于网络的简笔画图片