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)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

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

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
deda

Deda의 데이터 디자인

[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 / 정량리서치

    티스토리툴바