zoukankan      html  css  js  c++  java
  • 【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 }
  • 相关阅读:
    HDU 1061
    HDU 1028
    HDU 2191
    POJ 2249
    html.day01
    移动Web开发技巧
    chrome浏览的下载扩展程序
    webApp禁止用户保存图像
    弹出层easydialog-v2.0
    免写前缀JS包--prefixfree.min.js--插件
  • 原文地址:https://www.cnblogs.com/godlei/p/5642172.html
Copyright © 2011-2022 走看看