leetcodeRemove Duplicates from Sorted Array(easy) /java

我决定先刷easy。

这道题的诡异之处在于,不仅你得输出长度,你还得更改nums[]数组,把冗余的数清掉。比如

import java.io.*;
import java.util.*;

public class Solution {
    public static int removeDuplicates(int[] nums) {
        int r=0;
        int len=nums.length;
        int i=0,j=0;
        if(len<2)
            return 0;
        int cmp=nums[0];
        int c=0;
        nums[j]=nums[0];
        j++;
        for(i=1;i<len;i++)
        {
            if(cmp!=nums[i])
            {
                cmp=nums[i];
                nums[j]=nums[i];
                j++;
            }
            else
            {
                c++;
            }
        }
        r=len-c;
        return r;
    }
    public static void main(String[] args)
    {
        int[] a={1,1,1,1,4,5};
        System.out.println(removeDuplicates(a));
        int[] b={1,1,1,5};
        System.out.println(removeDuplicates(b));
    }
}

还算比较简单的一道题。

时间: 2024-11-08 18:54:26

leetcodeRemove Duplicates from Sorted Array(easy) /java的相关文章

[LeetCode] 026. Remove Duplicates from Sorted Array (Easy) (C++/Java)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 026. Remove Duplicates from Sorted Array (Easy) 链接: 题目:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/ 代码(github):https://github.com/ill

leetcode_26_ Remove Duplicates from Sorted Array (easy)

Remove Duplicates from Sorted Array 题目: 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 in place with constant mem

leetcode 80 Remove Duplicates from Sorted Array II ----- java

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 1, 1, 2, 2 and 3. It doesn't

Remove Duplicates From Sorted Array leetcode java

算法描述: 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 in place with constant memory. For example,Given input array

Leetcode 26. Remove Duplicates from Sorted Array (easy)

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. Exam

leetcode-Remove Duplicates from Sorted Array II-80

输入非递减数组,要求每个元素最多重复两次,求最后剩下的数组和元素个数 这题函数的返回值是元素个数,但是还需要把输入的参数也就是输入的数组也改动成合法的 因为是有序的序列,所以直接遍历一遍,用cur保存元素,cnt保存这个元素出现的次数就好,ON 1 class Solution { 2 public: 3 int removeDuplicates(vector<int>& nums) { 4 int ans=0; 5 if(nums.size()==0) return ans; 6 i

leetCode-数组:Remove Duplicates from Sorted Array

Remove Duplicates from Sorted Array:从排列后的数组中删除重复元素 考察数组的基本操作: class Solution { public int removeDuplicates(int[] nums) { if (nums==null || nums.length==0) return 0; int index = 1; for(int i =1; i<nums.length; i++){ if(nums[i]!=nums[i-1]){ nums[index]

26. Remove Duplicates from Sorted Array【easy】

26. Remove Duplicates from Sorted Array[easy] 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 in place with consta

【LeetCode-面试算法经典-Java实现】【026-Remove Duplicates from Sorted Array(删除排序数组中的重复元素)】

[026-Remove Duplicates from Sorted Array(删除排序数组中的重复元素)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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 anot