基础语法
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上提交,所以写的随意了一点。一开始就定义了数组和需要的条
件,也没有写返回值啥的。有些题考虑到了边界条件但也没有写上去,总之就是非常随意,请各位看的带哥别介。