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 |
| 31 |
Tags
- Python
- 뷰
- 백준
- flutter getx
- 코딩테스트 준비
- 안드로이드
- c++ 코테
- ML
- 플러터
- Laravel
- 라라벨
- 개발자 취업
- 알고리즘
- DP
- 파이썬 코테
- til
- react
- Flutter
- vue
- 코딩테스트
- 항해99
- 코딩테스트준비
- C++
- 오블완
- 99클럽
- 코테 파이썬
- 코테
- 개발자취업
- 티스토리챌린지
- 파이썬
Archives
- Today
- Total
잡다로그
[99클럽 코테 스터디] 15일차 TIL - 배열, Shuffle the Array 본문
[15일차] 배열
문제: https://leetcode.com/problems/shuffle-the-array/description/

Solution
class Solution(object):
def shuffle(self, nums, n):
"""
:type nums: List[int]
:type n: int
:rtype: List[int]
"""
x = nums[0:n] # 0부터 n-1까지, n개
y = nums[n:2*n] # n부터 2*n-1까지 n개
result = []
for i in range(n):
result.append(x[i])
result.append(y[i])
return result
Another Solution
나는 배열 슬라이싱을 이용했지만, 굳이 변수를 만들지 않고 인덱스만을 활용해서 정답을 출력하는 방법도 있다.
class Solution(object):
def shuffle(self, nums, n):
leng = len(nums)
j = n
a=[]
for i in range(n):
a.append(nums[i])
a.append(nums[j])
j+=1
return a
나다어
- 파이썬 인덱스 슬라이싱은 [n:k]로 쓰고, n부터 k-1까지 슬라이싱한다. k는 포함 안됨 주의!
- 배열 문제는 인덱스를 적극적으로 활용하자.
'Algorithm' 카테고리의 다른 글
| [Python/코테] LeetCode 2433. Find The Original Array of Prefix Xor (0) | 2024.06.15 |
|---|---|
| [99클럽 코테 스터디] 16일차 TIL - 배열, Number of Good Pairs (0) | 2024.06.15 |
| [Python/코테] 프로그래머스 순위 (0) | 2024.06.15 |
| [Python/코테] 플로이드-와샬 알고리즘 - 개념, 코드 (0) | 2024.06.15 |
| [99클럽 코테 스터디] 14일차 TIL - 그래프, Minimum Number of Moves to Seat Everyone (0) | 2024.06.13 |
Comments