LeetCode:26. Remove Duplicates from Sorted Array(Easy)

1. 原题链接

https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/

2. 题目要求

给定一个已经排序的整数数组nums[ ],返回除去重复元素后的数组长度

注意:不能重新创建一个数组,空间复杂度为O(1)

3. 解题思路

使用指针j来遍历数组,i用来计数。初始时,i指向nums[0],j指向nums[1]。

当nums[i] != nums[j]时,i++,且nums[i]=nums[j],从j所在元素位置继续比较。

最后返回 i+1

4. 代码实现

public class RemoveDuplicatesFromSortedArray26 {
    public static void main(String[] args) {
        int nums[] = {2, 2, 3, 4, 5, 5, 6};
        System.out.println(removeDuplicates(nums));
    }

    public static int removeDuplicates(int[] nums) {
        if (nums.length == 0) return 0;
        int i = 0;
        for (int j = 1; j < nums.length; j++) {
            if (nums[j] != nums[i]) { //不相等时i++
                i++;
                nums[i] = nums[j];
            }
        }

        return i + 1;
    }
}

 运行结果:

原文地址:https://www.cnblogs.com/huiAlex/p/8143070.html

时间: 2024-07-30 17:13:25

LeetCode:26. Remove Duplicates from Sorted Array(Easy)的相关文章

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

&lt;LeetCode OJ&gt; 26. Remove Duplicates from Sorted Array

26. Remove Duplicates from Sorted Array My Submissions Question Total Accepted: 104150 Total Submissions: 322188 Difficulty: Easy Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

[Leetcode][Python]26: Remove Duplicates from Sorted Array

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 26: Remove Duplicates from Sorted Arrayhttps://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the duplicates in place such that each element appear

leetcode_26题——Remove Duplicates from Sorted Array (string)

Remove Duplicates from Sorted Array Total Accepted: 57887 Total Submissions: 183534My Submissions Question Solution Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocat

【一天一道LeetCode】#26. Remove Duplicates from Sorted Array

一天一道LeetCode系列 (一)题目 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, G

【leetcode】26. 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 memory. 解题分析: 扫描一遍链表,用一个变量标记已找到的不重复的元

【LeetCode】26. 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 memory. For example,Given input array n

leetcode 之Remove Duplicates from Sorted Array(二)

描述    Follow up for ”Remove Duplicates”: What if duplicates are allowed at most twice?    For example, Given sorted array A = [1,1,1,2,2,3],    Your function should return length = 5, and A is now [1,1,2,2,3] 之前的想法是再加一个计数的变量就行了 int removeDeplicates1(

Leet Code OJ 26. Remove Duplicates from Sorted Array [Difficulty: 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 constant memory. For example, Given input array