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

解题分析:

扫描一遍链表,用一个变量标记已找到的不重复的元素的长度len,每当找到不重复元素时,就让被扫描元素与变量len标记的元素交换位置即可

具体代码:

 1 public class Solution {
 2    public static int removeDuplicates(int[] nums) {
 3         if(nums.length<=1)
 4             return nums.length;
 5         int num =nums[0];
 6         int len =1;
 7         for(int i=1;i<nums.length;i++){
 8             if(num!=nums[i]){
 9                 num=nums[i];
10                 nums[len]=nums[i];
11                 len++;
12             }
13         }
14         return len;
15     }
16
17 }
时间: 2024-11-03 03:27:05

【leetcode】26. Remove Duplicates from Sorted Array的相关文章

【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】#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】83 - 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 nums 

【LeetCode】026. 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

&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】83 - Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once. For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3. Solution: 1 /** 2 * Definition for singly-linked list. 3 * st

【Leetcode】82. Remove Duplicates from Sorted List II

Question: Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Given 1->2->3->3->4->4->5, return 1->2->5. Given 1->1->1->2->3, retu

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],