Array(5) -- Sort Colors,Set Matrix Zeroes

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;
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值