Move Zeroes Solutions in Java
Number 283
Difficulty Easy
Acceptance 57.9%
Link LeetCode
Solutions
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/move-zeroes/description//// Author : liuyubobobo/// Time : 2017-02-09import java.util.*;// Time Complexity: O(n)// Space Complexity: O(n)class Solution1 {public void moveZeroes(int[] nums) {ArrayList<Integer> nonZeroElements = new ArrayList<Integer>();// put all the non zero elements into a new vectorfor (int i = 0; i < nums.length; i++)if (nums[i] != 0)nonZeroElements.add(nums[i]);// make nums[0...nonZeroElements.size()) all non zero elementsfor (int i = 0; i < nonZeroElements.size(); i++)nums[i] = nonZeroElements.get(i);// make nums[nonZeroElements.size()...nums.size()) all zero elementsfor (int i = nonZeroElements.size(); i < nums.length; i++)nums[i] = 0;}private static void printArr(int[] arr){for(int i = 0 ; i < arr.length ; i ++)System.out.print(arr[i] + " ");System.out.println();}public static void main(String args[]){int[] arr = {0, 1, 0, 3, 12};(new Solution1()).moveZeroes(arr);printArr(arr);}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/move-zeroes/description//// Author : liuyubobobo/// Time : 2017-02-09import java.util.*;// Time Complexity: O(n)// Space Complexity: O(n)class Solution2 {public void moveZeroes(int[] nums) {int k = 0; // keep nums[0...k) are all zero elementsfor(int i = 0 ; i < nums.length ; i ++)if(nums[i] != 0)nums[k++] = nums[i];// make the nums[k...end) zerosfor(int i = k ; i < nums.length ; i ++)nums[i] = 0;}private static void printArr(int[] arr){for(int i = 0 ; i < arr.length ; i ++)System.out.print(arr[i] + " ");System.out.println();}public static void main(String args[]){int[] arr = {0, 1, 0, 3, 12};(new Solution2()).moveZeroes(arr);printArr(arr);}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/move-zeroes/description//// Author : liuyubobobo/// Time : 2017-02-09import java.util.*;// Time Complexity: O(n)// Space Complexity: O(n)class Solution3 {public void moveZeroes(int[] nums) {int k = 0; // keep nums[0...k) are all zero elementsfor(int i = 0 ; i < nums.length ; i ++)if(nums[i] != 0)swap(nums, k++, i);}private void swap(int[] nums, int i, int j){int t = nums[i];nums[i] = nums[j];nums[j] = t;}private static void printArr(int[] arr){for(int i = 0 ; i < arr.length ; i ++)System.out.print(arr[i] + " ");System.out.println();}public static void main(String args[]){int[] arr = {0, 1, 0, 3, 12};(new Solution3()).moveZeroes(arr);printArr(arr);}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/move-zeroes/description//// Author : liuyubobobo/// Time : 2017-02-09import java.util.*;// Time Complexity: O(n)// Space Complexity: O(n)class Solution4 {public void moveZeroes(int[] nums) {int k = 0; // keep nums[0...k) are all zero elementsfor(int i = 0 ; i < nums.length ; i ++)if(nums[i] != 0)if(k != i)swap(nums, k ++, i);elsek ++;}private void swap(int[] nums, int i, int j){int t = nums[i];nums[i] = nums[j];nums[j] = t;}private static void printArr(int[] arr){for(int i = 0 ; i < arr.length ; i ++)System.out.print(arr[i] + " ");System.out.println();}public static void main(String args[]){int[] arr = {0, 1, 0, 3, 12};(new Solution4()).moveZeroes(arr);printArr(arr);}}