026 Remove Duplicates from Sorted Array
来源:互联网 发布:java 消息组件 编辑:程序博客网 时间:2024/04/27 18:42
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.
#include <iostream>#include <cstdio>#include <string>#include <vector>using namespace std;class Solution {public:int removeDuplicates(vector<int>& nums) {if(nums.size() == 0) return nums.size();int cnt = 1;for(int i = 1; i < nums.size(); i++) {while(i < nums.size() && nums[i-1] == nums[i])i++;if(i < nums.size()) {cnt++;nums[cnt-1] = nums[i];}}return cnt;}};
0 0
- LeetCode 026 Remove Duplicates from Sorted Array
- LeetCode 026 Remove Duplicates from Sorted Array
- 026 - Remove Duplicates from Sorted Array
- Remove the duplicates from sorted array 026
- [LeetCode]026-Remove Duplicates from Sorted Array
- LeetCode 026 Remove Duplicates from Sorted Array
- 026 Remove Duplicates from Sorted Array
- LeetCode 026 Remove Duplicates from Sorted Array
- LeetCode 026 Remove Duplicates from Sorted Array
- LeetCode-026 Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array
- Remove Duplicates From Sorted Array
- Remove Duplicates from Sorted Array
- C++作业3
- Top命令的用法详细详解
- 6.5趣味逻辑之委派任务
- MySQL Cluster集群探索与实践
- Windows Server 2008 R2 Hyper-v 基本使用方法
- 026 Remove Duplicates from Sorted Array
- 深入理解JVM之三:类文件结构
- Markdown语法及简要规则
- CT诊断学基础知识
- URAL 1297 最长回文子串(后缀数组)
- 安装并配置TeXLive
- CNN model系列文章
- 关于JAVA多态的一些笔记
- 时域认识采样定理的一个小例子