0026_Remove Duplicates from Sorted Array
来源:互联网 发布:js原型链 编辑:程序博客网 时间:2024/05/20 21:44
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.
JAVA
题中要求将不重复的数字挪到数组的前边,并返回不重复的数字部分的长度result,也就是说程序运行之后,nums数组的前result位都要是不重复的数字,但是没要求有序。第一次只返回了result,并没有改变数组中的数据,导致WA,调整后顺利AC,但是效率排在后1/3。。。但是看了下排名靠前的代码,基本想法都是一样的,没分析出来他们快在哪里,还需要再看看。。
public class Solution { public int removeDuplicates(int[] nums) { if(nums.length == 0){ return 0; } int result = 1; int targetNum = nums[0]; int index = 1; int replaceIndex = 1; while(index < nums.length){ if(nums[index] != targetNum){ ++result; targetNum = nums[index]; nums[replaceIndex] = nums[index]; ++replaceIndex; } ++index; } return result; }}
阅读全文
0 0
- 0026_Remove Duplicates from Sorted Array
- LeetCode 26_Remove Duplicates from Sorted Array
- leetcode第一刷_Remove Duplicates from Sorted Array II
- leetcode第一刷_Remove Duplicates from Sorted List II
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates From Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Centos6.x 部署vsFTP
- eclipse指定启动的jdk
- Spring面试题
- 学习笔记--Linux 常用命令一,目录操作
- sqlserver序列号
- 0026_Remove Duplicates from Sorted Array
- 浅析openstack虚拟化技术
- JAVA开发环境
- 堆和栈的区别
- C#连接数据库,实现从数据库中取值并判断其大小(实现插入一条销售记录并更新库存的相应的内容)
- Dijkstra算法的实现及原理
- POJ 1696 Space Ant(极角排序)
- javascript 高级自动化构建工具Gulp
- 通过高通平台简单总结的权限问题