博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Leetcode]26. Remove Duplicates from Sorted Array
阅读量:7099 次
发布时间:2019-06-28

本文共 943 字,大约阅读时间需要 3 分钟。

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 = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.

 

思路:

  (1)虽然能AC,但是不对的思路:如果找到重复的数,就将其设置成一个很大的数,最后再排序一下,返回新的长度。

1 class Solution { 2     public int removeDuplicates(int[] nums) { 3         if (nums.length<=1) 4             return nums.length; 5         int duplicate = 0; 6         for (int i=0;i

  (2)正常的思路:如果有重复的元素,就跳过,然后把后面元素的值赋在重复的位置上

1 class Solution { 2     public int removeDuplicates(int[] nums) { 3         if (nums.length<=1) 4             return nums.length; 5         int index = 0; 6         for (int i=1;i

 

转载于:https://www.cnblogs.com/David-Lin/p/7725828.html

你可能感兴趣的文章
使用 MEF 公开 Silverlight“.NET研究” MVVM 应用程序中的接口
查看>>
Random Sequence 2011ACM福州赛区网络赛
查看>>
node.js 文件操作
查看>>
异常不可用于逻辑处理
查看>>
小米手机如期而至
查看>>
STM32 RCC实验 MCO脚输出时钟波形
查看>>
Flash 最小化,帧速变慢的问题
查看>>
java对redis的基本操作(一)
查看>>
基因组印记
查看>>
34个漂亮的应用程序后台管理界面(系列三)
查看>>
double free or corruption (!prev): 0x080644c8 ***
查看>>
在VMware上搭建iPhone开发环境(转)
查看>>
MongoCola使用教程 1 - MongoDB的基本操作和聚合功能
查看>>
2012年3月份30个优秀的jquery插件集合 功能强大
查看>>
公共的Json操作C#类
查看>>
WebService如何调试及测试工具
查看>>
HDU-2091 水题
查看>>
【转】条件编译#ifdef的妙用详解_透彻
查看>>
jQuery.autocomplete 支持中文输入
查看>>
配置ubuntu的mac主题
查看>>