GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/java/008_String_to_Integer(atoi).java at master · gsty/leetcode

Expand file tree

/

008_String_to_Integer(atoi).java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
25 lines (25 loc) · 695 Bytes
/

008_String_to_Integer(atoi).java

Copy path

File metadata and controls

25 lines (25 loc) · 695 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
public class Solution {
// example in leetcode book
private static final int maxDiv10 = Integer.MAX_VALUE / 10;
public int myAtoi(String str) {
int i = 0, n = str.length();
while (i < n && Character.isWhitespace(str.charAt(i)))
i++;
int sign = 1;
if (i < n && str.charAt(i) == '+')
i++;
else if (i < n && str.charAt(i) == '-') {
sign = -1;
i++;
}
int num = 0;
while (i < n && Character.isDigit(str.charAt(i))) {
int digit = Character.getNumericValue(str.charAt(i));
if (num > maxDiv10 || num == maxDiv10 && digit >= 8)
return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
num = num * 10 + digit;
i++;
}
return sign * num;
}
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.