FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/java/415_Add_Strings.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
/
415_Add_Strings.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
25 lines (22 loc) · 877 Bytes
Breadcrumbs
leetcode
/
java
/
415_Add_Strings.java
Copy path
File metadata and controls
25 lines (22 loc) · 877 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/third-maximum-number/
Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return
the maximum number.
Iterate over stings from end to front or carry. If either sting has ended, use zero. Add digits plus carry and mod 10.
Reverse result.
Time - O(n)
Space - O(n)
*/
public
class
Solution
{
public
String
addStrings
(
String
num1
,
String
num2
) {
StringBuilder
result
=
new
StringBuilder
();
int
carry
=
0
;
for
(
int
i
=
num1
.
length
() -
1
,
j
=
num2
.
length
() -
1
;
i
>=
0
||
j
>=
0
||
carry
==
1
;
i
--,
j
--) {
int
x
=
i
<
0
?
0
:
num1
.
charAt
(
i
) -
'0'
;
int
y
=
j
<
0
?
0
:
num2
.
charAt
(
j
) -
'0'
;
result
.
append
((
x
+
y
+
carry
) %
10
);
carry
= (
x
+
y
+
carry
) /
10
;
}
return
result
.
reverse
().
toString
();
}
}
Back
|
FazBrowse Home
|
New Git URL