description:

Given a sorted array nums, 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 by modifying the input array in-place with O(1) extra memory.
Note:

Example:

Example 1:

Given 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 returned length.
Example 2:

Given nums = [0,0,1,1,1,2,2,3,3,4],

Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.

It doesn't matter what values are set beyond the returned length.

my answer:

感恩

快慢指针,慢的指向的一直都是新的,也就是说来一个新的它才往前走一步,快的就是不断向前,发现和慢的不一样的就汇报给那个慢的,然后慢的更新。如果二者相等,则慢的不动,快的前进一步;如果二者不等,则慢的前进一步的同时更新那个新一步指向的数字为现在快指针指向的新数字,快的同时也往前走一步。


大佬的answer:

class Solution {
public:
    int removeDuplicates(vector<int>& nums) {
        if(nums.empty()) return 0;
        int n = nums.size();
        int pre = 0, cur = 0;
        while(cur<n){
            if(nums[pre] == nums[cur]){
                cur++;
            }else{
                nums[++pre] = nums[cur++];
            }
        }return pre+1;
    }
};

relative point get√:

hint :

标签智能推荐:

LeetCode高频题目(100)汇总-Java实现

&nbsp;LeetCode高频题目(100)汇总-Java实现&nbsp;&nbsp;&nbsp;【Leetcode-easy-1】TwoSum【Leetcode-easy-2】AddTwoNumbers【Leetcode-easy-3】LongestSubstringWithoutRepeatingCharacters【Leetcode-easy-5】LongestPalindromicSub

C++ 前期准备

在线编译网站:http://www.dooccn.com/cpp/刷题:https://leetcode.com/https://leetcode-cn.com/

Index

Tableofcontents目录TableofcontentsOJ-Improvemyself,better.Leetcode洛谷OJ-Improvemyself,better.Leetcode洛谷

算法-1

1.能少for循环,就少2.与for无关的变量的不进入for循环刷题网址:1.leetcode:https://leetcode.cn/https://leetcode-cn.com/2.牛客网:https://www.nowcoder.com/

8.2

python版按键精灵,pyautogui。未完成,看书deeplearning,刷题leetcode。明日:网络脚本,看书,刷题。

二分搜索常见题

LeetCode:1011.在D天内送达包裹的能力&nbsp;https://leetcode-cn.com/problems/capacity-to-ship-packages-within-d-days/875.爱吃香蕉的珂珂&nbsp;https://leetcode-cn.com/problems/koko-eating-bananas/1482.制作m束花所需的最少天数&nbsp;htt

09-06日记

1.leetcode复习五道2.你不知道的js学习3.webpack学习

总结-二叉树

二叉树:前序遍历:https://leetcode-cn.com/problems/binary-tree-preorder-traversal/中序遍历:https://leetcode-cn.com/problems/binary-tree-inorder-traversal/后序遍历:https://leetcode-cn.com/problems/binary-tree-postorder

11-29日记

1.leetcode两道2.koa源码学习3.跨域知识学习

ArrayList、ArrayDeque与LinkedList区别

https://blog.csdn.net/u011485472/article/details/109124458https://leetcode-cn.com/circle/article/bXc4tx/