Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- ssh도커설치
- 스프링부트팔로우취소
- 출처 코딩셰프
- 스프링구독
- vm도커설치하는법
- 멀티폼
- 스프링부트서버에사진전송
- 스프링부트
- 스프링사진
- 스프링이미지업로드
- 스프링익셉션처리
- 출처 메타코딩
- 인스타클론
- 서버에도커설치
- 출처 따배도
- 스프링부트사진올리기
- 도커설치하는법
- centos도커설치
- WAS웹서버
- 스프링부트api
- 스프링부트구독취소
- springboot_exception_handler
- 출처 문어박사
- 스프링부트팔로잉
- dockerinstall
- 우분투도커설치
- 스프링부트중복예외처리
- 스프링사진업로드
- 파이썬sort
- 출처 노마드코더
Archives
- Today
- Total
MakerHyeon
26. Remove Duplicates from Sorted Array 본문
https://leetcode.com/problems/remove-duplicates-from-sorted-array
Remove Duplicates from Sorted Array - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
SOLUTION CODE
# PYTHON
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
l = 1
for r in range(1,len(nums)):
if nums[r-1]!=nums[r]: # 중복수X 라면
nums[l]=nums[r] # l위치에 해당 수 넣고
l += 1 # l위치포인터 한칸 이동
return l
# C++
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int l = 1, r = 1;
while( r < nums.size()){
if(nums[r-1]!=nums[r]){
nums[l++] = nums[r];
}
r++;
}
return l;
}
};
● 포인터 이용
l - 중복되지않은 결과 원소가 담길 다음 위치를 가리킴
r - 이전값과 중복되었는지를 체크하기 위하여, 전체 배열을 돌며 검사할 포인터
'Algorithm > LeetCode' 카테고리의 다른 글
1929. Concatenation of Array (0) | 2023.01.01 |
---|---|
347. Top K Frequent Elements (0) | 2022.12.22 |
1. Two Sum (0) | 2022.12.22 |
242. Valid Anagram (1) | 2022.12.20 |
217. Contains Duplicate (0) | 2022.12.20 |
Comments