FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/problems/src/array/MissingNumber.java at master · KindleBooks66/leetcode · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
KindleBooks66
/
leetcode
Public
forked from
gouthampradhan/leetcode
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
leetcode
/
problems
/
src
/
array
/
MissingNumber.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
29 lines (26 loc) · 813 Bytes
Breadcrumbs
leetcode
/
problems
/
src
/
array
/
MissingNumber.java
Copy path
File metadata and controls
29 lines (26 loc) · 813 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
26
27
28
29
package
array
;
/**
* Created by gouthamvidyapradhan on 04/07/2017. Given an array containing n distinct numbers taken
* from 0, 1, 2, ..., n, find the one that is missing from the array.
*
* <p>For example, Given nums = [0, 1, 3] return 2.
*
* <p>Note: Your algorithm should run in linear runtime complexity. Could you implement it using
* only constant extra space complexity?
*/
public
class
MissingNumber
{
public
static
void
main
(
String
[]
args
)
throws
Exception
{
int
[]
nums
= {
0
};
System
.
out
.
println
(
new
MissingNumber
().
missingNumber
(
nums
));
}
public
int
missingNumber
(
int
[]
nums
) {
int
sum
=
0
;
int
n
=
nums
.
length
;
for
(
int
num
:
nums
) {
sum
+=
num
;
}
int
arrSum
= (((
n
+
1
)) *
n
) /
2
;
if
(
arrSum
==
sum
)
return
0
;
else
return
arrSum
-
sum
;
}
}
Back
|
FazBrowse Home
|
New Git URL