# 程式語言十八般武藝 題目:[283. Move Zeroes](https://leetcode.com/problems/move-zeroes/) 總共使用 16 種程式語言 程式語言:C++ ```cpp= class Solution { public: void moveZeroes(vector<int>& nums) { int low = 0; int high = 0; while(high<nums.size()){ if(nums[high]!=0){ swap(nums[low],nums[high]); low++; high++; }else{ high++; } } } }; ``` 程式語言:C ```c= void moveZeroes(int* nums, int numsSize){ int low = 0; int high = 0; while(high<numsSize){ if(nums[high]!=0){ int tmp = nums[low]; nums[low] = nums[high]; nums[high] = tmp; low++; high++; }else{ high++; } } } ``` 程式語言:Python ```python= class Solution(object): def moveZeroes(self, nums): low = 0; high = 0; while high < len(nums) : if nums[high]!=0 : tmp = nums[low]; nums[low] = nums[high] nums[high] = tmp low += 1 high += 1 else : high += 1 ``` 程式語言:Javascript ``` javascript= var moveZeroes = function(nums) { let low = 0; let high = 0; while(high<nums.length){ if(nums[high]!=0){ let tmp = nums[low]; nums[low] = nums[high]; nums[high] = tmp; low++; high++; }else{ high++; } } }; ``` 程式語言:Swift ```swift= class Solution { func moveZeroes(_ nums: inout [Int]) { var low = 0; var high = 0; while(high<nums.count){ if(nums[high] != 0){ var tmp = nums[low]; nums[low] = nums[high]; nums[high] = tmp; low += 1; high += 1; }else{ high += 1; } } } } ``` 程式語言:Java ```java= class Solution { public void moveZeroes(int[] nums) { int low = 0; int high = 0; while(high<nums.length){ if(nums[high]!=0){ int tmp = nums[low]; nums[low] = nums[high]; nums[high] = tmp; low++; high++; }else{ high++; } } } } ``` 程式語言:C# ```csharp= public class Solution { public void MoveZeroes(int[] nums) { int low = 0; int high = 0; while(high<nums.Length){ if(nums[high]!=0){ int tmp = nums[low]; nums[low] = nums[high]; nums[high] = tmp; low++; high++; }else{ high++; } } } } ``` 程式語言:Python3 ```python= class Solution: def moveZeroes(self, nums: List[int]) -> None: low = 0; high = 0; while high < len(nums) : if nums[high]!=0 : tmp = nums[low]; nums[low] = nums[high] nums[high] = tmp low += 1 high += 1 else : high += 1 ``` 程式語言:Ruby ```ruby= def move_zeroes(nums) low = 0; high = 0; while high<nums.length() if nums[high]!=0 tmp = nums[low]; nums[low] = nums[high]; nums[high] = tmp; low += 1; high += 1; else high += 1; end end end ``` 程式語言:Go ```go= func moveZeroes(nums []int) { low := 0 high := 0 for { if high >= len(nums) { break }else if nums[high]!=0 { tmp := nums[low] nums[low] = nums[high] nums[high] = tmp low = low + 1 high = high + 1 }else{ high = high + 1 } } } ``` 程式語言:Kotlin ```kotlin= class Solution { fun moveZeroes(nums: IntArray): Unit { var low = 0; var high = 0; while(high<nums.size){ if(nums[high]!=0){ var tmp = nums[low]; nums[low] = nums[high]; nums[high] = tmp; low++; high++; }else{ high++; } } } } ``` 程式語言:Rust ```rust= impl Solution { pub fn move_zeroes(nums: &mut Vec<i32>) { let mut i: usize = 0; for _ in 0..nums.len() { if nums[i] == 0 { nums.remove(i); nums.push(0); continue; } i += 1; } } } ``` 程式語言:PHP ```php= class Solution { function moveZeroes(&$nums) { $low = 0; $high = 0; while($high<count($nums)){ if($nums[$high]!=0){ $tmp = $nums[$low]; $nums[$low] = $nums[$high]; $nums[$high] = $tmp; $low++; $high++; }else{ $high++; } } } } ``` 程式語言:Typescript ```typescript= function moveZeroes(nums: number[]): void { let low = 0; let high = 0; while(high<nums.length){ if(nums[high]!=0){ let tmp = nums[low]; nums[low] = nums[high]; nums[high] = tmp; low++; high++; }else{ high++; } } }; ``` 程式語言:Dart ```dart= class Solution { void moveZeroes(List<int> nums) { var low = 0; var high = 0; while(high<nums.length){ if(nums[high]!=0){ var tmp = nums[low]; nums[low] = nums[high]; nums[high] = tmp; low++; high++; }else{ high++; } } } } ``` 程式語言:Scala ```scala= object Solution { def moveZeroes(nums: Array[Int]): Unit = { val limit = nums.length - 1 def move(idx: Int): Unit = idx match { case idx if idx == limit => nums(limit) = 0 case _ => nums(idx) = nums(idx + 1) move(idx + 1) } for { i <- limit to 0 by -1 if (nums(i) == 0) } yield { move(i) } } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up