//GitHub https://github.com/vellimole0621
vellimole0621 - Overview
キム・ヒョンギュ . vellimole0621 has 6 repositories available. Follow their code on GitHub.
github.com
Explore - LeetCode
LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore.
leetcode.com
:
Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in nums.
Consider the number of unique elements of nums to be k, to get accepted, you need to do the following things:
- Change the array nums such that the first k elements of nums contain the unique elements in the order they were present in nums initially. The remaining elements of nums are not important as well as the size of nums.
- Return k.
=> 숫자로 이루어진 배열이 주어져있다. 중복된 값을 뺀 뒤, 배열의 길이를 반환하라.
내 풀이
:
중간 인덱스 조절할 변수 생성(res) > 전체 배열에서 첫번째 요소를 제외하고, 이전 값과 같은 경우 그 값을 pop() 이용 제외한다. > 최종적으로 남은 nums 함수의 길이를 반환한다.
# Remove Duplicates from Sorted Array
# https://leetcode.com/explore/learn/card/fun-with-arrays/511/in-place-operations/3258/
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
res = 0
for i in range(0, len(nums)):
i -= res
if i != 0 and nums[i] == nums[i-1]:
nums.pop(i)
res += 1
return len(nums)
'프로그래밍 > Python' 카테고리의 다른 글
[파이썬 Python/알고리즘] LeetCode - Move Zeroes (0) | 2023.09.19 |
---|---|
[파이썬 Python/알고리즘] LeetCode - Replace Elements with Greatest Element on Right Side (0) | 2023.09.17 |
[파이썬 Python/알고리즘] LeetCode - Check If N and Its Double Exist (0) | 2023.09.11 |
[파이썬 Python/알고리즘] LeetCode - Remove Element (0) | 2023.09.07 |
[파이썬 Python/알고리즘] LeetCode - Merge Sorted Array (0) | 2023.09.05 |