[leetcode]26. Remove Duplicates from Sorted Array有序数组去重(单个元素只出现一次)
Given a sorted array nums, 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 by modifying the input array in-place with O(1) extra memory.
Example 1:
Given 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 returned length.
Example 2:
Given nums = [0,0,1,1,1,2,2,3,3,4], Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively. It doesn't matter what values are set beyond the returned length.
题意:
去除有序数组的重复元素,使得每个元素只能在数组中出现一次
思路:
仍然是在尽量不开新空间的情况下,
直接在原数组中进行操作。
指针i来遍历原数组。
指针j 从 index 1 开始, 因为index 为 0 的元素是什么牛鬼蛇神都不重要。
指针j 接受指针i扫过的、符合条件的元素。
指针j所到之处,便是新“数组”新生之时。
代码:
class Solution {
public int removeDuplicates(int[] nums) {
if(nums.length == 0) return 0;
int j = 1;
for(int i = 1; i < nums.length; i++){
if(nums[i] != nums[j-1]){
nums[j] = nums[i];
j++;
}
}
return j;
}
}
[leetcode]26. Remove Duplicates from Sorted Array有序数组去重(单个元素只出现一次)的更多相关文章
- [LeetCode] 26. Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...
- leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)
[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...
- 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] 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 (移除有序数组中重复数字)
题目链接: https://leetcode.com/problems/remove-duplicates-from-sorted-array/?tab=Description 从有序数组中移除重 ...
- 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
Problem: Given a sorted array, remove the duplicates in place such that each element appear only onc ...
- Java [leetcode 26]Remove Duplicates from Sorted Array
题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...
随机推荐
- 小程序通过background-image设置背景图片
微信小程序通过background-image设置背景:只支持线上图片和base64图片,不支持本地图片:base64图片设置步骤如下: 1.在网站http://imgbase64.duoshiton ...
- 记录一次mysql查询速度慢造成CPU使用率很高情况
1.某日zabbix告警,某台机器CPU使用率过高. 查看慢查询日志,看到很多sql语句都超过10秒 把sql语句拿出来放在查询窗口执行.前面加上explain就可以查看详细查询信息 playcode ...
- DNS基础
什么是DNS? DNS--Domain name system,域名系统,简单来说就是域名和IP地址间的映射关系.当你在浏览器的地址栏输入网址(或域名,如 www,baidu.com)的时候,在网络中 ...
- AttributeError: 'module' object has no attribute 'main'
本机环境:ubuntu16.04, ros-kinetic $ roscore 报错 Traceback (most recent call last): File , in <module& ...
- 解决Ubuntu中文显示为乱码
1. 安装所需软件 sudo apt-get install zh-autoconvert sudo apt-get install zhcon 2. 配置系统 $ vi /var/lib/local ...
- Linux根据MAC地址自动设置IP
Linux根据MAC地址自动设置IP #!/bin/sh #============config============ route_defa=60.12.70.65 addr_ip= link_ma ...
- 使用gitbook plugin
使用gitbook plugin. { "title": "xx doc", "author": "morya", &q ...
- wepy打开页面首次不显示,但是数据已经有了
page页面首次打开异步数据无法通过props传递到子组件 解决:在开发者工具关闭上传代码时自动压缩就解决了,在wepy文档里也有强调
- ios7自定义返回按钮后,右滑返回功能失效解决方法
-(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; //开启ios右滑返回 if ([ ...
- [leetcode]2. Add Two Numbers.cpp
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...