当前位置:网站首页>最大交换[贪心思想&单调栈实现]
最大交换[贪心思想&单调栈实现]
2022-07-28 18:06:00 【REN_林森】
前言
贪心/DP是练习/考察逻辑思维的好题目,一半步骤为先纸上模拟模拟找找规律,再寻找适合的数据结构,再用代码把自己的想法实现出来。
一、最大交换

二、贪心&单调栈
/* target:最多交换一次让数值最大。 很明显,如果数值从高到低位不成单调递减状态时,就把尽可能后面最大的值交换到尽可能的前。 贪心1:把后面大的和前面小的交换; 贪心2:尽可能,对于前面,要尽可能的往前找小的,相等时也需取左;对于后面,尽可能的往后找大的,当想等时也需取右侧。 总结:贪心思想,单调栈实现。 */
public int maximumSwap(int num) {
char[] arr = String.valueOf(num).toCharArray();
// step1:确定非单调处,以便开始往后寻最大值。
int i = 1;
while (i < arr.length && arr[i] <= arr[i - 1]) ++i;
if (i == arr.length) return num;
// step2:寻最大值所在处。
int idx = i;
while (i < arr.length) {
if (arr[idx] <= arr[i]) idx = i;
++i;
}
// step3:寻找尽可能前的位置。
i = 0;
while (i < arr.length && arr[i] >= arr[idx]) ++i;
// step4:交换位置。
swap(arr, i, idx);
return Integer.parseInt(String.valueOf(arr));
}
private void swap(char[] arr, int i, int j) {
char c = arr[i];
arr[i] = arr[j];
arr[j] = c;
}
总结
1)多多练习贪心/动态规划题目,对逻辑分析能力的提升有帮助。
2)面对问题,找找规律,根据数据结构特性选合适的数据结构,再用配合代码的分支/循环来解决问题,往往需要将问题转换,往数据结构&分支/循环/递归上靠。
参考文献
[1] LeetCode 最大交换
边栏推荐
- 软考中级(系统集成项目管理工程师)高频考点
- leetcode day1 分数排名
- 2、 Relationship between software operation and memory
- Leetcode Day5 delete duplicate email
- 4. Const and difine and the problem of initializing arrays with const and define
- Implementation of strcat in C language
- Overcome the "fear of looking at teeth", and we use technology to change the industry
- Thoroughly understand bit operations - shift left, shift right
- 6. Functions of C language, why functions are needed, how functions are defined, and the classification of functions
- 私有化部署的即时通讯平台,为企业移动业务安全保驾护航
猜你喜欢

Cloud computing notes part.2 - Application Management
![[C language] guessing numbers game [function]](/img/db/8ebdb02f137878224367503b730803.png)
[C language] guessing numbers game [function]

The cloud native programming challenge is hot, with 510000 bonus waiting for you to challenge!

leetcode day1 分数排名

JS batch add event listening onclick this event delegate target currenttarget onmouseenter OnMouseOver
![[C language] initial C language reflection and summary](/img/21/826d144867f7a73ec2cd8896a5250a.png)
[C language] initial C language reflection and summary

2022年下半年系统集成项目管理工程师认证8月20日开班

克服“看牙恐惧”,我们用技术改变行业

通信网络基础知识01

Overcome the "fear of looking at teeth", and we use technology to change the industry
随机推荐
[wechat applet development] page navigation and parameter transmission
HSETNX KEY_NAME FIELD VALUE 用法
MIR专题征稿 | 常识知识与推理:表示、获取与应用 (10月31日截稿)
河北邯郸:拓展基层就业空间 助力高校毕业生就业
9. Pointer of C language (3) classic program, exchange the value of two numbers for deep analysis, (easy to understand), are formal parameters and arguments a variable?
Cloud computing notes part.2 - Application Management
数字图像理论知识(一)(个人浅析)
The cloud native programming challenge is hot, with 510000 bonus waiting for you to challenge!
云原生编程挑战赛火热开赛,51 万奖金等你来挑战!
Overcome the "fear of looking at teeth", and we use technology to change the industry
3、 Are formal and actual parameters in a programming language variables?
时间转日期的sql语句应该怎么写?
河北:稳粮扩豆助力粮油生产提质增效
How many types of rain do you know?
Rand function generates pseudo-random numbers
Two methods to judge the size end
Application skills of programming rising and falling edge instructions of botu 1200/1500plc (bool array)
Know small and medium LAN WLAN
中国能否在元宇宙的未来发展中取得突破,占领高地?
Design of air combat game based on qtgui image interface