(JAVA)Array基础知识+LeetCode27 283 485

基础语法

import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;

public class ArrayTest {
    public static void main(String[] args) {
        //4 ways to create a array in java
        int[] a = {1,2,3};
        int[] b = new int[]{1,2,3};
        int[] c = new int[3];
        ArrayList<Integer> arr = new ArrayList<>();
        for(int i = 0; i < 3; i++)
        {
            arr.add(i+1);
        }
        System.out.println(arr);
        //[1, 2, 3]
        
        //add element
        arr.add(99);
        System.out.println(arr);
        //[1, 2, 3, 99]
        
        arr.add(3, 88);
        System.out.println(arr);
        //[1, 2, 3, 88, 99]
        
        //access element
        int arr1 = arr.get(1);
        System.out.println(arr1);
        
        //update 
        arr.set(1, 11);
        System.out.println(arr.get(1));
        
        //remove
        arr.remove(1);
        System.out.println(arr.get(1));
        System.out.println(arr);
        
        int length = arr.size();
        //c.length
        System.out.println(length);
        //iterate
        for(int i = 0; i < arr.size(); i++)
        {
            System.out.println(arr.get(i));
        }
        
        //Find the element 99 in c
        for(int i = 0; i < c.length; i++)
        {
            if(c[i] == 99){
                System.out.println(i);
            }
        }
        //Find the element 99 in arr
        boolean is99 = arr.contains(99);
        //we can use this bool to judge whether we find 99
        System.out.println(is99);
        

        //sort array c
        Arrays.sort(c);
        //sort array arr and reverse
        Collections.sort(arr, Collections.reverseOrder());
    }
}

LeetCode 27 —— Remove Element


//Given an array nums and a value val, remove all instances of 
//that value in-place and return the new length.

//Do not allocate extra space for another array, 
//you must do this by modifying the input array in-place with O(1) extra memory.

//The order of elements can be changed. 
//It doesn't matter what you leave beyond the new length.
class Array27 {
    public static void main(String[] args) {
        int[] nums = new int[]{0,1,2,2,3,0,4,2};
        int val = 2;
        
        int i = 0;
        int j = nums.length - 1;
        if(nums.length == 0)
        {
            System.out.println("Array nums is empty!");
        }
        else
        {
            while(i < j)
            {
                while(nums[i] != val && i < j)
                    i++;
                while(nums[j] == val && i < j)
                    j--;
                int temp = nums[i];
                nums[i] = nums[j];
                nums[j] = temp;
            }
        }
        for(int m = 0; m < nums.length; m ++)
        {
            System.out.println(nums[m]);
        }
        if(nums[i] == val)
            System.out.println("The number of element is " + i);
        else
            System.out.println("The number of element is " + i+1);
        }
}
   

这题用的是双指针(?类似),分别从前往后和从后往前,找到等于val和不等于val的就交换,直到两个指针相遇。

LeetCode 283 —— Move Zeroes

//Given an array nums, write a function to 
//move all 0's to the end of it while maintaining the relative order of the non-zero elements.

//You must do this in-place without making a copy of the array.
//Minimize the total number of operations.

public class Array283 {
    public static void main(String[] args) {
        int[] nums = new int[]{0,1,0,3,12};
        int temp = 0;
    //  let the nonzero element move to front of this array 
        for(int i = 0; i < nums.length; i++)
        {
            if(nums[i] != 0)
            {
                nums[temp] = nums[i];
                temp++;
            }
        }
    //  set the rest element to zero 
        for(int j = temp; j < nums.length; j++)
        {
            nums[j] = 0;
        }
        for(int t = 0; t < nums.length; t++)
        {
            System.out.println(nums[t]);
        }
    }
}

这题题目虽然是移动零,但却是通过找到非零元素然后移动到前端,再把剩下的元素归零的操作。

LeetCode 485 —— Max Consecutive Ones

// 485. Max Consecutive Ones
// Given a binary array, find the maximum number of 
// consecutive 1s in this array.
// Return the maximum number
public class Array485{
    public static void main(String[] args) {
        int[] arr = new int[]{1,1,0,1,1,1,1,1,0,1,1,0,1};
        int nowIndex = 0;
        int maxIndex = 0;
        for(int i = 0; i < arr.length; i++)
        {
            if(arr[i] == 1)
            {
                nowIndex += 1;
            }
            else if(arr[i] != 1)
            {
                if(nowIndex > maxIndex)
                {
                    maxIndex = nowIndex;
                }
                nowIndex = 0;
            }
        }    
        System.out.println(maxIndex);
    }
}

不要忘记比较nowIndex和maxIndex

最后:

	因为是自己写来练习的,也没有在LeetCode上提交,所以写的随意了一点。一开始就定义了数组和需要的条
件,也没有写返回值啥的。有些题考虑到了边界条件但也没有写上去,总之就是非常随意,请各位看的带哥别介。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值