The flexible test of array

Keywords: Programming Java

The following is a summary of my own array programming problems

LeetCode

66. Add a question

import java.util.Arrays;
class Solution66 {
    public static void main(String[] args){
        int[] digits={9,8,7,6,5,4,3,2,1,0};
        System.out.println(Arrays.toString(plusOne(digits)));
    }
    public static int[] plusOne(int[] digits) {
        int carry=1;
        for(int i=digits.length-1;i>=0;i--){
            int num=digits[i]+carry;
            digits[i]=num%10;
            carry=num/10;
            if(carry==0){
                break;
            }
        }
        if(carry==1){   //It can only be said that there must be all 9 in the array
            int[] arr=new int[digits.length+1];
            arr[0]=1;
            return arr;
        }
        return digits;

    }
}

169. Most elements

class Solution169 {
    public int majorityElement(int[] nums) {
        //Do not use sorting to solve in O(n)
        int m=nums[0];
        int count=1;
        for(int i=1;i<nums.length;i++){
            if(nums[i]==m){
                count++;
            }else{
                count--;
                if(count==0){
                    m=nums[i];
                    count=1;
                }
            }
        }
        return m;

        /*
        Arrays.sort(nums);  //O(n*logn)
        return nums[nums.length/2];
        */
        /*
        Arrays.sort(nums);
        int m=0;
        int mcount=0;
        for(int i=0;i<nums.length;){
            if(i>nums.length/2){
                break;
            }
            int count=1;
            for(int j=i+1;j<nums.length;j++){
                if(nums[j]==nums[i]){
                    count++;
                }else{
                    break;
                }
            }
            if(count>mcount){
                mcount=count;
                m=nums[i];
            }
            i+=count;
        }
        return m;
        */
    }
}

209. Subarray with the smallest length

class Solution209 {
    public int minSubArrayLen(int s, int[] nums) {
        int len=0;
        int i=0;
        int sum=0;
        for(int j=0;j<nums.length;j++){
            sum+=nums[j];
            while(sum>=s){
                len=len==0?(j-i+1):Math.min(len,j-i+1);
                sum-=nums[i];
                i++;
            }
        }
        return len;
    }
}

283. Move zero

class Solution283 {
    public void moveZeroes(int[] nums) {
        int k=0;
        int temp=0;
        for(int i=0;i<nums.length;i++){
            if(nums[i]!=0){
                temp=nums[i];
                nums[i]=nums[k];
                nums[k]=temp;
                k++;
            }
        }
    }
}

674. Longest continuous increasing sequence

class Solution674 {
    public static void main(String[] args){
        int[] nums={2,2,2,2};
        int len=findLengthOfLCIS(nums);
        System.out.println(len);
    }
    public static int findLengthOfLCIS(int[] nums) {
        if(nums.length==0||nums.length==1){
            return nums.length;
        }
        int maxL=0;
        int maxR=0;
        int l=0;
        int r=0;
        int maxCount=1;
        int count=1;
        for(int i=0;i<nums.length-1;i++){
            if(nums[i+1]>nums[i]){
                count++;
                r=i+1;
            }else{
                count=1;
                l=i+1;
                r=i+1;
            }
            if(count>maxCount){
                maxCount=count;
                maxL=l;
                maxR=r;
            }
        }
        System.out.println(maxL+"~"+maxR);
        return maxCount;
    }
}
/*class Solution {
    public int findLengthOfLCIS(int[] nums) {
        if(nums.length==0||nums.length==1){
            return nums.length;
        }
        int maxCount=1;
        int count=1;
        for(int i=0;i<nums.length-1;i++){
            if(nums[i+1]>nums[i]){
                count++;
            }else{
                count=1;
            }
            maxCount=count>maxCount?count:maxCount;
        }
        return maxCount;
    }
}*/

724. Find the central index of the array

class Solution724 {
    public int pivotIndex(int[] nums) {
        int sum=0;
        for(int num:nums){
            sum+=num;
        }
        int leftSum=0;
        int rightSum=0;
        for(int i=0;i<nums.length;i++){
            if(i==0){
                leftSum=0;
            }else{
                leftSum+=nums[i-1];
            }
            rightSum=sum-nums[i]-leftSum;
            if(rightSum==leftSum){
                return i;
            }
        }
        return -1;
    }
}

905. Sorting arrays by parity

class Solution905 {
    public int[] sortArrayByParity(int[] A) {
        int left=0;
        int right=A.length-1;
        while(left<right){
            if(A[left]%2==1&&A[right]%2==0){        
                int temp=A[left];
                A[left]=A[right];
                A[right]=temp;
            }else if(A[left]%2==0&&A[right]%2==1){  
                left++;
                right--;
            }else if(A[left]%2==0&&A[right]%2==0){
                left++;
            }else{
                right--;
            }
        }
        return A;
    }
}

1013. Divide the array into three equal parts

class Solution1013 {
    public boolean canThreePartsEqualSum(int[] A) {
        int sum=0;
        for(int num:A){
            sum+=num;
        }
        int key=sum/3;
        int group=0;
        for(int i=0;i<A.length;i++){
            key-=A[i];
            if(key==0){
                group++;
                key=sum/3;
            }
        }
        return group==3;
    }
}

1295. Even numbers

class Solution1295 {
    public int findNumbers(int[] nums) {
        int count=0;
        for(int i=0;i<nums.length;i++){
            if((nums[i]+"").length()%2==0){
                count++;
            }
        }
        return count;
    }
}

1313.

class Solution1313 {
    public int[] decompressRLElist(int[] nums) {
        int len=0;
        for(int i=0;i<nums.length;i+=2){
            len+=nums[i];
        }
        int[] arr=new int[len];
        int index=0;
        for(int i=1;i<nums.length;i+=2){
            for(int j=0;j<nums[i-1];j++){
                arr[index++]=nums[i];
            }
        }
        return arr;
    }
}

 

 

 

 

 

 

 

 

 

Published 10 original articles, won praise 0, visited 146
Private letter follow

Posted by niroshan on Sat, 22 Feb 2020 10:34:01 -0800