mirror of
https://github.com/Xevion/leetcode.git
synced 2025-12-06 03:15:28 -06:00
string-to-integer-atoi solution java
This commit is contained in:
32
string-to-integer-atoi/Solution.java
Normal file
32
string-to-integer-atoi/Solution.java
Normal file
@@ -0,0 +1,32 @@
|
||||
// Accepted
|
||||
// Runtime: 1 ms
|
||||
// Memory Usage: 38.9 MB
|
||||
// Submitted: January 14th, 2021
|
||||
|
||||
class Solution {
|
||||
public int myAtoi(String s) {
|
||||
int sign = 1;
|
||||
int i = 0;
|
||||
int result = 0;
|
||||
if (s.length() == 0) return 0;
|
||||
|
||||
// Skip through whitespace
|
||||
while (i < s.length() && s.charAt(i) == ' ')
|
||||
i++;
|
||||
|
||||
// If there is a sign present, extract it
|
||||
if (i < s.length() && (s.charAt(i) == '-' || s.charAt(i) == '+'))
|
||||
if (s.charAt(i++) == '-')
|
||||
sign = -1;
|
||||
|
||||
while (i < s.length() && Character.isDigit(s.charAt(i))) {
|
||||
if (result > Integer.MAX_VALUE / 10 || (result == Integer.MAX_VALUE / 10 && s.charAt(i) - '0' > Integer.MAX_VALUE % 10)) {
|
||||
return (sign == 1) ? Integer.MAX_VALUE : Integer.MIN_VALUE;
|
||||
}
|
||||
|
||||
result = (result * 10) + (s.charAt(i++) - '0');
|
||||
}
|
||||
|
||||
return result * sign;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user