Repository navigation
Expand file tree
/
Copy pathStringToInteger.java
More file actions
154 lines (120 loc) · 4.75 KB
/
Copy pathStringToInteger.java
File metadata and controls
154 lines (120 loc) · 4.75 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.abn.dsalgos.problems;
import org.testng.Assert;
/*
https://leetcode.com/problems/string-to-integer-atoi/
Input: s = "42"
Output: 42
Input: s = " -42"
Output: -42
Input: s = "4193 with words"
Output: 4193
Input: s = "words and 987"
Output: 0
Input: s = "-91283472332"
Output: -2147483648
*/
public class StringToInteger {
int maxValue = Integer.MAX_VALUE;
int minValue = Integer.MIN_VALUE;
public int myAtoi(String s1) {
String s = s1.trim();
StringBuilder str = new StringBuilder();
if (s.length() == 0) {
return 0;
}
char firstNonSpaceChar = s.charAt(0);
int multiplier = 1;
int startPointer = 0;
// Handle any char other than digits in the beginning after whitespace trim.
if (Character.isLetter(firstNonSpaceChar) || firstNonSpaceChar == '.') {
return 0;
}
//Handle positive number and negative signs
if (firstNonSpaceChar == '+') {
multiplier = 1;
startPointer = 1;
} else if (firstNonSpaceChar == '-') {
multiplier = -1;
startPointer = 1;
}
// Handle single length string with '+' / '-' only.
if (s.length() == 1 && startPointer == 1) {
return 0;
}
while (s.charAt(startPointer) == '0') {
startPointer++;
if (startPointer == s.length()) {
return 0;
}
}
for (int i = startPointer; i < s.length(); i++) {
char currChar = s.charAt(i);
if (!Character.isDigit(currChar)) {
break;
}
str.append(currChar);
}
return convertToInt(str, multiplier);
}
private int convertToInt(StringBuilder intAsString, int multiplier) {
if (multiplier == 1) {
if (isMoreThanLimit(intAsString, maxValue)) {
return maxValue;
}
return safeConvert(intAsString);
} else {
if (isMoreThanLimit(intAsString, minValue)) {
return minValue;
}
return -1 * safeConvert(intAsString);
}
}
private int safeConvert(StringBuilder intAsString) {
int result = 0;
for (int i = 0; i < intAsString.length(); i++) {
result = result * 10 + intAsString.charAt(i) - '0';
}
return result;
}
//Check whether string is more than integer limit or not if string has same length of digits as limit
//Testcase : 16
private boolean isMoreThanLimit(StringBuilder intAsString, int limit) {
String limitAsString = "" + limit;
if (limitAsString.charAt(0) == '-') {
limitAsString = limitAsString.substring(1);
}
if (intAsString.length() == limitAsString.length()) {
for (int i = 0; i < intAsString.length(); i++) {
int currDigitInString = intAsString.charAt(i) - '0';
int currDigitInLimit = limitAsString.charAt(i) - '0';
if (currDigitInString < currDigitInLimit) {
return false;
}
if (currDigitInString > currDigitInLimit) {
return true;
}
}
return true;
}
return intAsString.length() > limitAsString.length();
}
public static void main(String[] args) {
Assert.assertEquals(new StringToInteger().myAtoi("435"), 435);
Assert.assertEquals(new StringToInteger().myAtoi("word and 943"), 0);
Assert.assertEquals(new StringToInteger().myAtoi("-91283472332"), -2147483648);
Assert.assertEquals(new StringToInteger().myAtoi("3.45"), 3);
Assert.assertEquals(new StringToInteger().myAtoi("-109394"), -109394);
Assert.assertEquals(new StringToInteger().myAtoi("-9223372036854775809"), -2147483648);
Assert.assertEquals(new StringToInteger().myAtoi("9223372036854775808"), 2147483647);
Assert.assertEquals(new StringToInteger().myAtoi(" 45"), 45);
Assert.assertEquals(new StringToInteger().myAtoi("18446744073709551617"), 2147483647);
Assert.assertEquals(new StringToInteger().myAtoi(" 0000000000012345678"), 12345678);
Assert.assertEquals(new StringToInteger().myAtoi("+"), 0);
Assert.assertEquals(new StringToInteger().myAtoi("-"), 0);
Assert.assertEquals(new StringToInteger().myAtoi(" 000 "), 0);
Assert.assertEquals(new StringToInteger().myAtoi(" 00011 "), 11);
Assert.assertEquals(new StringToInteger().myAtoi("1095502006p8"), 1095502006);
Assert.assertEquals(new StringToInteger().myAtoi("2147483800"), 2147483647);
Assert.assertEquals(new StringToInteger().myAtoi(" "), 0);
}
}