LeetCode:26. Remove Duplicates from Sorted Array(Easy)
1. 原题链接
https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/
2. 题目要求
给定一个已经排序的整数数组nums[ ],返回除去重复元素后的数组长度
注意:不能重新创建一个数组,空间复杂度为O(1)
3. 解题思路
使用指针j来遍历数组,i用来计数。初始时,i指向nums[0],j指向nums[1]。
当nums[i] != nums[j]时,i++,且nums[i]=nums[j],从j所在元素位置继续比较。
最后返回 i+1
4. 代码实现
public class RemoveDuplicatesFromSortedArray26 {
public static void main(String[] args) {
int nums[] = {2, 2, 3, 4, 5, 5, 6};
System.out.println(removeDuplicates(nums));
}
public static int removeDuplicates(int[] nums) {
if (nums.length == 0) return 0;
int i = 0;
for (int j = 1; j < nums.length; j++) {
if (nums[j] != nums[i]) { //不相等时i++
i++;
nums[i] = nums[j];
}
}
return i + 1;
}
}
运行结果:

LeetCode:26. Remove Duplicates from Sorted Array(Easy)的更多相关文章
- Leetcode No.26 Remove Duplicates from Sorted Array(c++实现)
1. 题目 1.1 英文题目 Given an integer array nums sorted in non-decreasing order, remove the duplicates in- ...
- 26. Remove Duplicates from Sorted Array【easy】
26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...
- [Leetcode][Python]26: Remove Duplicates from Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...
- 【leetcode】 26. Remove Duplicates from Sorted Array
@requires_authorization @author johnsondu @create_time 2015.7.22 18:58 @url [remove dublicates from ...
- LeetCode 26. Remove Duplicates from Sorted Array (从有序序列里移除重复项)
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- 【一天一道LeetCode】#26. Remove Duplicates from Sorted Array
一天一道LeetCode系列 (一)题目 Given a sorted array, remove the duplicates in place such that each element app ...
- 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...
- 【LeetCode】26. Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- LeetCode OJ 26. Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
随机推荐
- HASH JION AND NESTED JION
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/sssbbbryj/article/details/27795905 关于HASH_JION的原 ...
- ubuntu误删home目录
今天第一次写shell脚本,一不小心把home目录全给删除了. 解决方案: 先把手打上二十大板!!! [root@myshell ~]#mkdir /home/test01 / ...
- LA 3415 保守的老师
题目链接:https://vjudge.net/contest/161820#problem/E 题意: 有一些同学,要从中选出一些同学来,人数尽量多,但是,两两之间要满足至少一个条件(身高差> ...
- 转载 【MySql】Update批量更新与批量更新多条记录的不同值实现方法
批量更新 mysql更新语句很简单,更新一条数据的某个字段,一般这样写: UPDATE mytable SET myfield = 'value' WHERE other_field = 'other ...
- 由inline-block小例子引申出的一些问题,及IE6、IE7兼容性解决方案
使用场景分析: 常见的对块与块之间的横向排列处理 对同级所有元素使用display:inline-block; , 之后块与块直接会产生间隙问题 解决办法: 给父级设 font-size:0; 别高兴 ...
- nmon监控linux系统性能
Nmon是一款计算机性能系统监控工具,使用 Nmon 可以很轻松的监控系统的 CPU.内存.网络.硬盘.文件系统.NFS.高耗进程.资源等信息.[简单方便] Nmon 安装 到https://sour ...
- 使用终端命令行将本地项目上传到Github并提交代码
第一步: 在Github上创建自己的repository 第二步:建立本地仓库cd到你的本地项目根目录下,执行git命令 1:$ cd 到你的项目目录下 2:$ git init 第三步:将本地项目工 ...
- 对AFNetworking的二次封装
HttpTool.h #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> typedef void(^HttpS ...
- NEC 框架规范 css reset
/* reset */html,body,h1,h2,h3,h4,h5,h6,div,dl,dt,dd,ul,ol,li,p,blockquote,pre,hr,figure,table,captio ...
- JSONP--解决ajax跨域问题
JSON和JSONP JSONP和JSON好像啊,他们之间有什么联系吗? JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.对于JSON大家应该是很了解了吧 ...