当前位置:网站首页>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.
边栏推荐
猜你喜欢
爆料!前华为微服务专家纯手打500页落地架构实战笔记,已开源
【Mysql系列】03_系统设计
How to determine the neural network parameters, the number of neural network parameters calculation
Array, string, date notes [Blue Bridge Cup]
二、第二章变量
Network model (U - net, U - net++, U - net++ +)
OAK-FFC系列产品上手指南
[UE] 入坑
解决 Pocess finished with exit code 1 Class not found 和 Command line is too long. Shorten the command
Primavera P6 Professional 21.12 Login exception case sharing
随机推荐
杰理AC632N蓝牙芯片iokey使用解析(通用MCU版)
神经网络参数如何确定的,神经网络参数个数计算
错误代码: 1118 - Row size too large (> 8126). Changing some columns to TEXT or BLOB may help. In current
HDRP shader gets pixel depth value and normal information
【教程】区块链是数据库?那么区块链的数据存储在哪里?如何查看数据?FISCO-BCOS如何更换区块链的数据存储,由RocksDB更换为MySQL、MariaDB,联盟链区块链数据库,区块链数据库应用
【综合练习12】实现静态网页的相关功能
servlet——servlet执行流程 | servlet关系视图
SDS观察站
MySQL表sql语句增删查改_增加
Neuropathic pain classification picture Daquan, neuropathic pain classification
10Super详解
数字钱包红海角逐,小程序生态快速引入可助力占领智慧设备入口
卷积神经网络梯度消失,神经网络中梯度的概念
HDRP Custom Pass Shader Get world coordinates and near clipping plane coordinates
WooCommerce Ecommerce WordPress Plugin - Make American Money
【Daily Question】640. Solving Equations
Database Basics
神经网络图怎么分析,画神经网络结构图
分割学习(loss and Evaluation)
AcWing 273. 分级(线性DP+结论)