leetcode------Remove Duplicates from Sorted Array II
生活随笔
收集整理的這篇文章主要介紹了
leetcode------Remove Duplicates from Sorted Array II
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
| 標(biāo)題: | Remove Duplicates from Sorted Array II |
| 通過率: | 30.7% |
| 難度: | 中等 |
ollow 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].
做過第一個(gè)版本。就是去除重復(fù)的,用一個(gè)指針記錄真實(shí)應(yīng)該到哪個(gè)位置就行,
現(xiàn)在時(shí)做去除大于2的重復(fù),依然與1一樣,有個(gè)區(qū)別就是用一個(gè)標(biāo)志位來判斷是不是大于2就行
具體看代碼:
1 public class Solution { 2 public int removeDuplicates(int[] A) { 3 if(A.length==0) return 0; 4 int index=1; 5 int flag=0; 6 for(int i=1;i<A.length;i++){ 7 if(A[i]!=A[i-1]){ 8 A[index]=A[i]; 9 index++; 10 flag=0; 11 } 12 else if(A[i]==A[i-1]){ 13 if(flag!=2){ 14 A[index]=A[i]; 15 index++; 16 flag=2; 17 } 18 } 19 } 20 return index; 21 } 22 }?
轉(zhuǎn)載于:https://www.cnblogs.com/pkuYang/p/4333971.html
總結(jié)
以上是生活随笔為你收集整理的leetcode------Remove Duplicates from Sorted Array II的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [Hadoop入门] - 1 Ubunt
- 下一篇: Nodejs基础中间件Connect