这是一类典型的题,思路是,一开始可以再开一个辅助数组,这样会更直观一些。如果新数组改变不会影响原数组,则可以合并起来,如本题。
package Level2;
import java.util.Arrays;
/**
* Remove Duplicates from Sorted Array II
*
* 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].
*/
public class S80 {
public static void main(String[] args) {
// int[] A = {1,1,1,1,2,2,2,3};
int[] A = {1,2,2};
int k = removeDuplicates(A);
for(int i=0; i<k; i++){
System.out.print(A[i] + " ");
}
}
// 很经典的一道题!
// 思路是,一开始可以再开一个辅助数组,这样会更直观一些。
// 如果新数组改变不会影响原数组,则可以合并起来,如本题
public static int removeDuplicates(int[] A) {
// i用来遍历数组,j保存新数组的最后位置
int i = 1, j = 0;
int cnt = 0;
if(A.length < 3){
return A.length;
}
for(; i<A.length; i++){
if(A[i] == A[j]){ // 重复的情况
if(cnt == 0){ // 当且仅当重复一次(共出现两次)的情况下才复制
A[++j] = A[i];
}
cnt++;
}else{ // 不重复的情况,更新j的位置,然后直接复制过去
A[++j] = A[i];
cnt = 0; // 重置counter
}
}
return j+1;
}
}
分享到:
相关推荐
LeetCode Remove Duplicates from Sorted Array解决方案
26.Remove_Duplicates_from_Sorted_Array删除有序数组中的重复项【LeetCode单题讲解系列
leetcode LeetCode 这个库用于总结leetcode中遇到的习题 常用数据结构习题总结 1.线性表 解决进度 No. Describition mark 1 Remove Duplicates from Sorted Array 2 Remove Duplicates from Sorted Array II 3 ...
String/26_remove_duplicates_from_sorted_array.md) [(雅虎)139。 Word Break](Leetcode 问题/数组和字符串/139.word_break.md) [140. Word Break ii](Leetcode 问题/数组和字符串/140.word_break_ii.md) [151. ...
Remove Duplicates from Sorted Array iii. Plus One iv. Pascal's Triangle v. Merge Sorted Array vi. Sum vii. Find Minimum in Rotated Sorted Array viii. Largest Rectangle in Histogram ix. Maximal ...
Leetcode经典01背包 algo 1. 数据结构与算法 数组,链表,(串和序列) 堆,栈,队列 树,图 排序,搜索 贪心,回溯,动态规划 堆:一种完全二叉树,任意节点大于左右孩子(大顶堆) 树:二叉树,DFS,BFS 1 排序与...
leetcode 答案LeetCode LeetCode in Swift 这个Repo 用来存下我在LeetCode 解题的原始档,包含解题中遇到的错误,也包含解出AC 的答案, 以下的清单将连结到我的Github Pages 中,皆有题目中文翻译与解题的过程。...
Remove Duplicates from Sorted Array II Longest Consecutive Sequence Two Sum 3Sum 3Sum Closest 4Sum Remove Element Move Zeroes Next Permutation Permutation Sequence Valid Sudoku Trapping Rain Water ...
leetcode算法题主函数如何写 leetcode 每日一题 从今天开始,每天必须完成至少2题。即为保持写代码的手感,也巩固一下编码的基础知识。 2018.1.23 题目:Remove Duplicates from Sorted Array Given a sorted array,...
颜色分类leetcode My Leetcode Problems Solutions Using javascript(ES6) 1 Two Sum 两数之和 5 Longest Palindromic Substring 最长回文子串 7 Reverse Integer 整数反转 9 Palindrome Number 回文数 11 Container...
lru缓存leetcode leetcode 1. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 5. Longest Palindromic Substring 7. Reverse Integer 9. ...
leetcode 1004 leetcode E:简单,M:中等,H:困难 数组和字符串 217. Contains Duplicate (E) 48. Rotate Image (M) -> 2 73. Set Matrix Zeroes (M) 1. Two Sum (E) 167. Two Sum II - Input array is sorted (E)...
java lru leetcode :ice_cream: LeetCode Kindem 的个人 LeetCode ...Duplicates from Sorted Array 48 Rotate Image 53 Maximum Subarray 55 Jump Game 56 Merge Intervals 64 Minimum Path Sum 73
leetcode python 001 LeetCode 建立两个个主要资料夹(题目收集、学习内容)+一个玩整的README整理 题目 主要以Python记录于VScode上 先记录头150题 学习 以JupyterNotebook为主 纪录各种资料结构、演算法等 ...
com.leetcode.array Search a 2D Matrix Spiral Matrix com.leetcode.list Linked List Cycle Linked List Cycle II Remove Duplicates from Sorted List com.leetcode.string Single Number com.leetcode.tree ...
26.Remove Duplicates from Sorted Array 53.Maximum Subarray 70.Climbing Stairs 121.Best Time to Buy and Sell Stock 122.Best Time to Buy and Sell Stock II 123.Best Time to Buy and Sell Stock III 141....
leetcode 苹果 OnlineJudge Code for Online Judge. LeetCode # Title Solution Difficulty 1 Two Sum Easy 26-1 Remove Duplicates from Sorted Array Easy 26-2 Remove Duplicates from Unsorted Array Easy 27 ...
O(m+n) time, O(m+n) sapce.*0026 Remove Duplicates from Sorted Array使用双指针,一个快指针,一个慢指针。开始时,两个指针都指向首元素。当两指针元素值相同时,快指针+1;当两指针元素不同时,慢
leetcode写题闪退 #*的多少代表此题的有意思程度 有几题第一次写的时候思绪比较混乱: *****Regular Expression Matching 2014.10.29 对于Find Minimum in Rotated Sorted Array II 和 Find Minimum in Rotated ...
leetcode 代码Remove Duplicates from Sorted Array