leetcode 之Remove Duplicates from Sorted Array(2)
描述
Follow up for ”Remove Duplicates”: What if duplicates are allowed at most twice?
For example, Given sorted array A = [1,1,1,2,2,3],
Your function should return length = 5, and A is now [1,1,2,2,3]
之前的想法是再加一个计数的变量就行了
int removeDeplicates1(int A[], int n)
{
int index = , count = ;
for (int i = ; i < n; i++)
{
if (A[index] != A[i])
{
A[index++] = A[i];
count = ;
}
else
{
if (count <= )
{
A[index] = A[i];
count++;
}
}
} return index + ;
}
后来看到了一个更简洁的做法,这个想法很巧妙啊:因为是排好序的,所以只需拿第三个和第一个比,不同的话保存即可。
int removeDeplicates1(int A[], int n)
{
int index = ;
for (int i = ; i < n; i++)
{
if (A[index-] != A[i])
{
A[index++] = A[i];
} } return index;
}
leetcode 之Remove Duplicates from Sorted Array(2)的更多相关文章
- LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>
LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...
- 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] 80. Remove Duplicates from Sorted Array II ☆☆☆(从有序数组中删除重复项之二)
https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/discuss/27976/3-6-easy-lines-C% ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项 II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [LeetCode] 26. Remove Duplicates from Sorted Array ☆(从有序数组中删除重复项)
[LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项 描述 Given a sorted array nums, remove the d ...
- [LeetCode] 80. Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [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】Remove Duplicates from Sorted Array II
Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...
- 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 OJ Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
随机推荐
- 【BZOJ4198】【NOI2015】荷马史诗(贪心,Huffman树)
[BZOJ4198][NOI2015]荷马史诗(贪心,Huffman树) 题面 BZOJ 洛谷 题解 合并果子都是不知道多久以前做过的了.现在才知道原来本质就是一棵哈夫曼树啊. 这题我们仔细研究一下题 ...
- 第三周——构建一个简单的Linux系统MenuOS
[洪韶武 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ] 第三周 构建一个 ...
- spark(三)spark sql
一.DataFrame 1.DataFrame是组织成命名列的数据的分布式集合,类似于关系型数据库的一张表,如果没有列名就等于RDD,如果有列名,就是DataFrames DataFrames可以从各 ...
- NOIP2010-2015后四题汇总
1.前言 正式开始的第一周的任务——把NOIP2010至NOIP2015的所有D1/2的T2/3写出暴力.共22题. 暴力顾名思义,用简单粗暴的方式解题,不以正常的思路思考.能够较好的保证正确性,但是 ...
- Codeforces Round #358 (Div. 2) A B C 水 水 dfs序+dp
A. Alyona and Numbers time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Java Socket UDP编程
package com; import java.io.IOException; import java.net.*; /** * UDP Client * * Created by Administ ...
- [DeeplearningAI笔记]卷积神经网络3.10候选区域region proposals与R-CNN
4.3目标检测 觉得有用的话,欢迎一起讨论相互学习~Follow Me 3.10 region proposals候选区域与R-CNN 基于滑动窗口的目标检测算法将原始图片分割成小的样本图片,并传入分 ...
- Tensorboard教程:监控指标可视化
Tensorflow监控指标可视化 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 强烈推荐Tensorflow实战Google深度学习框架 实验平台: Tensorflow1.4. ...
- 1.java内存区域与内存溢出异常
1.java运行时数据区如图所示: 2.每个区域的功能 (1)程序计数器(寄存器) 当前线程所执行的字节码的行号指示器 为了线程切换后能够恢复到正确的执行位置,因此每个线程拥有自己独立的程序计数器 如 ...
- 树链剖分处理+线段树解决问题 HDU 5029
http://acm.split.hdu.edu.cn/showproblem.php?pid=5029 题意:n个点的树,m次操作.每次操作输入L,R,V,表示在[L,R]这个区间加上V这个数字.比 ...