Remove Duplicates From Sorted Array leetcode java
算法描述:
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.
算法分析:一个有序数组,出去其中出现次数大于1的数,返回剩余元素个数
代码:设置两个指针,i 遍历原数组,j 是去除重复后的下标
public int removeDuplicates(int[] nums) {
if(nums == null || nums.length == 0)
return 0;
int len = nums.length;
int j = 1; //数组第一个元素是不需要变的,所以长度至少为1
for(int i = 1; i < len; i++){ //数组遍历从下标1开始
if(nums[i] == nums[i - 1])
continue;
else {
nums[j++] = nums[i];
}
} return j;
}
Remove Duplicates From Sorted Array leetcode java的更多相关文章
- Remove Duplicates from Sorted Array [LeetCode]
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- Remove Duplicates from Sorted List leetcode java
题目: Given a sorted linked list, delete all duplicates such that each element appear only once. For e ...
- Remove Duplicates From Sorted Array
Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...
- 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] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
随机推荐
- Docker 使用Dockerfile构建redis镜像
Dockerfile实现: FROM centos: MAINTAINER hongdada "hongdaqi159505@gmail.com" WORKDIR /home RU ...
- 【做题】HDU6331 Walking Plan——矩阵&分块
题意:给出一个有\(n\)个结点的有向图,边有边权.有\(q\)组询问,每次给出\(s,t,k\),问从\(s\)到\(t\)至少经过\(k\)条边的最短路. \(n \leq 50, \, q \l ...
- wqCms6.0在IIS6的Getshell
2017-02-15发布 一.漏洞利用点 漏洞文件:admin_UploadDataHandler.ashx 自定义构造上传点 二.hack it 三.POC <html> <bod ...
- html页面使用前端框架布局时,弹出子页面等情况若出现布局混乱,可能是回发导致
解决方法:需要带调用 弹框 那里 写一段return false防止回发
- function CONVERSION_EXIT ****INPUT/OUTPUT说明
CONVERSION_EXIT_ALPHA_INPUT和CONVERSION_EXIT_ALPHA_OUTPUT 函数说明 CONVERSION_EXIT_MATN1_INPUT 物料号码转换函数 C ...
- mac终端不好用?用brew神器代替
一.概念 Brew是一款Mac OS平台下的软件包管理工具,拥有安装.卸载.更新.查看.搜索等很多实用的功能.简单的一条指令,就可以实现包管理,而不用你关心各种依赖和文件路径的情况,十分方便快捷. 官 ...
- 负数字符串经过int处理之后还是负数
<?php $v = '-1'; $b = (int)$v; echo $b;
- 学习笔记42—Win7下安装Linux双系统
1.下载Linux镜像:http://mirrors.163.com/ubuntu-releases/18.04.1/ 方法一: 1.用软通牒软件将Linux的镜像写入空的优盘中, 具体如下: 1) ...
- 学习笔记17—circos安装集(window环境)
Windows7环境下Circos使用教程 一.下载安装软件包 1.strawberry perl 因为Circos软件是依赖perl语言编译环境的,但是windows环境下默认是没有perl的,所以 ...
- json文件不能有注释
之前一直忽视了这个问题,直接导致taiga-front的部署的时候不能通过其他IP访问. 如图: 首先是提示 app-loader.js:1 Your conf.json file is not a ...