java算法(2)
来源:互联网 发布:sql语句给字段起别名 编辑:程序博客网 时间:2024/06/10 01:29
原题:
// 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.
答案:
public class Solution { public int removeDuplicates(int[] nums) { if(nums.length == 0 || nums == null) return 0; if(nums.length < 2) return nums.length; int index = 1; for(int i = 1; i < nums.length; i++) { if(nums[i] != nums[i - 1]) { nums[index++] = nums[i]; } } return index; }}
阅读全文
0 0
- 算法--java(2)
- java算法(2)
- Java 排序算法(2)
- 算法上机2(java)
- java(算法2)冒泡
- Java与算法(2)
- Java(8):基础算法(2)--冒泡排序算法 + 选择排序算法 + 插入排序算法
- 算法导论(一)2、算法基础-插入排序 java
- 均衡算法代码(java)(2)
- 算法2-快速选择(java实现)
- 快速排序算法2(java)
- java面试算法题(2)
- java经典算法2
- java小算法(2)
- java-递归算法2
- Java算法小结2
- 算法-java(2)
- Java-排序算法2
- java学习第31天,calendar转换
- java算法(1)
- 主从复制原理
- apply,call,bind的区别
- ReactNative基础(三)了解ScrollView并打造一个Banner效果
- java算法(2)
- 文章标题
- Rgb转换三原色
- 《程序员修炼之道—从小工到专家》注重实效的哲学
- 【网站建设】关于服务器跪掉了
- java算法(3)
- java笔记(二)static和final
- Node.js安装及环境配置之Windows篇
- CodeVS3657 括号序列 解题报告【区间DP】