leetcode 26
26. Remove Duplicates from Sorted Array
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.
对排好序的数列去重。返回不重复的数的个数n,并将其一次排列在原来数组的前n位。
代码如下:
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if(nums.empty())
{
return ;
}
int n = nums.size();
if(n == )
{
return ;
}
int sum = ;
int j = ;
for(int i = ; i < n; i++)
{
if(nums[j] != nums[i])
{
nums[j+] = nums[i];
sum++;
j++;
}
}
return sum;
}
};
leetcode 26的更多相关文章
- 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 26. 删除排序数组中的重复项
目录 # 前端与算法 leetcode 26. 删除排序数组中的重复项 题目描述 概要 提示 解析 算法 # 前端与算法 leetcode 26. 删除排序数组中的重复项 题目描述 26. 删除排序数 ...
- [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 ...
- Java实现 LeetCode 26 删除排序数组中的重复项
26. 删除排序数组中的重复项 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) ...
- 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] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...
- 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
https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the ...
- [LeetCode]26. 删除排序数组中的重复项(数组,双指针)
题目 给定一个排序数组,你需要在 原地 删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下 ...
随机推荐
- 寒假学习unity的第一天
1.在Assert中创建材质Material,可以为物体附上材质 2.实例化命令Instantiate(要生成的物体,生成的位置,生成物体的选择角度) 3.检测鼠标左键 if(Inhibitor.Ge ...
- Liferay 6开发学习(二十六):数据库连接相关问题
Liferay中怎么更换数据库? 常碰到有人问Liferay怎么更换数据库,怎么修改数据库连接.在我们第一次启动Liferay的时候,会有一个配置向导,在此配置向导我们可以选择数据库,并配置数据库连接 ...
- 查看.netframeword版本
打开“我的电脑“,在地址栏输入 %systemroot%\Microsoft.NET\Framework
- javascript delete方法
学习delete可以参考下面两个博客,写的都很好,本文大部分参考与以下两个博客 http://www.cnblogs.com/windows7/archive/2010/03/28/1698387.h ...
- struts2访问servlet API
搭建环境: 引入jar包,src下建立struts.xml文件 项目配置文件web.xml. web.xml: <?xml version="1.0" encoding=&q ...
- poj 2109 Power of Cryptography
点击打开链接 Power of Cryptography Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16388 Ac ...
- Selenium 疑问之二:如何使页面滚动条移动到指定元素element的位置处?
在用selenium做测试时,会遇到需要操作的元素不在当前可视页面中的情况,如果是手工测试,自然很简单,手动拖拽滚动条到目标元素处即可. 那么,selenium如何实现这种情形呢?答案是需要借助Jav ...
- 动手学servlet(三) 请求头和响应头信息
获取请求头信息 package servletdemo; import java.io.IOException; import java.util.Enumeration; import javax. ...
- VS error retrieving information from user datastore
搭建好VS2005+PB6.0的开发环境后,新建MFC智能设备应用程序工程出错,错误信息如下: error retrieving information from user datastore 很奇怪 ...
- xml规范及xml解析
http://www.cnblogs.com/wang-meng/p/5374498.html 1,XML基础介绍 xml的概念: XML 指可扩展标记语言(EXtensible Markup Lan ...