-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay27.java
More file actions
65 lines (54 loc) · 2.19 KB
/
Day27.java
File metadata and controls
65 lines (54 loc) · 2.19 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*Implement Atoi
Given a string s, the objective is to convert it into integer format without utilizing any built-in functions. Refer the below steps to know about atoi() function.
Cases for atoi() conversion:
Skip any leading whitespaces.
Check for a sign (‘+’ or ‘-‘), default to positive if no sign is present.
Read the integer by ignoring leading zeros until a non-digit character is encountered or end of the string is reached. If no digits are present, return 0.
If the integer is greater than 231 – 1, then return 231 – 1 and if the integer is smaller than -231, then return -231.
Examples:
Input: s = "-123"
Output: -123
Explanation: It is possible to convert -123 into an integer so we returned in the form of an integer
Input: s = " -"
Output: 0
Explanation: No digits are present, therefore the returned answer is 0.
Input: s = " 1231231231311133"
Output: 2147483647
Explanation: The converted number will be greater than 231 – 1, therefore print 231 – 1 = 2147483647.
Input: s = "-999999999999"
Output: -2147483648
Explanation: The converted number is smaller than -231, therefore print -231 = -2147483648.
Input: s = " -0012gfg4"
Output: -12
Explanation: After ignoring leading zeros nothing is read after -12 as a non-digit character ‘g’ was encountered. */
/*class Solution {
public int myAtoi(String s) {
int i = 0, n = s.length();
int sign = 1;
long result = 0;
// 1. Skip leading spaces
while (i < n && s.charAt(i) == ' ') {
i++;
}
// 2. Sign check
if (i < n && (s.charAt(i) == '+' || s.charAt(i) == '-')) {
sign = (s.charAt(i) == '-') ? -1 : 1;
i++;
}
// 3. Digit processing with SAFE overflow check
while (i < n && s.charAt(i) >= '0' && s.charAt(i) <= '9') {
int digit = s.charAt(i) - '0';
// Overflow check BEFORE update
if (result > (Integer.MAX_VALUE - digit) / 10) {
return (sign == 1) ? Integer.MAX_VALUE : Integer.MIN_VALUE;
}
result = result * 10 + digit;
i++;
}
return (int)(sign * result);
}
}
*/
/*⏱ Complexity
Time: O(n)
Space: O(1) */