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

本文展示了Java中创建、访问、更新、删除数组元素的方法,并通过LeetCode题目27和283解释了双指针技巧和移动元素的策略。此外,还介绍了寻找连续1的最大数量问题。通过实例代码,深入理解了数组操作和算法应用。

基础语法

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上提交,所以写的随意了一点。一开始就定义了数组和需要的条
件,也没有写返回值啥的。有些题考虑到了边界条件但也没有写上去,总之就是非常随意,请各位看的带哥别介。
代码下载地址: https://pan.quark.cn/s/b4a8e0160cfc 齿轮与轴系零件在机械设备中扮演着至关重要的角色,它们负责实现动力传输、调整运动形态以及承受工作载荷等核心功能。 在机械工程的设计实践中,齿轮和轴系的设计是一项关键的技术任务,其内容涵盖了材料选用、构造规划、承载能力分析等多个技术层面。 下面将系统性地介绍《齿轮及轴系零件结构设计指导书》中的核心知识点。 一、齿轮设计1. 齿轮种类:依据齿廓轮廓的不同,齿轮可划分为直齿齿轮、斜齿轮以及人字齿轮等类别,各类齿轮均具有特定的性能特点与适用工况,能够满足多样化的工作环境与载荷需求。 2. 齿轮规格参数:模数大小、压力角数值、齿数数量、分度圆尺寸等是齿轮设计的基础数据,这些参数直接决定了齿轮的物理尺寸与运行性能。 3. 齿轮材质选用:齿轮材料的确定需综合评估其耐磨损性能、硬度水平以及韧性表现,常用的材料包括铸铁、钢材、铝合金等。 4. 齿轮强度验证:需进行齿面接触应力分析与齿根弯曲应力分析,以确保齿轮在实际运行过程中不会出现过度磨损或结构破坏。 5. 齿轮加工工艺:涉及切削加工、滚齿加工、剃齿加工、淬火处理等工艺流程,工艺方案的选择将直接影响齿轮的加工精度与使用寿命。 二、轴设计1. 轴的分类方式:依据轴在机械装置中的功能定位与受力特点,可将轴划分为心轴、转轴以及传动轴等类型。 2. 轴的材料选择:通常采用钢材作为轴的材料,例如碳素结构钢或合金结构钢,特殊需求时可选用不锈钢材料或轻质合金材料。 3. 轴的构造规划:需详细考虑轴的轴向长度、截面直径、键槽布置、轴承安装位置等要素,以满足轴的强度要求、刚度要求以及稳定性要求。 4. 轴的强度验证:需进行轴的扭转强度分析与弯曲强度分析,以防止轴在运行过程中发生塑性变形...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值