0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

LeetCode日本語修行5日目- [26- 並べ替えられた配列から重複の値を取り除く III]

Last updated at Posted at 2021-04-18

Remove Duplicates from Sorted Array

参考:https://leetcode.com/problems/remove-duplicates-from-sorted-array/

問題の内容

並べ替えられた配列numsが与えられたとき,各要素が一度だけ現れる,重複をその場で除去し,新しい長さを返す。
別の配列のための空間を使わない.入力配列をその場でO(1)の余分なメモリを使って修正する.

例:

例1:
Input: nums = [1,1,2]
Output: 2, nums = [1,2]
numsの中に重複をその場で除去した後、最初の2つの要素が1と2です、length = 2を返す必要があります。返されたlengthの先に何を残すかは問題ではありません。

例2:
Input: nums = [0,0,1,1,1,2,2,3,3,4]
Output: 5, nums = [0,1,2,3,4]

ヒント:

0 <= nums.length <= 3 * 104
-104 <= nums[i] <= 104
nums is sorted in ascending order.


日曜日に合わせて、簡単な問題で良かった...

class Solution {
    fun removeDuplicates(nums: IntArray): Int {
        if(nums.size == 1){
            return 1
        }
        var a = 0
        for(i in 1 until nums.size){   
            if(nums[i] != nums[a]){
                nums[++a] = nums[i]
            } else{
                continue
            }
        }
        return a+1
    }
}

最後のreturn a+1気をつけてください

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?