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相关推荐

  1. Remove Duplicates from Sorted Array II -- LeetCode

    原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/  这道题跟Remove Duplicates ...

  2. LeetCode集锦(八) - 第26题 Remove Duplicates From Sorted Array

    LeetCode集锦(八) - 第26题 Remove Duplicates From Sorted Array 问题 Given a sorted array nums, remove the du ...

  3. 2016.5.57—— Remove Duplicates from Sorted List

    Remove Duplicates from Sorted List 本题收获: 指针: 不管什么指针在定义是就初始化:ListNode *head = NULL; 如果给head指针赋值为第一个no ...

  4. [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

    Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For examp ...

  5. 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++> 给出排序好的 ...

  6. Leetcode OJ: Remove Duplicates from Sorted Array I/II

    删除排序数组重复元素,先来个简单的. Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates i ...

  7. LeetCode26. Remove Duplicates from Sorted Array

    问题链接:LeetCode26. Remove Duplicates from Sorted Array 注意点: 1.数组中可能是0个元素: 2.C++程序中,循环变量声明不能写在for语句中(编译 ...

  8. [leetcode]83.Remove Duplicates from Sorted List

    题目 Given a sorted linked list, delete all duplicates such that each element appear only once. Exampl ...

  9. 【11_83】Remove Duplicates from Sorted List

    这道题本质上不难,难的是细节处理,容易出错. 第一遍写的代码越改越大,越臃肿,此时,不如推倒重写,果然,第二次一遍过. Remove Duplicates from Sorted List My Su ...

最新文章

  1. HDU 4869 Turn the pokers(思维+组合公式+高速幂)
  2. CRM系统助力企业找到最大盈利客户
  3. 面向方面编程(Spring AOP)
  4. java 建立ssh隧道_JAVA SSH框架搭建流程
  5. python读取xml文件内容显示不全_python读取xml文件时的问题
  6. dir_recurse是 php函数,php关于url、文件、目录、ip的相关问题汇总
  7. 拓端tecdat|R语言通过伽玛与对数正态分布假设下的广义线性模型对大额索赔进行评估预测
  8. html点击图片后图片移动,移动端点击图片放大图片,可左右切换图片
  9. H5 游戏 俄罗斯方块 双人互动游戏
  10. 用Python寻找最优投资组合
  11. WLAN 基础概念(一)
  12. 基于JAVAWeb美食网站设计计算机毕业设计源码+数据库+lw文档+系统+部署
  13. 【论文笔记】Reaching agreement in the presence of faults (EIG)
  14. 【GDScript】保存/加载物品装备数据
  15. android 白色圆点,Android通知图标是一个白色圆圈
  16. 带符号整数的除法与余数
  17. 现在的传奇游戏 哪个是真的传奇,真假传奇应该怎么区分?
  18. 小米手机因为默认权限设置电话拨号,短信等广播无法使用的解决办法
  19. div设置滚动条和滚动条属性
  20. 父亲节送礼!购机推荐iQOO Neo6 SE与红米Note 11T Pro

热门文章

  1. 转这个博客了,以前的博客不用了。(技术为主,寒暄为辅)
  2. 修改framework后重新刷入手机
  3. 高通 MSM8K bootloader 之三: LK
  4. hdu 5511 Minimum Cut-Cut——分类讨论思想+线段树合并
  5. 基于Linux命令行KVM虚拟机的安装配置与基本使用
  6. 前端知识点总结—-响应式
  7. ELK 日志管理系统,再次尝试记录
  8. [译]Spring Session 与 Spring Security
  9. Windows App开发之集合控件与数据绑定
  10. linux 原子整数操作详解 及 volatile (二)