Integer Array Ladder questions
1.这个题不难,关键在于把题目意思理解好了。这个题问的不清楚。要求return new length,很容易晕掉。
其实就是return 有多少个单独的数。
import java.util.Arrays; /*
* Question: 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.
*/
public class RemoveDuplicatesfromSortedArray {
public static void main(String[] args){
int[] nums = new int[]{1,1,2};
removeDuplicates(nums);
}
public static int removeDuplicates(int[] nums){
if(nums == null || nums.length == 0){
return 0;
}
int index = 1;
for(int i=1 ;i<nums.length;i++){
if(nums[i] != nums[i-1]){
index ++;
}
}
nums = Arrays.copyOf(nums, index); return index;
}
public static int removeDuplicatesWorking(int[] nums){ return 0;
}
}
Integer Array Ladder questions的更多相关文章
- zenefits oa - sort integer array in lexographical order
[ 12 | 2434 | 23 | 1 | 654 | 222 | 56 | 100000 ] Then the output should be: [ 1 | 100000 | 12 | 222 ...
- Equal Sides Of An Array
参考:http://stackoverflow.com/questions/34584416/nested-loops-with-arrays You are going to be given an ...
- array题目合集
414. Third Maximum Number 给一个非空的整数数组,找到这个数组中第三大的值,如果不存在,那么返回最大的值.要求时间复杂度为o(n) 例如: Example 1: Input: ...
- [LeetCode] 330. Patching Array 数组补丁
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...
- [LeetCode] 805. Split Array With Same Average 用相同均值拆分数组
In a given integer array A, we must move every element of A to either list B or list C. (B and C ini ...
- LeetCode面试常见100题( TOP 100 Liked Questions)
LeetCode面试常见100题( TOP 100 Liked Questions) 置顶 2018年07月16日 11:25:22 lanyu_01 阅读数 9704更多 分类专栏: 面试编程题真题 ...
- [LeetCode] Minimum Moves to Equal Array Elements II 最少移动次数使数组元素相等之二
Given a non-empty integer array, find the minimum number of moves required to make all array element ...
- [LeetCode] Minimum Moves to Equal Array Elements 最少移动次数使数组元素相等
Given a non-empty integer array of size n, find the minimum number of moves required to make all arr ...
- [LeetCode] Patching Array 补丁数组
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...
随机推荐
- Permanent Space 和 Heap Space
JVM堆内存 JVM堆内存分为2块:Permanent Space 和 Heap Space. Permanent 即 持久代(Permanent Generation),主要存放的是Java类定 ...
- android 开发 View _2_ View的属性动画ObjectAnimator ,动画效果一览
支持:https://www.cnblogs.com/whoislcj/p/5738478.html translationX的效果: protected void onCreate(Bundle s ...
- Maven CXF wsdl2Java 给指定名空间设置包名
<plugin> <groupId>org.apache.cxf</groupId> <artifactId>cxf-codegen-plugin< ...
- IDEA导入Eclipse项目
目录 一.导入项目 二.启动项目 一.导入项目 1.欢迎界面,选择Import Project 2.选择源码的位置,点击OK 3.选择Eclipse模型,点击Next 4.默认选择,点击Next 5. ...
- Halcon除法
今天,用到了Halcon 的除法.求出两个region的面积,area1,area2.我想求出它们的比值area1/area2.但是发现比值是整数,没有保留小数.应该改为这样area1/real(ar ...
- spring boot报Unsupported Media Type Content type '*/*;charset=UTF-8' not supported
1.请求设置Content-Type:application/json即可 ajax一般默认:Content-Type: application/x-www-form-urlencoded;chars ...
- delphi图片欣赏
作者QQ:276678295
- 在delphi中XLSReadWriteII.组件的应用实例(2)
第三方组件:XLSReadWriteII.v.5.20.67_XE3 实例源码如下: unit Unit1; interface uses Winapi.Windows, Winapi.Messa ...
- Java ArrayList调用构造方法传入"容量size"不生效,如何初始化List容量size
创建一个ArrayList对象,传入整型参数 @Test public void arrayListConstructor(){ ArrayList<Object> objects = n ...
- 如何使用Shiro
一.架构 要学习如何使用Shiro必须先从它的架构谈起,作为一款安全框架Shiro的设计相当精妙.Shiro的应用不依赖任何容器,它也可以在JavaSE下使用.但是最常用的环境还是JavaEE.下面以 ...