deda
Deda의 데이터 디자인
deda
전체 방문자
오늘
어제
  • 분류 전체보기 (121)
    • Python (27)
      • Python 기초 (17)
      • Python 데이터분석 (10)
    • SQL (9)
    • Coding test (54)
      • Python 기초문제 (45)
      • LeetCode (9)
    • BigData (2)
    • ZeroBase (3)
    • UX (0)
    • Business Review (1)
    • 통계 & 수학 (17)
      • 통계학 (14)
      • 수학 (3)
    • 스터디 (6)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 릿코드
  • 마이데이터
  • 프로그래밍
  • 제로베이스
  • 군수열
  • 빅데이터
  • 통계
  • 미니콘다
  • 팩토리얼
  • 네카라쿠배
  • 데이터분석가
  • matplotlib
  • 함수
  • 모듈
  • 등비수열
  • 최소공배수
  • 계산기
  • pandas
  • 소인수분해
  • 부트캠프
  • 데이터분석
  • 데이터사이언티스트
  • 등차수열
  • 코딩
  • 기초수학
  • 계차수열
  • BMI
  • SQL
  • 파이썬
  • 데이터엔지니어

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
deda
Coding test/LeetCode

[LeetCode] Move Zeroes

[LeetCode] Move Zeroes
Coding test/LeetCode

[LeetCode] Move Zeroes

2022. 5. 20. 15:23

🔍  문제

Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.

Note that you must do this in-place without making a copy of the array.

Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]

Input: nums = [0]
Output: [0]

 

🖥  실행

1) Answer

class Solution:
    def moveZeroes(self, nums: List[int]) -> None:
        n = 0
        while nums.count(0) > n:
            nums.remove(0)
            nums.append(0)
            n += 1

 

2) Better Solution

class Solution(object):
    def moveZeroes(self, nums):
        i = 0
        for j in range(len(nums)):
            if nums[j] != 0:
                nums[i], nums[j] = nums[j], nums[i]
                i += 1

 

'Coding test > LeetCode' 카테고리의 다른 글

[LeetCode] Max Consecutive Ones II  (0) 2022.05.20
[LeetCode] Height Checker  (0) 2022.05.20
[LeetCode] Replace Elements with Greatest Element on Right Side  (0) 2022.05.20
[LeetCode] Valid Mountain Array  (0) 2022.05.20
[LeetCode] Merge Sorted Array  (0) 2022.05.20
  • 🔍  문제
  • 🖥  실행
  •  
'Coding test/LeetCode' 카테고리의 다른 글
  • [LeetCode] Max Consecutive Ones II
  • [LeetCode] Height Checker
  • [LeetCode] Replace Elements with Greatest Element on Right Side
  • [LeetCode] Valid Mountain Array
deda
deda
데이터 분석 / 파이썬 / UX / 정량리서치

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.