Java for LeetCode 026 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.
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.
解题思路:
看到题目的例子,感觉非常简单,不就返回一个int么?Too Simple,提交后才发现,nums[]的值也得跟着变,不过加行代码就行了,JAVA实现如下:
public int removeDuplicates(int[] nums) {
if(nums.length==0)
return 0;
int result=1,c=nums[0];
for(int i=0;i<nums.length;i++){
if(nums[i]>c){
c=nums[i];
result++;
nums[result-1]=c;
}
}
return result;
}
Java for LeetCode 026 Remove Duplicates from Sorted Array的更多相关文章
- LeetCode 026 Remove Duplicates from Sorted Array
题目描述:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such t ...
- Java for LeetCode 080 Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For examp ...
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>
LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)
https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...
- LeetCode OJ Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)
[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
随机推荐
- 【Gym 100610A】Alien Communication Masterclass
题 Andrea is a famous science fiction writer, who runs masterclasses for her beloved readers. The mos ...
- 在excel批量更改单元格类型后的批量刷新显示
把E的东西变成完整显示 解决办法: 选中所需要更改的整列数据------>菜单栏的数据选项------>分列
- (转)google Java编程风格中文版
转:http://www.hawstein.com/posts/google-java-style.html 目录 前言 源文件基础 源文件结构 格式 命名约定 编程实践 Javadoc 后记 前言 ...
- 多线程java代码移植到android&下载文本界面的更新
1)效果演示:
- Spring学习8-SSH+Log4j黄金整合
最下面有log4j的详解及配置步骤 步骤一.导入相应的jar包(具体参看下一篇博文) 步骤二.修改WEB.XML文件,内容如下: <?xml version="1.0" en ...
- 找出进程中各线程cpu消耗情况
以root用户执行以下命令,以PID 5423举例: 1,根据top命令,找到占用CPU高的进程,找到PID PID USER PR NI VIRT RES SHR S %CPU ...
- vc++ 6.0下Glut的配置 及 Glut 框架介绍
2014-04-08 16:18:30 一.配置Glut 学习来源: http://blog.sina.com.cn/s/blog_5f0cf7bd0100c9oa.html 亲测可行. Glut的 ...
- xss绕过过滤之方法
很多网站为了避免XSS的攻击,对用户的输入都采取了过滤,最常见的就是对<>转换成<以及>,经过转换以后<>虽然可在正确显示在页面上,但是已经不能构成代码语句了.这个 ...
- [Angularjs]ng-file-upload上传文件
写在前面 最近在弄文档库的H5版,就查找了下相关的上传组件,发现了ng-upload的东东,推荐给大家. 系列文章 [Angularjs]ng-select和ng-options [Angularjs ...
- Android开源项目第二篇——工具库篇
本文为那些不错的Android开源项目第二篇——开发工具库篇,**主要介绍常用的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容 ...