当前位置:网站首页>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 使用之 CEP
- F - birthday cake (Shandong race)
- 【高老师软件需求分析】20级云班课习题答案合集
- The concept of C language array
- Opencv learning log 32 edge extraction
- Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs
- Penetration test (3) -- Metasploit framework (MSF)
- Alice and Bob (2021 Niuke summer multi school training camp 1)
- Research Report of exterior wall insulation system (ewis) industry - market status analysis and development prospect prediction
猜你喜欢
PySide6 信号、槽
C language learning notes
[exercise-7] crossover answers
Information security - threat detection - Flink broadcast stream broadcaststate dual stream merging application in filtering security logs
Determine the Photo Position
渗透测试 ( 5 ) --- 扫描之王 nmap、渗透测试工具实战技巧合集
Analyse du format protobuf du rideau en temps réel et du rideau historique de la station B
Information security - Epic vulnerability log4j vulnerability mechanism and preventive measures
Ball Dropping
mysql导入数据库报错 [Err] 1273 – Unknown collation: ‘utf8mb4_0900_ai_ci’
随机推荐
Determine the Photo Position
VS2019初步使用
信息安全-安全专业名称|CVE|RCE|POC|VUL|0DAY
Penetration test (2) -- penetration test system, target, GoogleHacking, Kali tool
Penetration test 2 --- XSS, CSRF, file upload, file inclusion, deserialization vulnerability
Path problem before dynamic planning
C language must memorize code Encyclopedia
Research Report on shell heater industry - market status analysis and development prospect forecast
Penetration testing (5) -- a collection of practical skills of scanning King nmap and penetration testing tools
Web based photo digital printing website
7-1 懂的都懂 (20 分)
Analyse du format protobuf du rideau en temps réel et du rideau historique de la station B
B - Code Party (girls' competition)
[exercise -11] 4 values why sum is 0 (and 4 values of 0)
基于web的照片数码冲印网站
Understand what is a programming language in a popular way
信息安全-史诗级漏洞Log4j的漏洞机理和防范措施
【练习-1】(Uva 673) Parentheses Balance/平衡的括号 (栈stack)
渗透测试 2 --- XSS、CSRF、文件上传、文件包含、反序列化漏洞
Penetration test (7) -- vulnerability scanning tool Nessus