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 5 years have passed since last update.

leetcode学習ノート_No.26

Last updated at Posted at 2020-01-25

LeetCodeの26番目の問題の解決方法

LeetCodeはコーディング面接に向けた練習に使えるサイトです。自分が勉強した知識を利用して問題を解決することで学習効果を高めるためです。

LeetCode

  • 要求:指定されたソート後の配列中の重複要素を削除する

  • 出力:新しい配列と配列の長さ

  • サンプル1:

    • 指定された配列:nums = [1,1,2]
    • 出力結果:New_nums = [1,2], length = 2
  • サンプル2:

    • 指定された配列:nums = [0,0,1,1,1,2,2,3,3,4],
    • 出力結果:New_nums = [0,1,2,3,4], length = 5
leetcode_00026.py
import random
nums =[] #配列初期化

# ランダム関数で指定範囲の配列を作成する(重複あり)
for i in range(15):
    nums.append(random.randint(1,10))

# 配列の要素をソートする
nums.sort()
print(nums[:])

# 重複要素を削除する
for _ in nums:
    #最後の要素の場合for_loopから抜ける
    if nums.index(_) == len(nums)-1:
        break
    #該当要素の次の要素が一致する場合、該当要素を削除する
    elif _ == nums[nums.index(_)+1]:
        nums.pop(nums.index(_))
        continue

# 結果を出力する
print("リスト",nums[:], "の長さは",len(nums))
  • 出力結果(サンプル)
    出力結果
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?