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 ...
随机推荐
- BZOJ-2257 瓶子和燃料 分解因数+数论方面乱搞(裴蜀定理)
一开始真没想出解法...后来发现那么水.... 2257: [Jsoi2009]瓶子和燃料 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 970 So ...
- href="javascript:void(0)"
javascript:是伪协议,表示url的内容通过javascript执行.void(0)表示不作任何操作,这样会防止链接跳转到其他页面.这么做往往是为了保留链接的样式,但不让链接执行实际操作,具体 ...
- HDU 1285 确定比赛名次
传送门 确定比赛名次 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HD 2177(威佐夫博弈 入门)
取(2堆)石子游戏 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- ASP.NET MVC ActionFilterAttribute的执行顺序
http://diaosbook.com/Post/2014/6/3/execution-order-of-actionfilter-aspnet-mvc ASP.NET MVC里面我们要自定义Act ...
- redis.conf
redis示例配置文件 分类: redis2013-10-22 16:39 427人阅读 评论(0) 收藏 举报 转载自https://raw.github.com/antirez/redis/2.6 ...
- IDEA 从SVN检出项目相关配置
1.新建好一个工程,然后通过SVN检出项目 2.检出后一般tomcat的环境是配置好的,点击上方Project Structure按钮,弹出窗体,查看Project项,一般没问题,如果要配置就配置Pr ...
- LINQ to SQL更新数据库操作(转载)
使用LINQ to SQL建模Northwind数据库 在这之前一起学过LINQ to SQL设计器的使用,下面就使用如下的数据模型: 当使用LINQ to SQL设计器设计以上定义的五个类(Prod ...
- C# 排序算法记录
class Program { static void Main(string[] args) { , , , , , , , , -, , , }; //假设一个最小的值 ]; ; i < a ...
- ios中的几种多线程实现
iOS 支持多个层次的多线程编程,层次越高的抽象程度越高,使用起来也越方便,也是苹果最推荐使用的方法.下面根据抽象层次从低到高依次列出iOS所支持的多线程编程范式:1, Thread;2, Cocoa ...