Remove Duplicates from Sorted Array II
来源:互联网 发布:农业大数据方案 编辑:程序博客网 时间:2024/05/22 01:38
题目
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]
.
方法
和上一题不同的是相同元素允许存在两个,使用flag来标记该元素是否已经保存了两个。
public int removeDuplicates(int[] A) { if (A == null) { return 0; } int len = A.length; if (len < 3) { return len; } boolean flag = false; int end = 0; for (int i = 1; i < len; i++) { if (A[i] != A[end]) { end++; A[end] = A[i]; flag = false; } else { if (flag == false) { end++; A[end] = A[i]; flag = true; } } } return end + 1; }
0 0
- LeetCode: Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- LeetCode: Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- [Leetcode] Remove Duplicates from Sorted Array II
- [LeetCode] Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- [leetcode] Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- Remove duplicates from sorted array II
- Remove Duplicates from Sorted Array II
- [LeetCode]Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- Remove Duplicates from Sorted Array II
- [leetcode]Remove Duplicates from Sorted Array II
- LeetCode-Remove Duplicates from Sorted Array II
- mysql 建立索引
- C++类的Constructors,Copy,Assignment,Destruction(二)
- 黑马程序员-Java入门需掌握的30个基本概念
- 常用排序算法总结与分析(含全部源码)
- OCP 1Z0 052 11
- Remove Duplicates from Sorted Array II
- 一种对拉格朗日乘子的直观理解
- leedcode做题总结,题目Reverse Words in a String14/03/05
- 【Leetcode】Word Break II
- 黑马程序员-Java基础语法
- 黑马程序员-Java关键字
- oracle11g倒出空表
- HDU 3049 Data Processing 数论题解
- 黑马程序员_XML