[LeetCode] 26. Remove Duplicates From Sorted Array

class Solution {
    public int removeDuplicates(int[] nums) {
        if (nums.length <= 1) {
            return nums.length;
        }
        
        // p: the rightest endpoint pointer of a non-repeating subarray
        // q: the pointer to detect new non-repeating number
        int p = 0;
        int q = 1;
        
        // q keeps moving until it finds a non-repeating number or reaches to the end of the array.
        while (q < nums.length) {
            if (nums[p] != nums[q]) {
                nums[p + 1] = nums[q];
                p++;
            }
            q++;
        }
        
        // return the length of the non-repeating "array"
        return p + 1;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1,I mean, there is a certain irony involved when guys who...
    桑桑木阅读 506评论 0 0
  • 不知多少次解锁 每次确实无奈而又绝望的锁屏 心中锁 却找不到合适的钥匙
    霞光中的兰草阅读 211评论 0 0
  • 今天去创新工场参加了优才的师徒班发布会,伍星邀请来很多技术人助阵。 伍星首先讲了做优才学院的初衷,ucai不仅是优...
    赵弘阅读 234评论 0 2
  • 随着社会的发展,物价持续上涨,经济压力不断加大,越来越多的朋友开始关注贷款投资,其中小额贷款最为常见。什么是小额贷...
    海绵宝宝_405a阅读 364评论 0 0