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 ...
随机推荐
- Machine Learing 入门 —— 开门第0篇
一.最近懒了 7月没怎么写博客,倒是一直在学Machine Learning的入门知识,在这里给大家推荐一个不错的自学网站:https://www.coursera.org/ ,Andrew Ng是联 ...
- (String)、toString()与String.valueOf()的区别
(String).Object.toString()正常情况下跟String.valueOf()没有区别. 但当Object是null的时候.toString会抛出异常.valueOf返回" ...
- PythonTip(2)
结尾0的个数 描述: 给你一个正整数列表 L, 输出L内所有数字的乘积末尾0的个数.(提示:不要直接相乘,数字很多,相乘得到的结果可能会很大). 例如: L=[2,8,3,50], 则输出:2 n = ...
- 【转】Java虚拟机类型卸载和类型更新解析
[摘要] 前面系统讨论过java类型加载(loading)的问题,在这篇文章中简要分析一下java类型卸载(unloading)的问题,并简要分析一下如何解决如何运行时加载newly ...
- 【洛谷P1726】上白泽慧音
上白泽慧音 题目链接 强联通分量模板题,Tarjan求强联通分量,记录大小即可 #include<iostream> #include<cstring> #include< ...
- 【题解】UVA11584 Partitioning by Palindromes
UVA11584 https://www.luogu.org/problemnew/show/UVA11584 暑假开始刷lrj紫/蓝书DP题 这几天做的一道 思路 预处理出所有的回文串是否存在 前提 ...
- 有连接服务&无连接服务
面向连接的服务 通信双方在通信时要事先建立一条通信线路,其过程包括建立连接.使用链接.释放链接三个过程 如: TCP 电话 面向无连接的服务 通信双方不需要事先建立一条通信线路,而是把每个带有目的选址 ...
- 自定义App首次启动引导页
代码如下 #import"ZBGuidePageView.h" @interfaceZBGuidePageView()<UIScrollViewDelegate> @p ...
- etcd部署简单说明
etcd是一个K/V分布式存储,每个节点都保存完成的一份数据.有点类似redis.但是etcd不是数据库. 1.先说废话.之所以会用etcd,并不是实际项目需要,而是前面自己写的上传的DBCacheS ...
- java各种业务解决方案总结
最近有点时间,突然感慨良多,感觉辛苦工作这么久什么都没有,总结了以前的工作,将接触的主要工具列出来,希望给大家解决问题做参考.相关工具都是实践检验过的 1.数据库 (1).内存数据库 redis (2 ...