Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite60f420

Browse files
two drafts
1 parentb97047d commite60f420

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

‎EASY/src/easy/StringToInteger.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
packageeasy;
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+
publicclassStringToInteger {
21+
publicintmyAtoi(Stringstr) {
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+
return0;
32+
}
33+
}

‎MEDIUM/src/medium/RandomizedSet.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
importjava.util.Random;
88
importjava.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
publicclassRandomizedSet {
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-
publicbooleandelete(intval) {
31+
publicbooleanremove(intval) {
3132
returnset.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
classRandomizedSet_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-
publicbooleandelete(intval) {
93+
publicbooleanremove(intval) {
9594
if(forwardMap.containsValue(val)){
9695
intkey =reverseMap.get(val);
9796
reverseMap.remove(val);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp