Skip to content

Commit 0f34e82

Browse files
committed
😄update my solutions
1 parent 8d865ea commit 0f34e82

File tree

5 files changed

+55
-3
lines changed

5 files changed

+55
-3
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<p align="center"><img width="300" src="https://raw.githubusercontent.com/Ma63d/leetcode-spider/master/img/site-logo.png"></p>
22
<p align="center">
3-
<img src="https://img.shields.io/badge/Solved/Total(Locked)-14/573(107)-green.svg?style=flat-square" alt="">
3+
<img src="https://img.shields.io/badge/Solved/Total(Locked)-15/573(107)-green.svg?style=flat-square" alt="">
44
<img src="https://img.shields.io/badge/Hard-0-blue.svg?style=flat-square" alt="">
5-
<img src="https://img.shields.io/badge/Medium-5-blue.svg?style=flat-square" alt="">
5+
<img src="https://img.shields.io/badge/Medium-6-blue.svg?style=flat-square" alt="">
66
<img src="https://img.shields.io/badge/Easy-9-blue.svg?style=flat-square" alt="">
77
</p>
88
<h3 align="center">My leetcode solutions</h3>
@@ -22,6 +22,7 @@
2222
|002|[add-two-numbers](https://leetcode.com/problems/add-two-numbers/)| [c](.&#x2F;solutions&#x2F;002.add-two-numbers&#x2F;add-two-numbers.c)|Medium|27.64%||
2323
|003|[longest-substring-without-repeating-characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/)| [javascript](.&#x2F;solutions&#x2F;003.longest-substring-without-repeating-characters&#x2F;longest-substring-without-repeating-characters.js)|Medium|24.27%||
2424
|007|[reverse-integer](https://leetcode.com/problems/reverse-integer/)| [c](.&#x2F;solutions&#x2F;007.reverse-integer&#x2F;reverse-integer.c)|Easy|24.32%||
25+
|008|[string-to-integer-atoi](https://leetcode.com/problems/string-to-integer-atoi/)| [c++](.&#x2F;solutions&#x2F;008.string-to-integer-atoi&#x2F;string-to-integer-atoi.cpp)|Medium|13.96%||
2526
|009|[palindrome-number](https://leetcode.com/problems/palindrome-number/)| [python](.&#x2F;solutions&#x2F;009.palindrome-number&#x2F;palindrome-number.py)|Easy|35.20%||
2627
|013|[roman-to-integer](https://leetcode.com/problems/roman-to-integer/)| [python](.&#x2F;solutions&#x2F;013.roman-to-integer&#x2F;roman-to-integer.py)|Easy|45.52%||
2728
|014|[longest-common-prefix](https://leetcode.com/problems/longest-common-prefix/)| [c++](.&#x2F;solutions&#x2F;014.longest-common-prefix&#x2F;longest-common-prefix.cpp)|Easy|31.44%||
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"c++":"class Solution {\npublic:\n int myAtoi(string str) {\n int flag = 1;\n int result = 0;\n if (str.size() == 0) {\n return 0;\n }\n int i = 0;\n while(i < str.size() && str[i] == ' ') {\n i++;\n continue;\n }\n if (str[i] == '+' || str[i] == '-') {\n flag = str[i++] == '+' ? 1 : -1;\n }\n while(i < str.size() && str[i] >= '0' && str[i] <= '9') {\n if (result > INT_MAX / 10 || (result == INT_MAX / 10 && str[i] - '0' > 7)) {\n return (flag == 1) ? INT_MAX : INT_MIN;\n }\n result = 10 * result + (str[i++] - '0');\n }\n return result * flag;\n }\n};"}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Implement atoi to convert a string to an integer.
2+
3+
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.
4+
5+
6+
Notes:
7+
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.
8+
9+
10+
Update (2015-02-10):
11+
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.
12+
13+
14+
spoilers alert... click to show requirements for atoi.
15+
16+
Requirements for atoi:
17+
18+
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.
19+
20+
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.
21+
22+
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.
23+
24+
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.
25+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
int myAtoi(string str) {
4+
int flag = 1;
5+
int result = 0;
6+
if (str.size() == 0) {
7+
return 0;
8+
}
9+
int i = 0;
10+
while(i < str.size() && str[i] == ' ') {
11+
i++;
12+
continue;
13+
}
14+
if (str[i] == '+' || str[i] == '-') {
15+
flag = str[i++] == '+' ? 1 : -1;
16+
}
17+
while(i < str.size() && str[i] >= '0' && str[i] <= '9') {
18+
if (result > INT_MAX / 10 || (result == INT_MAX / 10 && str[i] - '0' > 7)) {
19+
return (flag == 1) ? INT_MAX : INT_MIN;
20+
}
21+
result = 10 * result + (str[i++] - '0');
22+
}
23+
return result * flag;
24+
}
25+
};

solutions/result.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"1":{"id":1,"level":1,"title":"two-sum","paidOnly":false,"acceptance":"34.13%","language":["python"],"generated":true},"2":{"id":2,"level":2,"title":"add-two-numbers","paidOnly":false,"acceptance":"27.64%","language":["c"],"generated":true},"3":{"id":3,"level":2,"title":"longest-substring-without-repeating-characters","paidOnly":false,"acceptance":"24.27%","language":["javascript"],"generated":true},"7":{"id":7,"level":1,"title":"reverse-integer","paidOnly":false,"acceptance":"24.32%","language":["c"],"generated":true},"9":{"id":9,"level":1,"title":"palindrome-number","paidOnly":false,"acceptance":"35.20%","language":["python"],"generated":true},"13":{"id":13,"level":1,"title":"roman-to-integer","paidOnly":false,"acceptance":"45.52%","language":["python"],"generated":true},"14":{"id":14,"level":1,"title":"longest-common-prefix","paidOnly":false,"acceptance":"31.44%","language":["c++"],"generated":true},"19":{"id":19,"level":2,"title":"remove-nth-node-from-end-of-list","paidOnly":false,"acceptance":"33.34%","language":["c"],"generated":true},"26":{"id":26,"level":1,"title":"remove-duplicates-from-sorted-array","paidOnly":false,"acceptance":"35.50%","language":["c"],"generated":true},"27":{"id":27,"level":1,"title":"remove-element","paidOnly":false,"acceptance":"38.84%","language":["c++"],"generated":true},"28":{"id":28,"level":1,"title":"implement-strstr","paidOnly":false,"acceptance":"27.97%","language":["c++"],"generated":true},"35":{"id":35,"level":1,"title":"search-insert-position","paidOnly":false,"acceptance":"39.66%","language":["c"],"generated":true},"56":{"id":56,"level":2,"title":"merge-intervals","paidOnly":false,"acceptance":"29.89%","language":["javascript"],"generated":true},"61":{"id":61,"level":2,"title":"rotate-list","paidOnly":false,"acceptance":"24.34%","language":["c"],"generated":true},"lastUpdatedTime":"2017-07-28","total":573,"solved":14,"locked":107}
1+
{"1":{"id":1,"level":1,"title":"two-sum","paidOnly":false,"acceptance":"34.13%","language":["python"],"generated":true},"2":{"id":2,"level":2,"title":"add-two-numbers","paidOnly":false,"acceptance":"27.64%","language":["c"],"generated":true},"3":{"id":3,"level":2,"title":"longest-substring-without-repeating-characters","paidOnly":false,"acceptance":"24.27%","language":["javascript"],"generated":true},"7":{"id":7,"level":1,"title":"reverse-integer","paidOnly":false,"acceptance":"24.32%","language":["c"],"generated":true},"8":{"id":8,"level":2,"title":"string-to-integer-atoi","paidOnly":false,"acceptance":"13.96%","language":["c++"],"generated":true},"9":{"id":9,"level":1,"title":"palindrome-number","paidOnly":false,"acceptance":"35.20%","language":["python"],"generated":true},"13":{"id":13,"level":1,"title":"roman-to-integer","paidOnly":false,"acceptance":"45.52%","language":["python"],"generated":true},"14":{"id":14,"level":1,"title":"longest-common-prefix","paidOnly":false,"acceptance":"31.44%","language":["c++"],"generated":true},"19":{"id":19,"level":2,"title":"remove-nth-node-from-end-of-list","paidOnly":false,"acceptance":"33.34%","language":["c"],"generated":true},"26":{"id":26,"level":1,"title":"remove-duplicates-from-sorted-array","paidOnly":false,"acceptance":"35.50%","language":["c"],"generated":true},"27":{"id":27,"level":1,"title":"remove-element","paidOnly":false,"acceptance":"38.84%","language":["c++"],"generated":true},"28":{"id":28,"level":1,"title":"implement-strstr","paidOnly":false,"acceptance":"27.97%","language":["c++"],"generated":true},"35":{"id":35,"level":1,"title":"search-insert-position","paidOnly":false,"acceptance":"39.66%","language":["c"],"generated":true},"56":{"id":56,"level":2,"title":"merge-intervals","paidOnly":false,"acceptance":"29.89%","language":["javascript"],"generated":true},"61":{"id":61,"level":2,"title":"rotate-list","paidOnly":false,"acceptance":"24.34%","language":["c"],"generated":true},"lastUpdatedTime":"2017-07-28","total":573,"solved":15,"locked":107}

0 commit comments

Comments
 (0)