研究生菜鸟的leetcode起步第一天(数组基本操作篇)

研究生菜鸟的leetcode起步第一天

一、数组的基本操作

leetcode 第485题

这里出现的问题主要是,当数组后面的元素都是1时,只进行start++操作,而无法计入n中,因此做出了改进,增加了到数组末尾的判断条件。

class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        int n=0;
        int start=0;
        for(int i = 0;i < nums.length;i++){
            if(nums[i] == 1){
                start ++;
            }
            if((nums[i] == 0) || (i == nums.length-1)){
                if(start>n){
                    n = start;
                }
                start = 0;
            }
        }
        return n;
    }
}

 但up主给出了更简单的写法,只是再返回值时对count值和result值做出对比。

class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        int count = 0,result = 0;
        for(int i = 0;i < nums.length;i++){
            if(nums[i] == 1){
                count ++;
            }else{
                result = max(result,count);
                count = 0;
            }
        }
        return max(result,count);
    }
}

leetcode 第283题

 该题的官方解法才用了双指针的解法,双指针是编程中的常用思路,需要多注意,而且要注意使用while循环,不要上来就用for循环。
 该题思路是,采用一个前指针始终停在为第一个零的位置,另一个指针往下寻找,碰到不为零的数即交换。

class Solution {
    public void moveZeroes(int[] nums) {
        int n = nums.length;
        int a=0,b=0;
        while(b<n){
            if(nums[b] != 0){
                swap(nums,a,b);
                a++;
            }
            b++;
        }
    }
    public void swap(int nums[],int a,int b){
        int temp;
        temp = nums[a];
        nums[a] = nums[b];
        nums[b] = temp;         
    }
}

&esmp;有一种简单方法是对数组进行遍历,将所有的非零元素提前,再在后面进行补零的操作。

class Solution {
    public void moveZeroes(int[] nums) {
        int n = nums.length;
        int count = 0;
        for(int i = 0;i<n;i++){
            if(nums[i] != 0){
                nums[count] = nums[i];
                count++;
            }
        }
        while(count<n){
            nums[count] = 0;
            count++;
        }
    }
    
}

leetcode 第27题

 因为上面的题,做到该题时第一反应也是双指针算法,所以自己采用了如上题的双指针。思想还是采用了都是从开头出发的两个指针,这里看到up主的解题思路则是设置了分别从开头和末尾开始的两个指针。

class Solution {
    public int removeElement(int[] nums, int val) {
        int n = nums.length;
        int a=0,b=0;
        while(b<n){
            if(nums[b] != val){
                swap(nums,a,b);
                a++;
            }
            b++;
        }
        return a;
    }
    public void swap(int[] nums,int a,int b){
        int temp;
        temp = nums[b];
        nums[b] = nums[a];
        nums[a] = temp;
    }
}

 自己根据双指针的思路编写程序发现,存在着错误,数组输出不完全。

class Solution {
    public int removeElement(int[] nums, int val) {
        int n = nums.length;
        int a=0,b=n-1;
        while(a<b){
            if(nums[a] == val){
                if(nums[b] != val){
                    swap(nums,a,b);
                }
                b--;
            }
            a++;
        }
        return a+1;
    }
    public void swap(int[] nums,int a,int b){
        int temp;
        temp = nums[b];
        nums[b] = nums[a];
        nums[a] = temp;
    }
}

 通过学习了解到,因为判断条件是两指针相遇,但两指针相遇的位置有两种情况,一是都停留在val上,另一种则相反,所以需要在return时增加判断条件。提交答案发现前面给出的解答还存在着当首位和末尾都是val的时候,不进行交换就错过了。

class Solution {
    public int removeElement(int[] nums, int val) {
        int n = nums.length;
        if(nums.length == 0){
            return 0;
        }
        int a=0,b=n-1;
        while(a<b){
            if(nums[a] == val){
                if(nums[b] != val){
                    swap(nums,a,b);
                }
                b--;
            }else{
                a++;
            }
            
        }
        if(nums[a] == val){
            return a;
        }else{
            return a+1;
        }
    }
    public void swap(int[] nums,int a,int b){
        int temp;
        temp = nums[b];
        nums[b] = nums[a];
        nums[a] = temp;
    }
}

 up采用了两个while循环表示两个指针。

class Solution {
    public int removeElement(int[] nums, int val) {
        int n = nums.length;
        if(nums.length == 0){
            return 0;
        }
        int a=0,b=n-1;
        while(a<b){
            while((a<b)&&(nums[a] != val)){
                a++;
            }
            while((a<b)&&(nums[b] == val)){
                b--;
            }
            swap(nums,a,b);
            
        }
        if(nums[a] == val){
            return a;
        }else{
            return a+1;
        }
    }
    public void swap(int[] nums,int a,int b){
        int temp;
        temp = nums[b];
        nums[b] = nums[a];
        nums[a] = temp;
    }
}

 总结:本该一天完成的东西拖拖拉拉到两天,也深刻体会到自己能力的不足,继续加油吧。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值