当前位置:网站首页>最大交换[贪心思想&单调栈实现]
最大交换[贪心思想&单调栈实现]
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 最大交换
边栏推荐
- CDGA|工业互联网行业怎么做好数据治理?
- Article translation software - batch free translation software supports major translation interfaces
- “中国网事·感动2022”二季度网络感动人物评选结果揭晓
- How navicate modifies the database name
- Source code analysis of scripy spider
- [C language] string reverse order implementation (recursion and iteration)
- C language operators and input and output
- [C language] simulation implementation of pow function (recursion)
- How to write the SQL statement of time to date?
- Leetcode Day1 score ranking
猜你喜欢

How many types of rain do you know?

通信网络基础知识01

Saltstack advanced

5. Difference between break and continue (easy to understand version)

JS preventdefault() keyboard input limit onmousewheel stoppropagation stop event propagation

Concurrent programming, do you really understand?
![[C language] summary of methods for solving the greatest common divisor](/img/38/3a099948ebf51fd0da3076f71f9dad.png)
[C language] summary of methods for solving the greatest common divisor

Hebei: stabilizing grain and expanding beans to help grain and oil production improve quality and efficiency

Implementation of strstr in C language

跨区域网络的通信学习静态路由
随机推荐
【NPP安装插件】
Article translation software - batch free translation software supports major translation interfaces
Integration and implementation of login click graphic verification code in personal blog system
C language array and bubble sort
[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
9. Pointer of C language (2) wild pointer, what is wild pointer, and the disadvantages of wild pointer
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?
Source code analysis of scripy spider
私有化部署的即时通讯平台,为企业移动业务安全保驾护航
通信网络基础知识01
利用STM32的HAL库驱动1.54寸 TFT屏(240*240 ST7789V)
Saltstack advanced
云计算笔记part.2——应用管理
认识中小型局域网WLAN
[C language] Hanoi Tower problem [recursion]
String中常用的API
CodeIgnier框架实现restful API接口编程
KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
Getting started with enterprise distributed crawler framework
Use of strtok and strError