当前位置:网站首页>1323. Maximum number of 6 and 9
1323. Maximum number of 6 and 9
2022-07-06 16:07:00 【mrbone9】
Address :
Power button
https://leetcode-cn.com/problems/maximum-69-number/
subject :
Give you a number only 6 and 9 Positive integer composed of num.
You can only flip one digit at most , take 6 become 9, Or the 9 become 6 .
Please return the maximum number you can get .
Example 1:
| Input :num = 9669 Output :9969 explain : Change the first digit to get 6669 . Change the second digit to get 9969 . Change the third digit to get 9699 . Change the fourth digit to get 9666 . The biggest number is 9969 . |
Example 2:
| Input :num = 9996 Output :9999 explain : Take the last one from 6 Change to 9, As a result, 9999 It's the biggest number . |
Example 3:
| Input :num = 9999 Output :9999 explain : It's the biggest number without change . |
Tips :
| 1 <= num <= 10^4 num The number in each digit is 6 perhaps 9 . |
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/maximum-69-number
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas :
Intuition is to put numbers into an integer array , So each bit represents a carry , Convenient operation
The integer number is traversed in order to get each digit , such as :
| Numbers | %10 | /10 |
| 1234 | 4 | 123 |
| 123 | 3 | 12 |
| 12 | 2 | 1 |
| 1 | 1 | 0 |
Method 1 、 Integer array save value operation
int maximum69Number (int num){
int ret = 0;
int cnum[5];
memset(cnum, 0, sizeof(cnum));
int i = 0;
while(num)
{
cnum[i++] = num % 10;
num /= 10;
}
int clen = i;
for(int j=clen-1; j>=0; j--)
{
int ch = cnum[j];
if(ch != 9)
{
cnum[j] = 9;
break;
}
}
for(i=clen-1; i>=0; i--)
{
ret *= 10;
ret += cnum[i];
}
return ret;
}边栏推荐
- Web based photo digital printing website
- 信息安全-威胁检测-flink广播流BroadcastState双流合并应用在过滤安全日志
- Essai de pénétration (1) - - outils nécessaires, navigation
- Penetration test (7) -- vulnerability scanning tool Nessus
- Analyse du format protobuf du rideau en temps réel et du rideau historique de la station B
- SSM框架常用配置文件
- [exercise-8] (UVA 246) 10-20-30== simulation
- F - birthday cake (Shandong race)
- JS调用摄像头
- 1010 things that college students majoring in it must do before graduation
猜你喜欢

B - Code Party (girls' competition)

Pyside6 signal, slot
Frida hook so layer, protobuf data analysis

Analysis of protobuf format of real-time barrage and historical barrage at station B

渗透测试 ( 5 ) --- 扫描之王 nmap、渗透测试工具实战技巧合集

信息安全-安全编排自动化与响应 (SOAR) 技术解析

Essai de pénétration (1) - - outils nécessaires, navigation

Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability

B - 代码派对(女生赛)

1010 things that college students majoring in it must do before graduation
随机推荐
[exercise -11] 4 values why sum is 0 (and 4 values of 0)
The most complete programming language online API document
JS call camera
Path problem before dynamic planning
Opencv learning log 19 skin grinding
X-Forwarded-For详解、如何获取到客户端IP
Penetration test (8) -- official document of burp Suite Pro
C basic grammar
C language must memorize code Encyclopedia
Penetration test (7) -- vulnerability scanning tool Nessus
Differential (one-dimensional, two-dimensional, three-dimensional) Blue Bridge Cup three body attack
Gartner:关于零信任网络访问最佳实践的五个建议
Gartner: five suggestions on best practices for zero trust network access
用C语言写网页游戏
【高老师UML软件建模基础】20级云班课习题答案合集
China's peripheral catheter market trend report, technological innovation and market forecast
Hdu-6025-prime sequence (girls' competition)
Programmers, what are your skills in code writing?
New to redis
想应聘程序员,您的简历就该这样写【精华总结】