LeetCode_26. Remove Duplicates from Sorted Array
时间:2019-09-19
本文章向大家介绍LeetCode_26. Remove Duplicates from Sorted Array,主要包括LeetCode_26. Remove Duplicates from Sorted Array使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
26. Remove Duplicates from Sorted Array
Easy
Given a sorted array nums, 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 by modifying the input array in-place with O(1) extra memory.
Example 1:
Given nums = [1,1,2], Your function should return length =2
, with the first two elements ofnums
being1
and2
respectively. It doesn't matter what you leave beyond the returned length.
Example 2:
Given nums = [0,0,1,1,1,2,2,3,3,4], Your function should return length =5
, with the first five elements ofnums
being modified to0
,1
,2
,3
, and4
respectively. It doesn't matter what values are set beyond the returned length.
Clarification:
Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
Internally you can think of this:
// nums is passed in by reference. (i.e., without making a copy) int len = removeDuplicates(nums); // any modification to nums in your function would be known by the caller. // using the length returned by your function, it prints the first len elements. for (int i = 0; i < len; i++) { print(nums[i]); }
package leetcode; public class RemoveDuplicatesFromSortedArray { @org.junit.Test public void test() { int[] nums1 = { 1, 1, 2 }; int[] nums2 = { 0, 0, 1, 1, 1, 2, 2, 3, 3, 4 }; System.out.println(removeDuplicates(nums1)); System.out.println(removeDuplicates(nums2)); } public int removeDuplicates(int[] nums) { if (nums.length == 0) { return 0; } int i = 0; for (int j = 1; j < nums.length; j++) { if (nums[j] != nums[i]) { i++; nums[i] = nums[j]; } } return i + 1; } }
原文地址:https://www.cnblogs.com/denggelin/p/11547139.html
- 【深度学习系列】CNN模型的可视化
- mongodb11天之屠龙宝刀(六)mapreduce:mongodb中mapreduce原理与操作案例
- 真实场景的虚拟视点合成(View Synthsis)详解
- mongodb11天之屠龙宝刀(七)functions: mongodb 执行 functions入门案例
- 真实场景的双目立体匹配(Stereo Matching)获取深度图详解
- mongodb11天之屠龙宝刀(八)聚合函数与管道:sql与mongodb聚合函数对比
- Java基础-day04-基础题
- OpenCV亚像素角点cornerSubPixel()源代码分析
- mongodb11天之屠龙宝刀(十) 备份 还原 导出 导入::CSV,JSON,BOSN,解决中文乱码
- 【Go 语言社区】 golang 算法课程 第一季 第2节 洗牌算法
- 一条update语句的优化探索(r9笔记第80天)
- OpenCV角点检测源代码分析(Harris和ShiTomasi角点)
- Java基础-day03-代码题
- mongodb11天之屠龙宝刀(九)js函数入门:MongoDB基于js的数据类型修改
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法