当前位置:网站首页>[C language brush leetcode] 1432 The maximum difference that can be obtained by changing an integer (m)
[C language brush leetcode] 1432 The maximum difference that can be obtained by changing an integer (m)
2022-07-18 21:20:00 【kinbo88】
【
Give you an integer num . You can do the following steps for it two :
Select a number x (0 <= x <= 9).
Choose another number y (0 <= y <= 9) . Numbers y Can be equal to x .
take num All appear in x All digits are used y Replace .
The new integer obtained You can't A leading 0 , The new integer obtained is also You can't yes 0 .
Make twice right num The results of the operation are a and b .
Please return a and b Of Maximum difference .
Example 1:
Input :num = 555
Output :888
explain : First choice x = 5 And y = 9 , And save the new numbers in a in .
Second choice x = 5 And y = 1 , And save the new numbers in b in .
Now? , We have a = 999 and b = 111 , The maximum difference is 888
Example 2:
Input :num = 9
Output :8
explain : First choice x = 9 And y = 9 , And save the new numbers in a in .
Second choice x = 9 And y = 1 , And save the new numbers in b in .
Now? , We have a = 9 and b = 1 , The maximum difference is 8
Example 3:
Input :num = 123456
Output :820000
Example 4:
Input :num = 10000
Output :80000
Example 5:
Input :num = 9288
Output :8700
Tips :
1 <= num <= 10^8
source : Power button (LeetCode)
link :https://leetcode.cn/problems/max-difference-you-can-get-from-changing-an-integer
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
】
First, put the numbers into the array by bits .
Then turn this number to the maximum at one time , Then turn this number to the minimum again , Many conditional judgments , The code is longer than the .
This question is mainly about details .
int idx;
void tonumarr(int num, int *numarr) {
int tmp;
while(num) {
tmp = num % 10;
numarr[idx++] = tmp;
num = num / 10;
}
}
int getnum(int *newarr) {
int i;
int ret = 0;
for (i = idx - 1; i >= 0; i--) {
ret = ret * 10 + newarr[i];
}
return ret;
}
int getmin(int num, int *numarr) {
int ret = 0;
int i;
int *newarr = NULL;
int tmp = 0xff; // initialization
int flag = 0xff;
if (idx == 1) { // Single digits are set directly 1
return 1;
}
newarr = (int *)malloc(sizeof(int) * 9);
memcpy(newarr, numarr, sizeof(int) * 9);
// This is definitely not a single digit , Judge whether the first place is 1
if (newarr[idx - 1] != 1) {
tmp = newarr[idx - 1]; // That is, we need to put all tmp Switch to 1
newarr[idx - 1] = 1;
}
for (i = idx - 2; i >= 0; i--) { // Can run in at least once
if (flag == 0xff && newarr[i] != 0 && newarr[i] != newarr[idx - 1]) {
if (tmp == 0xff) { // The first is 1 Words , Start to change in the second place
tmp = newarr[i];
flag = 0; // The second place can be changed directly into 0
} else {
flag = 1; // First non 1 Words , Can only become 1
}
}
if (newarr[i] == tmp) {
newarr[i] = flag;
}
}
ret = getnum(newarr);
return ret;
}
int getmax(int num, int *numarr) {
int ret = 0;
int i;
int *maxarr = NULL;
int tmp = 0xff; // initialization
int flag = 0;
if (idx == 1) { // Single digits are set directly 9
return 9;
}
maxarr = (int *)malloc(sizeof(int) * 9);
memcpy(maxarr, numarr, sizeof(int) * 9);
// This is definitely not a single digit , Judge whether the first place is 1
if (maxarr[idx - 1] != 9) {
tmp = maxarr[idx - 1]; // That is, we need to put all tmp Switch to 9
maxarr[idx - 1] = 9;
}
for (i = idx - 2; i >= 0; i--) { // Can run in at least once
if (tmp == 0xff && maxarr[i] != maxarr[idx - 1]) { // The first is 9 Words , Start to change in the second place
tmp = maxarr[i];
}
if (maxarr[i] == tmp) {
maxarr[i] = 9;
}
}
ret = getnum(maxarr);
return ret;
}
int maxDiff(int num){
int min;
int max;
int *numarr = (int *)malloc(sizeof(int) * 9);
memset(numarr, 0, sizeof(int) * 9);
idx = 0;
tonumarr(num, numarr);
min = getmin(num, numarr);
max = getmax(num, numarr);
return max - min;
}
/* Wrong point :
1. if (newarr[idx - 1] != 1) The first judgment here , No assignment to the first
2. if (tmp == 0xff) tmp Judgment should only be entered once , and for Every time the loop goes
3. If the first one is 1, The second is also 1, Then the second place cannot be changed , This is not considered
4. Find the minimum , If the second is 0 No choice , This is also missing
*/边栏推荐
- R language uses pcauchy function to generate Cauchy distribution cumulative distribution function data, and uses plot function to visualize Cauchy distribution cumulative distribution function data
- World Tour Finals 2019 D - Distinct Boxes 题解
- Redis cluster
- R language ggplot2 visualization: use the ggecdf function of ggpubr package to visualize the empirical cumulative density function curve
- UE4阴影:PerObjectShadow验证
- 博客从 CloudBase 迁移至云主机
- Learning notes of Transe model
- MySQL common commands for viewing database performance
- R语言使用glm函数构建泊松对数线性回归模型处理三维列联表数据构建饱和模型、使用step函数基于AIC指标实现逐步回归筛选最佳模型
- 剑指 Offer 57. 和为s的两个数字
猜你喜欢

Stm32f407---- power management

产业园区如何做好精细化运营管理

Pending issues

High numbers | calculation of double integral 1 | high numbers | handwritten notes

鸿蒙开发板上安装HAP应用方法之经典

Find out the motivation and needs of enterprise location, and carry out investment attraction work efficiently

General Administration of market supervision: 15 batches of ice cream products such as Pudding Ice cream are unqualified

鲁汶大学招募博士后研究员,利用AI/ML对太阳活动区图像进行分析和耀斑预测...

Sword finger offer 53 - ii Missing numbers from 0 to n-1

Machine learning BP (back propagation) neural network
随机推荐
UE4 shadow: perobjectshadow verification
R language uses LM function to build multiple regression model, writes regression equation according to model coefficient, and uses summary function to calculate the summary statistical information of
Programming implementation of I2C communication protocol
Dynamically adding routes and refreshing the page will show a blank screen
R语言ggplot2可视化:使用ggpubr包的gghistogram函数可视化直方图、使用add参数在直方图中添加均值虚线竖线、横轴添加轴须图(rug plot)
剑指 Offer 53 - I. 在排序数组中查找数字 I
一步到位玩透Ansible-目录
Epic-kbs9 industrial computer brushing document
Discussion on ble Bluetooth battery service
Learning notes of Transe model
OpenCV 教程 02: OpenCV 的核心操作
Sword finger offer 57 And are two numbers of S
World Tour Finals 2019 D - Distinct Boxes 题解
Mathematical modeling - Classification Model (based on logistic regression)
Win10 how to convert FAT32 format disks into NFTs format without formatting
What is industrial planning? How to make industrial planning for industrial parks
On array method reconstruction and re encapsulation -foreach map -- push (), unshift (), shift (), map (), filter (), every (), some (), reduce ()
摸清企业选址动机及需求,高效开展招商引资工作
UE4 Lights UWorld to FScene [2]
R语言ggplot2可视化:使用ggpubr包的ggecdf函数可视化经验累积密度分布函数曲线(Empirical cumulative density function)