zoukankan      html  css  js  c++  java
  • 数组问题

    LeetCode 283

    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.

    For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0]

    给定一个数组,写一个函数,将数组中的所有0挪到数组的末尾,而维持其他所有非0元素的相对位置.举例 input:[0,1,0,3,12] output:[1,3,12,0,0]

    代码

    方法一:分别使用两个变量来指向元素,sizet用来指向大于1的元素,i用来遍历元素,当元素全部遍历完,将count指向的元素之后全部赋值为0即可

       public static void moveZeroes(int[] nums) {
            int size = 0;
            for (int i = 0; i < nums.length; i++) {
                if (nums[i] > 0) {
                    nums[size] = nums[i];
                    size++;
                }
            }
            for (int i = size; i < nums.length; i++) {
                nums[i] = 0;
            }
        }  

    方法二:使用两个变量,count用来指向第一个为0的元素,也就是说count前的元素都是大于0的,i用来遍历元素,如果遍历到大于0的元素就和count所指向的元素交换位置

       public static void moveZeroes3(int[] nums) {
            int k = 0;
            for (int i = 0; i < nums.length; i++) {
                if (nums[i] > 0) {
                    if (i != k) {   //如果数组中没有0,就不需要交换
                        swap(nums, i, k++);
                    } else {
                        k++;
                    }
                }
            }
        }
    
        public static void swap(int[] arr, int i, int j) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    

      

    LeetCode 27

    Given an array and a value, 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.

    Example:

    Given nums = [3,2,2,3], val = 3,
    
    Your function should return length = 2, with the first two elements of nums being 2.

    给定一个数组nums和一个数值val,将数组中所有等于val的元素删除,并返回剩余的元素个数

    代码

       public static int removeElement(int[] nums, int val) {
            int k = 0;
            for (int i = 0; i < nums.length; i++) {
                if (nums[i] != val) {
                    if (i != k) {  //如果数组中没有val,就不需要交换
                        swap(nums, i, k++);
                    } else {
                        k++;
                    }
                }
            }
            return k;
        }
    
        public static void swap(int[] arr, int i, int j) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }

    LeetCode26

    Given a sorted array, remove the duplicates in-place such that each element appear only once 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.

    Example:

    Given nums = [1,1,2],
    
    Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
    It doesn't matter what you leave beyond the new length.
    给定一个有序数组,对数组中的元素去重,使得原数组的每个元素只有一个.返回去重后的数组的长度值

    代码

       public static int removeDuplicates(int[] nums) {
            int k = 0;
            for (int i = 0; i < nums.length; i++) {
                if (nums[k] != nums[i]) {
                    nums[++k] = nums[i];
                }
            }
            return k + 1;
        }
    

      

    LeetCode 80

    Follow up for "Remove Duplicates":
    What if duplicates are allowed at most twice?

    For example,
    Given sorted array nums = [1,1,1,2,2,3],

    Your function should return length = 5, with the first five elements of nums being 1122 and 3. It doesn't matter what you leave beyond the new length.

    代码

       public static int removeDuplicates(int[] nums) { //1,1,1,2,2,3,3,4,4,4,5
            int k = 0;
            for (int i = 0; i < nums.length; i++) {
                if (k < 2 || nums[i] > nums[k - 2]) {
                    nums[k] = nums[i];
                    k++;
                }
            }
            return k;
        }
  • 相关阅读:
    用java批量重命名文件
    登入Tomcat Manager时出现401错误——解决方法
    bat启动/停止oracle服务
    java基本类型与字符串之间的转换
    java面向对象 要点
    理解main 方法
    第二章 变量,数据类型和运算符
    初始JAVA
    jdbcTemplate
    Aspectj
  • 原文地址:https://www.cnblogs.com/Hangtutu/p/8157559.html
Copyright © 2011-2022 走看看