[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 ...
随机推荐
- 【SpringBoot】整合定时任务和异步任务
========================10.SpringBoot整合定时任务和异步任务处理 =============================== 1.SpringBoot定时任务s ...
- TableLayoutPanel 动态添加 行 列
//添加行 横排 ++this.tbPnl.RowCount; this.tbPnl.RowStyles.Add(new System.Windows.Forms.RowStyle(System ...
- python3中的编码
python2字符串编码存在的问题: 使用 ASCII 码作为默认编码方式,对中文处理不友好 把字符串分为 unicode 和 str 两种类型,将unicode作为唯一内码,误导开发者 python ...
- 请确保 ASP.NET State Service (ASP.NET 状态服务)已启动 问题解决
当iis部署的网站访问遇到如下错误时: 无法向会话状态服务器发出会话状态请求.请确保 ASP.NET State Service (ASP.NET 状态服务)已启动,并且客户端端口与服务器端口相同.如 ...
- saltstack基础知识
saltstack简介 saltstack基于python开发的C/S架构的配置管理工具,分为服务器端salt-master和客户端salt-minion.并且支持浩称最快的ZeroMQ消息队列机制, ...
- JavaScript获取mp4文件MIME编码格式,用于判读是否是h.264,解决在线播放只有声音问题
测试网址:https://gpac.github.io/mp4box.js/test/filereader.html js库:mp4box.js 不能在线播放的:audio/mp4; codecs=& ...
- 怎样使用PL/SQL在不安装oracle 客户端的情况下使用oracle数据库
在网上查了好多这方面的例子,但是似乎说的都不准确,在咨询朋友后终于实现了本机不安装oracle 的情况下,在windows系统上实现连接服务器上的数据库,现在贴出来与大家共享. 首先,我们需要一个PL ...
- tomcat的 tomcat-user.xml
http://blog.csdn.net/asdeak/article/details/1879284 很多个tomcat因为在缺少 " <role rolename="m ...
- Ali流量控制中间件Sentinel
原文链接: https://blog.csdn.net/u012190514/article/details/81383698 Sentinel 是什么 随着微服务的流行,服务和服务之间的稳定性变得越 ...
- LSTM/RNN中的Attention机制
一.解决的问题 采用传统编码器-解码器结构的LSTM/RNN模型存在一个问题,不论输入长短都将其编码成一个固定长度的向量表示,这使模型对于长输入序列的学习效果很差(解码效果很差). 注意下图中,ax ...