当前位置:网站首页>最大交换[贪心思想&单调栈实现]
最大交换[贪心思想&单调栈实现]
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 最大交换
边栏推荐
- 5. Difference between break and continue (easy to understand version)
- Source insight project import and use tutorial
- [C language] string reverse order implementation (recursion and iteration)
- 2、 Relationship between software operation and memory
- 云计算笔记part.2——应用管理
- Implementation of memmove in C language
- C language function
- CodeIgnier框架实现restful API接口编程
- Failed to install app-debug. apk: Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]
- [网络]跨区域网络的通信学习IPv4地址的分类和计算
猜你喜欢

“中国网事·感动2022”二季度网络感动人物评选结果揭晓
![[C language] string reverse order implementation (recursion and iteration)](/img/c3/02d0a72f6026df8a67669293e55ef2.png)
[C language] string reverse order implementation (recursion and iteration)
![[C language] Fibonacci sequence [recursion and iteration]](/img/02/6cff776db583f1b149686e15649d41.png)
[C language] Fibonacci sequence [recursion and iteration]

Function fitting based on MATLAB
![[C language] print pattern summary](/img/48/d8ff17453e810fcd9269f56eda4d47.png)
[C language] print pattern summary

【NPP安装插件】

What is the process of swing event processing?

Saltstack advanced

JVM(二十四) -- 性能监控与调优(五) -- 分析GC日志

Const pointer of C language and parameter passing of main function
随机推荐
Concurrent programming, do you really understand?
[in depth study of 4g/5g/6g topics -44]: urllc-15 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -9-low delay technology -3-non slot scheduling mini
Why is there no log output in the telnet login interface?
How to write the SQL statement of time to date?
9. Pointer of C language (2) wild pointer, what is wild pointer, and the disadvantages of wild pointer
9. Pointer of C language (4) pointer and one-dimensional array, pointer operation
C+ + core programming
Leetcode day4 the highest paid employee in the Department
Data system of saltstack
Design of air combat game based on qtgui image interface
Two methods to judge the size end
CodeIgnier框架实现restful API接口编程
adb remount of the / superblock failed: Permission denied
C language functions and pointers
Servlet学习笔记
Leetcode day3 find duplicate email addresses
Find the memory occupied by the structure
Sprint for golden nine and silver ten, stay up at night for half a month, collect 1600 real interview questions from Android post of major manufacturers
Stories of Party members | Li qingai uses cartoons to drive farmers to increase income and become rich
Crawl IP