Remove Duplicates from Sorted ListII
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]
.
原始思路:
找一个标签数字,遍历向量元素,如果相等,则加1, 如果大于2,则continue遍历,如果不同 标签数字换成新的数字
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{vector<int> a = { 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4,4 };int k = 0;int key = a[0];int index = 0;int i = 0;while (i < a.size()){if (a[i] == key){k++;if (k<3)a[index++] = key;else {i++;continue;}}else{key = a[i];a[index++] = key;k = 1;}i++;}for (int i = 0; i < a.size(); i++)cout << a[i] << endl;system("pause");return 0;
}
应为允许一个重复,所以判断就不是相邻而是相隔一个的数字是否不一样。
不一样就需要更新值,但注意到这题更新数组不能是当前的状态,而需要是更新前一个不同的数,用temp存当前值以便于下一次赋值。
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{vector<int> a = { 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4,4 };int k = 0;/*int key = a[0];int index = 0;int i = 0;while (i < a.size()){if (a[i] == key){k++;if (k<3)a[index++] = key;else {i++;continue;}}else{key = a[i];a[index++] = key;k = 1;}i++;}*/int num = 1, i, temp = a[1];for (i = 2; i<a.size(); ++i)if (a[i] != a[i - 2]){a[num++] = temp;temp = a[i];}a[num++] = temp;for (int i = 0; i < a.size(); i++)cout << a[i] << endl;system("pause");return 0;
}
Remove Duplicates from Sorted ListII相关推荐
- Remove Duplicates from Sorted Array II -- LeetCode
原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 这道题跟Remove Duplicates ...
- LeetCode集锦(八) - 第26题 Remove Duplicates From Sorted Array
LeetCode集锦(八) - 第26题 Remove Duplicates From Sorted Array 问题 Given a sorted array nums, remove the du ...
- 2016.5.57—— Remove Duplicates from Sorted List
Remove Duplicates from Sorted List 本题收获: 指针: 不管什么指针在定义是就初始化:ListNode *head = NULL; 如果给head指针赋值为第一个no ...
- [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素
Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For examp ...
- 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 OJ: Remove Duplicates from Sorted Array I/II
删除排序数组重复元素,先来个简单的. Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates i ...
- LeetCode26. Remove Duplicates from Sorted Array
问题链接:LeetCode26. Remove Duplicates from Sorted Array 注意点: 1.数组中可能是0个元素: 2.C++程序中,循环变量声明不能写在for语句中(编译 ...
- [leetcode]83.Remove Duplicates from Sorted List
题目 Given a sorted linked list, delete all duplicates such that each element appear only once. Exampl ...
- 【11_83】Remove Duplicates from Sorted List
这道题本质上不难,难的是细节处理,容易出错. 第一遍写的代码越改越大,越臃肿,此时,不如推倒重写,果然,第二次一遍过. Remove Duplicates from Sorted List My Su ...
最新文章
- HDU 4869 Turn the pokers(思维+组合公式+高速幂)
- CRM系统助力企业找到最大盈利客户
- 面向方面编程(Spring AOP)
- java 建立ssh隧道_JAVA SSH框架搭建流程
- python读取xml文件内容显示不全_python读取xml文件时的问题
- dir_recurse是 php函数,php关于url、文件、目录、ip的相关问题汇总
- 拓端tecdat|R语言通过伽玛与对数正态分布假设下的广义线性模型对大额索赔进行评估预测
- html点击图片后图片移动,移动端点击图片放大图片,可左右切换图片
- H5 游戏 俄罗斯方块 双人互动游戏
- 用Python寻找最优投资组合
- WLAN 基础概念(一)
- 基于JAVAWeb美食网站设计计算机毕业设计源码+数据库+lw文档+系统+部署
- 【论文笔记】Reaching agreement in the presence of faults (EIG)
- 【GDScript】保存/加载物品装备数据
- android 白色圆点,Android通知图标是一个白色圆圈
- 带符号整数的除法与余数
- 现在的传奇游戏 哪个是真的传奇,真假传奇应该怎么区分?
- 小米手机因为默认权限设置电话拨号,短信等广播无法使用的解决办法
- div设置滚动条和滚动条属性
- 父亲节送礼!购机推荐iQOO Neo6 SE与红米Note 11T Pro