Array(5) -- Sort Colors,Set Matrix Zeroes
来源:互联网 发布:java编程思想英文版 编辑:程序博客网 时间:2024/06/05 20:06
Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
解法1:使用两个值标记目前已确定的最后面的0和最前面的2;如果是0就向前移动,如果是2就向后移动。
void sortColors(vector<int>& nums) { int redPos = 0; int bluePos = nums.size() - 1; for(int i = 0; i < nums.size(); i++){ if(nums[i] == 0){ while(nums[redPos] == 0) redPos++; if(i > redPos) swap(nums[redPos++], nums[i--]); } else if(nums[i] == 2){ while(nums[bluePos] == 2) bluePos--; if(i < bluePos) swap(nums[bluePos--], nums[i--]); else if(i >= bluePos) return; } } }
解法2:不交换位置,在每种颜色的最后进行写入。写入0,会覆盖掉1个后面的1和2;写入1会覆盖掉后面的一个2,;写入2对其他没有影响。
void sortColors(vector<int>& nums) { int r=0, w=0, b=0; // label the end of different colors; for(int num: nums){ if(num==0) {nums[b++]=2; nums[w++]=1; nums[r++]=0; } else if(num==1) {nums[b++]=2; nums[w++]=1;} else if(num==2) b++; }}
Set Matrix Zeroes
将每行和每列的状态存储在行列的头部,用额外的col0记录第一列的状态。
一般空间优化的思路:1)只保留一个阶段的状态,空间循环使用 2)使用已有的空间进行存储
void setZeroes(vector<vector<int>>& matrix) { int col0 = 1; int m = matrix.size(), n = matrix[0].size(); for(int i = 0; i < m; i++){ if(matrix[i][0] == 0) col0 = 0; for(int j = 1; j < n; j++){ if(matrix[i][j] == 0){ matrix[i][0] = 0; matrix[0][j] = 0; } } } for(int i = m - 1; i > -1; i--){ for(int j = n - 1; j > 0; j--){ if(matrix[i][0] == 0 || matrix[0][j] == 0) matrix[i][j] = 0; } if(!col0) matrix[i][0] = 0; } }
Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
How many possible unique paths are there?
解法1:DP,如果一个块有两个方向可以到达,则该块可能路径数为两者之和;否则等于上一方向
int uniquePaths(int m, int n) { int path[m][n]; path[0][0] = 1; for (int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ if(i - 1 >= 0 && j - 1 >= 0) path[i][j] = path[i-1][j] + path[i][j-1]; else if(j - 1 >= 0 && !(i - 1 >= 0)) path[i][j] = path[i][j-1]; else if(i - 1 >= 0 && !(j - 1 >= 0)) path[i][j] = path[i-1][j]; } } return path[m-1][n-1]; }
解法2:机器人一共要走m+n-2步,需要在m+n-2中选m-1步向下走,即 (m-1)C(m+n-2), 空间复杂度O(1)
public int uniquePaths(int m, int n) { if(m == 1 || n == 1) return 1; m--; n--; if(m < n) { // Swap, so that m is the bigger number m = m + n; n = m - n; m = m - n; } long res = 1; int j = 1; for(int i = m+1; i <= m+n; i++, j++){ // Instead of taking factorial, keep on multiply & divide res *= i; res /= j; } return (int)res; }
- Array(5) -- Sort Colors,Set Matrix Zeroes
- LeetCode[Array]: Set Matrix Zeroes
- [leetcode][array] Set Matrix Zeroes
- Array-----73. Set Matrix Zeroes
- Leetcode 73 - Set Matrix Zeroes(Array)
- LeetCode刷题【Array】 Set Matrix Zeroes
- leetcode.array--73. Set Matrix Zeroes
- 【leetcode】Array—— Set Matrix Zeroes(73)
- LeetCode: Set Matrix Zeroes
- Set Matrix Zeroes
- Set Matrix Zeroes
- LeetCode Set Matrix Zeroes
- [Leetcode] Set Matrix Zeroes
- LeetCode: Set Matrix Zeroes
- Set Matrix Zeroes
- Leetcode: Set Matrix Zeroes
- [LeetCode] Set Matrix Zeroes
- Set Matrix Zeroes
- 12.16《采用 缓冲输入 、 输出字节流 来实现图片的拷贝》
- VC++头文件中的条件编译问题(转载)
- 第十六周 点结构体
- ThinkPHP 遇到Call to undefined function ImageCreateFrombmp() 问题解决
- C Primer Plus 练习p31
- Array(5) -- Sort Colors,Set Matrix Zeroes
- c语言50题之小球落地问题、猴子吃桃问题
- B/S架构简介
- spring scope prototype与singleton区别
- STM32学习10
- 一个绚丽的Downloading动效分析与实现
- 练习c语言题目
- 天池体验(二)——新人离线赛数据可视化分析
- 案例三————健康栏目