FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/java/283_Move_Zeros.java at master · jakehoare/leetcode · GitHub
jakehoare
/
leetcode
Public
Notifications
You must be signed in to change notification settings
Fork
30
Star
51
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
java
/
283_Move_Zeros.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
25 lines (21 loc) · 681 Bytes
Breadcrumbs
leetcode
/
java
/
283_Move_Zeros.java
Copy path
File metadata and controls
25 lines (21 loc) · 681 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
https://leetcode.com/problems/move-zeroes/
Given an array nums, write a function to move all 0's to the end of it while maintaining
the relative order of the non-zero elements.
Move non-zeros to front. For each non-zero, swap with the next index after the existing block of non-zeros.
Time - O(n)
Space - O(1)
*/
public
class
Solution
{
public
void
moveZeroes
(
int
[]
nums
) {
int
nextNonZero
=
0
;
for
(
int
i
=
0
;
i
<
nums
.
length
; ++
i
) {
if
(
nums
[
i
] !=
0
) {
int
temp
=
nums
[
i
];
nums
[
i
] =
nums
[
nextNonZero
];
nums
[
nextNonZero
] =
temp
;
++
nextNonZero
;
}
}
}
}
Back
|
FazBrowse Home
|
New Git URL