Skip to content

Commit e60f420

Browse files
two drafts
1 parent b97047d commit e60f420

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

EASY/src/easy/StringToInteger.java

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package easy;
2+
/**8. String to Integer (atoi) QuestionEditorial Solution My Submissions
3+
Total Accepted: 115114
4+
Total Submissions: 839893
5+
Difficulty: Easy
6+
Implement atoi to convert a string to an integer.
7+
8+
Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.
9+
10+
Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.*/
11+
12+
/**Requirements for atoi:
13+
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
14+
15+
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
16+
17+
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
18+
19+
If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.*/
20+
public class StringToInteger {
21+
public int myAtoi(String str) {
22+
//case 1: str is greater than Integer.MAX_VALUE, return Integer.MAX_VALUE as the question states it
23+
24+
//case 2: str is smaller than Integer.MIN_VALUE, return Integer.MIN_VALUE as the question states it
25+
26+
//case 3: str contains non-numeric values
27+
28+
//case 4: there're many leading whitespace characters which we'll have to ignore
29+
30+
//case 5: when finding the first non-whitespace character, it could possibly be a '+' or '-' sign, after that, we parse all the consecutive numbers
31+
return 0;
32+
}
33+
}

MEDIUM/src/medium/RandomizedSet.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
import java.util.Random;
88
import java.util.Set;
99

10-
/**This solution get AC'ed. Although it's not really doing random*/
10+
/**This solution get AC'ed. Although it's not really doing random 8/4/2016
11+
* Now, they've updated the test case and also the question description: Each element must have the same probability of being returned.*/
1112
public class RandomizedSet {
1213

1314
Set<Integer> set;
@@ -27,7 +28,7 @@ public boolean insert(int val) {
2728
}
2829

2930
/** Deletes a value from the set. Returns true if the set contained the specified element. */
30-
public boolean delete(int val) {
31+
public boolean remove(int val) {
3132
return set.remove(val);
3233
}
3334

@@ -63,8 +64,6 @@ public static void main(String...args){
6364
* int param_3 = obj.getRandom();
6465
*/
6566

66-
/**TODO: submit this solution on OJ later, it's throwing cannot find symbol remove() method on line 16,
67-
* this is an OJ bug that people reported on Discuss, submit it later, see if it can get AC'ed.*/
6867
class RandomizedSet_2nd_solution {
6968

7069
Map<Integer, Integer> forwardMap;//key is auto increment index, value if the inserted val
@@ -91,7 +90,7 @@ public boolean insert(int val) {
9190
}
9291

9392
/** Deletes a value from the set. Returns true if the set contained the specified element. */
94-
public boolean delete(int val) {
93+
public boolean remove(int val) {
9594
if(forwardMap.containsValue(val)){
9695
int key = reverseMap.get(val);
9796
reverseMap.remove(val);

0 commit comments

Comments
 (0)