LeetCode算法入门- Remove Duplicates from Sorted Array -day21
LeetCode算法入門- Remove Duplicates from Sorted Array -day21
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.
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.
題目分析
將一個數組重復的元素去掉,并返回數組長度。
Java實現
定義兩個指針,i指針從0開始,j從1開始,如果nums[i]與nums[j]相等,則j向后移動,移到nums[i]與nums[j]不相等,這是才將
nums[++i] = nums[j],最后返回i+1.
總結
以上是生活随笔為你收集整理的LeetCode算法入门- Remove Duplicates from Sorted Array -day21的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring常见术语理解
- 下一篇: 交易系统如何确保账簿100%准确