[Swift]LeetCode26. 刪除排序數組中的重複項 | Remove Duplicates from Sorted Array

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-uvewhbju-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.git

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.github

Example 1:數組

Given nums = [1,1,2],

Your function should return length = , with the first two elements of  being  and  respectively.

It doesn't matter what you leave beyond the returned length.2nums12

Example 2:微信

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

Your function should return length = , with the first five elements of  being modified to , , , , and  respectively.

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

Clarification:app

Confused why the returned value is an integer but your answer is an array?函數

Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.this

Internally you can think of this:spa

// nums is passed in by reference. (i.e., without making a copy)
int len = removeDuplicates(nums);

// any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
    print(nums[i]);
}

給定一個排序數組,你須要在原地刪除重複出現的元素,使得每一個元素只出現一次,返回移除後數組的新長度。code

不要使用額外的數組空間,你必須在原地修改輸入數組並在使用 O(1) 額外空間的條件下完成。

示例 1:

給定數組 nums = [1,1,2], 

函數應該返回新的長度 2, 而且原數組 nums 的前兩個元素被修改成 , 。 

你不須要考慮數組中超出新長度後面的元素。12

示例 2:

給定 nums = [0,0,1,1,1,2,2,3,3,4],

函數應該返回新的長度 5, 而且原數組 nums 的前五個元素被修改成 , , , , 。

你不須要考慮數組中超出新長度後面的元素。
01234

說明:

爲何返回數值是整數,但輸出的答案是數組呢?

請注意,輸入數組是以「引用」方式傳遞的,這意味着在函數裏修改輸入數組對於調用者是可見的。

你能夠想象內部操做以下:

// nums 是以「引用」方式傳遞的。也就是說,不對實參作任何拷貝
int len = removeDuplicates(nums);

// 在函數裏修改輸入數組對於調用者是可見的。
// 根據你的函數返回的長度, 它會打印出數組中該長度範圍內的全部元素。
for (int i = 0; i < len; i++) {
    print(nums[i]);
}

 
 1 class Solution {
 2     func removeDuplicates(_ nums: inout [Int]) -> Int {
 3         //若是數組長度爲0則返回0
 4         if nums.count==0{return 0}
 5         //定義返回的數組新長度
 6         var i:Int=0
 7         //遍歷數組
 8         for num in nums
 9         {
10             if num != nums[i]
11             {
12                 i+=1
13                 nums[i]=num
14             }
15             
16         }
17         return i+1
18     }
19 }

高效率版

 1 class Solution {
 2     func removeDuplicates(_ nums: inout [Int]) -> Int {
 3         var length : Int = 0
 4         if nums.count > 0{
 5             length = 1
 6             var prev : Int = nums[0]
 7             var currentIndex = 1
 8             var index : Int = 1
 9             while index < nums.count {
10                 while(index < nums.count && nums[index] == prev){
11                     index += 1
12                 }
13 
14                 if index < nums.count && nums[index] != prev {
15                     nums[currentIndex] = nums[index]
16                     prev = nums[currentIndex]
17                     length += 1
18                     currentIndex += 1
19                 }
20 
21             } 
22         }
23         
24         return length
25         
26     }
相關文章
相關標籤/搜索