Skip to content

Commit 105a732

Browse files
committed
leetcode
1 parent 8606f3b commit 105a732

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed

MoveZeroes/move_zeroes.dart

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
3+
-* Move Zeroes *-
4+
5+
Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.
6+
7+
Note that you must do this in-place without making a copy of the array.
8+
9+
10+
11+
Example 1:
12+
13+
Input: nums = [0,1,0,3,12]
14+
Output: [1,3,12,0,0]
15+
Example 2:
16+
17+
Input: nums = [0]
18+
Output: [0]
19+
20+
21+
Constraints:
22+
23+
1 <= nums.length <= 104
24+
-231 <= nums[i] <= 231 - 1
25+
26+
27+
Follow up: Could you minimize the total number of operations done?
28+
29+
30+
*/
31+
32+
class Solution {
33+
void moveZeroes(List<int> nums) {
34+
int zero = 0;
35+
36+
for (int index = 0; index < nums.length; index++) {
37+
if (nums[index] == 0) {
38+
zero++;
39+
} else if (zero > 0) {
40+
int tempBall = nums[index];
41+
nums[index] = 0;
42+
nums[index - zero] = tempBall;
43+
}
44+
}
45+
}
46+
}
47+
48+
class B {
49+
void moveZeroes(List<int> nums) {
50+
int index = 0;
51+
for (int i = 0; i < nums.length; i++) {
52+
nums[index++] = nums[i];
53+
}
54+
while (index < nums.length) {
55+
nums[index] = 0;
56+
index++;
57+
}
58+
}
59+
}
60+
61+
class C {
62+
void moveZeroes(List<int> nums) {
63+
int left = 0;
64+
int right = 1;
65+
while (left < nums.length && right < nums.length) {
66+
if (nums[left] == 0 && nums[right] != 0) {
67+
int temp = nums[left];
68+
nums[left] = nums[right];
69+
nums[right] = temp;
70+
} else if (nums[left] == 0 && nums[right] == 0) {
71+
right++;
72+
} else if (nums[left] != 0) {
73+
left++;
74+
right++;
75+
} else {
76+
left++;
77+
}
78+
}
79+
}
80+
}

MoveZeroes/move_zeroes.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
func moveZeroes(nums []int) {
4+
var left int = 0
5+
var right int = 1
6+
for left < len(nums) && right < len(nums) {
7+
if nums[left] == 0 && nums[right] != 0 {
8+
var temp int = nums[left]
9+
nums[left] = nums[right]
10+
nums[right] = temp
11+
} else if nums[left] == 0 && nums[right] == 0 {
12+
right++
13+
} else if nums[left] != 0 {
14+
right++
15+
left++
16+
} else {
17+
left++
18+
}
19+
}
20+
21+
}

MoveZeroes/move_zeroes.md

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# 🔥 Move Zeroes 🔥 || Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1
4+
5+
```dart
6+
class Solution {
7+
void moveZeroes(List<int> nums) {
8+
int zero = 0;
9+
10+
for (int index = 0; index < nums.length; index++) {
11+
if (nums[index] == 0) {
12+
zero++;
13+
} else if (zero > 0) {
14+
int tempBall = nums[index];
15+
nums[index] = 0;
16+
nums[index - zero] = tempBall;
17+
}
18+
}
19+
}
20+
}
21+
```
22+
23+
## Solution - 2
24+
25+
```dart
26+
class Solution {
27+
void moveZeroes(List<int> nums) {
28+
int index = 0;
29+
for (int i = 0; i < nums.length; i++) {
30+
nums[index++] = nums[i];
31+
}
32+
while (index < nums.length) {
33+
nums[index] = 0;
34+
index++;
35+
}
36+
}
37+
}
38+
```
39+
40+
## Solution - 3
41+
42+
```dart
43+
class Solution {
44+
void moveZeroes(List<int> nums) {
45+
int left = 0;
46+
int right = 1;
47+
while (left < nums.length && right < nums.length) {
48+
if (nums[left] == 0 && nums[right] != 0) {
49+
int temp = nums[left];
50+
nums[left] = nums[right];
51+
nums[right] = temp;
52+
} else if (nums[left] == 0 && nums[right] == 0) {
53+
right++;
54+
} else if (nums[left] != 0) {
55+
left++;
56+
right++;
57+
} else {
58+
left++;
59+
}
60+
}
61+
}
62+
}
63+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
116116
- [Toeplitz Matrix](ToeplitzMatrix/toeplitz_matrix.dart)
117117
- [Missing Number](MissingNumber/missing_number.dart)
118118
- [First Bad Version](FirstBadVersion/first_bad_version.dart)
119+
- [Move Zeroes](MoveZeroes/move_zeroes.dart)
119120

120121
## Reach me via
121122

0 commit comments

Comments
 (0)