| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 | 31 |
Tags
- 백준
- 기사헤드라인
- 백준2225
- 헤드라인모음
- MySQL
- 헤드라인뉴스
- 20201017뉴스
- 헤드라인기사
- json
- 오늘의뉴스
- 파이썬
- 20201015뉴스
- 20201011뉴스
- 뉴스
- 기사
- encoding
- 20200615뉴스
- 코테
- 20201018뉴스
- LeetCode #Python #알고리즘 #코딩테스트 #interview
- 뉴스헤드라인
- 20200816뉴스
- 경제뉴스
- 20201016뉴스
- 20201013뉴스
- 크롤링
- Python
- 헤드라인
- 알고리즘
- C++
Archives
- Today
- Total
HelloCho
[Python] #27. Remove Element(Easy) 본문
문제

이 문제는 배열과 값이 주어질 때, 배열 안에서 주어진 값을 제거하고 값을 in-place방식으로 return 하는 문제이다.
해결 방법:
for문을 통해서 val과 일치하는 값을 제거했다. del을 통해서 제거할 때 index값이 하나씩 당겨지기 때문에 s라는 변수를 통해서 index값을 맞춰줬다.
class Solution:
def removeElement(self, nums, val):
s = 0
for idx in range(len(nums)):
if nums[idx-s] == val:
del nums[idx-s]
s +=1
return len(nums)
결과

28ms의 속도와 13.9MB로 통과했다!
'코테 > LeetCode' 카테고리의 다른 글
| [Python] #54. Spiral Matrix(Medium) (0) | 2020.09.11 |
|---|---|
| [Python] #28. Implement strStr()(Easy) (0) | 2020.09.06 |
| [Python] #26. Remove Duplicates from Sorted Array(Easy) (0) | 2020.09.06 |
| [Python] #21. Merge Two Sorted Lists(Easy) (0) | 2020.09.06 |
| [Python] #20. Valid Parentheses(Easy) (0) | 2020.09.06 |
Comments