栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【LeetCode】No.26. Remove Duplicates from Sorted Array -- Java Version

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【LeetCode】No.26. Remove Duplicates from Sorted Array -- Java Version

题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/

1. 题目介绍(移除排序数组中的重复元素)

Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same.

【Translate】: 给定一个按非递减顺序排序的整数数组nums,就地删除重复的元素,使每个惟一元素只出现一次。元素的相对顺序应该保持不变。

Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.

【Translate】: 由于在某些语言中不可能更改数组的长度,因此必须将结果放在数组nums的第一部分。更正式地说,如果在删除重复项之后还有k个元素,那么nums的前k个元素应该保存最终结果。它与前k个元素以外的元素无关。

Return k after placing the final result in the first k slots of nums.

【Translate】: 将最终结果放入nums的前k个槽后返回k。

Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.

【Translate】: 不要为其他数组分配额外的空间。为此,必须使用O(1)额外内存来修改输入数组。

【Custom Judge】:

【测试用例】:

【约束】:

2. 题解 2.1 nicer loops – O(n)

  StefanPochmann所提供的题解 5 lines C++/Java, nicer loops 。不多解释,就是很简单。

public int removeDuplicates(int[] nums) {
    int i = nums.length > 0 ? 1 : 0;
    for (int n : nums)
        if (n > nums[i-1])
            nums[i++] = n;
    return i;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/855464.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号