Leetcode 75:Sort Colors

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.

Note:
You are not suppose to use the library's sort function for this problem.


【算法思路】:重新new一个新的数组出来,接受依次接收新的0,1,2.但是由于无返回型的方法,所以一直没有AC。


小总结一下:怎样获取长度?

如果是数组的话就是:数组.length属性;如果是字符串的话就是:字符串.length()方法;如果是集合的话就是:集合.size()方法


public class Solution {
    public void sortColors(int[] nums) {
       
        int len = nums.length;
        //String result="";
        int[] result = new int[len];
        int n = 0;
         
        for(int i = 0;i<len-1;i++)
            if(nums[i]==0){
                result[n++]=nums[i];
                
            }
        
        
        for(int i = 0;i<len-1;i++)
            if(nums[i]==1){
                result[n++]=nums[i];
               
            }
        
        
        for(int i = 0;i<len-1;i++)
            if(nums[i]==2){
                result[n++]=nums[i];
            }
        
        return result;
        
       // result.toCharArray();
    }
}

【AC算法思路】:最直接的方法是,计数排序。先计算0,1,2的个数,然后重新给数组赋值……两次循环,一次计数,一次赋值

public class Solution {
    public void sortColors(int[] nums) {
       
        int len = nums.length;
        int m = 0;//分别来计算0,1,2的个数
        int n = 0;
        int l = 0;
        
        for(int i = 0;i<len;i++){
            
            if(nums[i]==0){
                m++;
                
            }
        
            else if(nums[i]==1){
                n++;
               
            }
            else if(nums[i]==2){
                l++;
            }
        }
        
        for(int i = 0;i<len;i++){
            if(i < m)
                nums[i] = 0;
                
            else if(i >= m && i < m + n)//重m到m+n为1,剩下的为2(前提只有0,1,2三种数)
                nums[i] = 1;
                
            else
                nums[i] = 2;
        }
        
       
    }
}

【AC算法思路】:一次循环实现的算法,设置两个index,left记录第一个1的位置,left左边为0,right记录第一个非2的位置,right右边为2.

由于只扫一遍,左边存放0和1,右边存放2.两边往中间靠。然后使用i从头到尾扫一遍,直到与right相遇。

i遇到0就换到左边去,遇到2就换到右边去,遇到1就跳过。

需要注意的是:由于left记录第一个1的位置,因此A[left]与A[i]交换后,A[left]为0,A[i]为1,因此left++,i++;

而right记录第一个非2的位置,可能为0或1,因此A[right]与A[i]交换后,A[right]为2,A[i]为0或1,i不能前进,要后续判断,rigth--。

由此该数组分为4段:[0,left)-->0; [left,i)-->1; [i,right]-->乱序; (right,n-1]-->2

0  0  0  1  1  1  2  1  0  2  1  2  2  2

           ^         ^             ^

          left         i            right


java代码:

public class Solution {
    public void sortColors(int[] nums) {
        int len = nums.length;
        int left= 0;//left记录第一个1的位置,left左边为0
        int rigth = len-1;//,right记录第一个非2的位置,right右边为2.
        int i = 0;
        while(i <= rigth){
            if(nums[i]==0){
                
                int t;
                t = nums[i];
                nums[i] =nums[left];
                nums[left]= t;
                
              //  swap(nums[i],nums[left]);
                left++;
                i++;
            }
            
            else if(nums[i]==1){
                i++;
            }
            
            else{
                 int t;
                t = nums[i];
                nums[i] =nums[rigth];
                nums[rigth]= t;
                
               // swap(nums[i],nums[rigth]);
                rigth--;
            }
        }
        
    }
   /* public void swap(int a,int b){
        int t;
        t = a;
        a = b;
        b = t;
    }*/
}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值