AI写作智能体 自主规划任务,支持联网查询和网页读取,多模态高效创作各类分析报告、商业计划、营销方案、教学内容等。 广告
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example, Given input array nums = [1,1,2], Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length. ~~~ public class Solution { public int removeDuplicates(int[] A) { if(A.length ==0){ return 0; } int count=1; for(int i=1; i<A.length; ++i){ if(A[i] != A[i-1]){ //注意这行代码 A[count]=A[i]; count++; } } return count; } } // 若数族大小为0/1。 数组A 不变,返回0/1。 // 若数组大小大于1。用count记录新数组元素A的末尾的下一个index,若a[i] != a[i-1], 则把 a[i]的值赋给count位置上(a[count] = a[i] )。 // 例如数组元素为 1, 2 2, 2 2, 3, 3, 4。 此时count=2,表示A的下标2前面有两个不同的元素,若 A[5]=3,A[4]=2, 则到A[5] != A[4]时,将A[5]的只赋给A[count]上,然后count++ ~~~