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
- leetcode #2206. Divide Array Into Equal Pairs
- LeetCode #
- #20. Valid Parentheses java
- leetcode
- Find Pivot Index
- 도메인 주도 설계 핵심
- codingame #코딩게임 #codingame fall challenge2023 #코딩게임 2023 가을 챌린지
- 코딩게임
- codinGame
- ddd
- 도메인 #도메인 주도 설계 #도메인 주도 설계 핵심 #DDD
- DDD #도메인 #도메인 주도 설계 #도메인 주도 설계 핵심
- 867. Transpose Matrix #Transpose Matrix
- 도메인 주도 설계 핵심 #DDD #도메인 주도 설계 #도메인
- leetcode #20. Valid Parentheses #알고리즘 #leetcode Valid Parentheses
- base7
- DDD #도메인 #도메인 주도 설계 #도메인 주도 섥계 핵심
- Fall Challenge 2023
- #move zeroes
- aws #cloudwatch #log insight
- leetcode #알고리즘 #릿코드
- 반 버논
Archives
- Today
- Total
서하아빠의 개발 블로그
283. Move Zeroes 본문
Given an integer arraynums, move all0's to the end of it while maintaining the relative order of the non-zero elements.
Notethat you must do this in-place without making a copy of the array.
Example 1:
Input: nums = [0,1,0,3,12] Output: [1,3,12,0,0]
Example 2:
Input: nums = [0] Output: [0]
Constraints:
- 1 <= nums.length <= 104
- -231<= nums[i] <= 231- 1
풀이방법
- swap을 이용한 방법으로 풀이
class Solution {
public void moveZeroes(int[] nums) {
int j = 0;
for( int i=0; i < nums.length; i++ ) {
if( nums[i] != 0 ) {
// if i position is not 0, swap i and j number.
swap( nums, i, j);
j++;
}
}
}
private static void swap( int nums[], int i, int j) {
int temp = nums[j];
nums[j] = nums[i];
nums[i] = temp;
}
}
'알고리즘 > LeetCode' 카테고리의 다른 글
217. Contains Duplicate (0) | 2022.05.20 |
---|---|
25. Reverse Nodes in k-Group (0) | 2022.05.20 |
61. Rotate List (0) | 2022.05.19 |
200. Number of Islands (0) | 2021.06.14 |
693. Binary Number with Alternating Bits (0) | 2021.04.20 |