LeetCode Remove Duplicates from Sorted Array
2015-06-24 15:27
162 查看
Description:
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.
Solution:
Sort the array, then skip the one whose value is the same as its former one.
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.
Solution:
Sort the array, then skip the one whose value is the same as its former one.
import java.util.Arrays; public class Solution { public int removeDuplicates(int[] nums) { if (nums.length == 0) return 0; Arrays.sort(nums); int len = 1, key = 1; int num = nums[0]; for (int i = 1; i < nums.length; i++) { if (nums[i] != num) { len++; num = nums[i]; nums[key++] = nums[i]; } } return len; } }
相关文章推荐
- 数据库接口开发
- finally不管有没有错都会运行 finally 块用于清除 try 块中分配的任何资源,以及运行任何即使在发生异常时也必须执行的代码
- Redis笔记
- mysql远程登录权限不足问题解决
- java解析xml字符串(用dom4j)
- EasyUI常用控件的禁用方法
- java导出excel方法 小记
- 【LeetCode】Binary Tree Level Order Traversal(层序遍历)
- Android文件缓存 Json文件的读写
- QTP中Test参数化和Action参数化区别
- 【性能诊断】十、性能问题综合分析(案例1,windbg、Network Monitor)
- C# 当double数值较大且小数位过多时转化成字符串并保留小数位
- About JDBC
- 5.2 测试计划和估算
- iOS7滑动返回
- 服务器硬件监控之Check_openmanage
- Android-->.9文件制作,简单粗暴(菜鸟都能秒懂)
- UVa 11577 - Letter Frequency
- 设计模式中类的关系
- (转)理解MySQL——索引与优化