双指针和codetop复习
双指针和codetop复习
- 1.双指针
- 1.[移动零](https://leetcode.cn/problems/move-zeroes/description/)
- 递归
- 1.[计算布尔二叉树的值](https://leetcode.cn/problems/evaluate-boolean-binary-tree/)
- 2.[Pow(X,n)](https://leetcode.cn/problems/powx-n/)
- 3.[两两交换链表中的节点](https://leetcode.cn/problems/swap-nodes-in-pairs/description/)
- 动态规划
- 1.[不同路径](https://leetcode.cn/problems/unique-paths/description/)
- 2.[不同路径II](https://leetcode.cn/problems/unique-paths-ii/description/)
- 贪心
- 1.[最大数](https://leetcode.cn/problems/largest-number/description/)
1.双指针
1.移动零
//创建双指针,cur=0,dest=-1,
//cur的作用,扫描数组,nums[cur]==0,cur++ nums[cur]!=0时,再处理
//这样就把数组分成三个部分,[0,dest]:已经处理的[dest+1,cur-1]:里面全部是0,[cur,size]:全是待处理的部分
递归
1.计算布尔二叉树的值
2.Pow(X,n)
//这种暴力递归不可取,计算 myPow(x, n) 时,需要递归 n 次(比如 n=10000 就要递归 10000 层)。当 n 很大(比如 n=1e9),会触发栈溢出或超时
//这道题叫快速幂,所以在上面的暴力做优化,每次都算n的一半,比如n=10,第一次算n=5,第二次算n=2,这样就可以达到快速降幂
3.两两交换链表中的节点
//提前保存要返回的指针也就是第一次head->next,然后只交换节点中的val,然后head向后走两步
动态规划
1.不同路径
//mn,但是开(m+1n+1),把(0,1)或者(1,0)初始化为1
2.不同路径II
贪心
1.最大数
//先把所有数字to_string到vector ,然后把里面的所有string用sort(默认升序)排序,用sort时,重新写下排序规则 [](string s1,string s2){return s1+s2>s2+s1}