ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't matter what you leave beyond the new length. ~~~ public class Solution { public int removeDuplicates(int[] A) { if(A==null || A.length==0) return 0; int idx = 0; int count = 0; for(int i=0;i<A.length;i++) { if(i>0 && A[i]==A[i-1]) { count++; if(count>=3) continue; } else { count = 1; } A[idx++]=A[i]; } return idx; } } ~~~