当前位置:网站首页>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;
}
边栏推荐
- 信息安全-安全编排自动化与响应 (SOAR) 技术解析
- Shell Scripting
- STM32 learning record: LED light flashes (register version)
- 1010 things that college students majoring in it must do before graduation
- Interesting drink
- Analyse du format protobuf du rideau en temps réel et du rideau historique de la station B
- Opencv learning log 19 skin grinding
- Research Report on shell heater industry - market status analysis and development prospect forecast
- [exercise-3] (UVA 442) matrix chain multiplication
- D - function (HDU - 6546) girls' competition
猜你喜欢
渗透测试 2 --- XSS、CSRF、文件上传、文件包含、反序列化漏洞
Penetration testing (5) -- a collection of practical skills of scanning King nmap and penetration testing tools
信息安全-安全编排自动化与响应 (SOAR) 技术解析
Luogu P1102 A-B number pair (dichotomy, map, double pointer)
Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs
渗透测试 ( 7 ) --- 漏洞扫描工具 Nessus
Data storage in memory & loading into memory to make the program run
Programmers, what are your skills in code writing?
渗透测试 ( 8 ) --- Burp Suite Pro 官方文档
1010 things that college students majoring in it must do before graduation
随机推荐
信息安全-威胁检测引擎-常见规则引擎底座性能比较
nodejs爬虫
Ball Dropping
Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
Borg maze (bfs+ minimum spanning tree) (problem solving report)
Luogu P1102 A-B number pair (dichotomy, map, double pointer)
Data storage in memory & loading into memory to make the program run
Research Report of cylindrical grinder industry - market status analysis and development prospect forecast
Shell脚本编程
mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’
Information security - Analysis of security orchestration automation and response (soar) technology
D - function (HDU - 6546) girls' competition
【练习-7】(Uva 10976)Fractions Again?!(分数拆分)
Penetration test (7) -- vulnerability scanning tool Nessus
E. Breaking the Wall
用C语言写网页游戏
【练习-6】(Uva 725)Division(除法)== 暴力
Information security - security professional name | CVE | rce | POC | Vul | 0day
F - birthday cake (Shandong race)
Perform general operations on iptables