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

Commit 548ab66

Browse files
committed
solve aylei#8
1 parent a11870d commit 548ab66

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed

‎src/lib.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ mod n0004_median_of_two_sorted_arrays;
55
mod n0005_longest_palindromic_substring;
66
mod n0006_zigzag_conversion;
77
mod n0007_reverse_integer;
8+
mod n0008_string_to_integer_atoi;
9+
mod n0009_palindrome_number;

‎src/n0008_string_to_integer_atoi.rs‎

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
* [8] String to Integer (atoi)
3+
*
4+
* Implement <code><span>atoi</span></code> which converts a string to an integer.
5+
*
6+
* 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.
7+
*
8+
* 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.
9+
*
10+
* 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.
11+
*
12+
* If no valid conversion could be performed, a zero value is returned.
13+
*
14+
* Note:
15+
*
16+
* <ul>
17+
* <li>Only the space character <code>&#39; &#39;</code> is considered as whitespace character.</li>
18+
* <li>Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [-2^31, 2^31 - 1]. If the numerical value is out of the range of representable values, INT_MAX (2^31 - 1) or INT_MIN (-2^31) is returned.</li>
19+
* </ul>
20+
*
21+
* Example 1:
22+
*
23+
*
24+
* Input: "42"
25+
* Output: 42
26+
*
27+
*
28+
* Example 2:
29+
*
30+
*
31+
* Input: " -42"
32+
* Output: -42
33+
* Explanation: The first non-whitespace character is &#39;-&#39;, which is the minus sign.
34+
* Then take as many numerical digits as possible, which gets 42.
35+
*
36+
*
37+
* Example 3:
38+
*
39+
*
40+
* Input: "4193 with words"
41+
* Output: 4193
42+
* Explanation: Conversion stops at digit &#39;3&#39; as the next character is not a numerical digit.
43+
*
44+
*
45+
* Example 4:
46+
*
47+
*
48+
* Input: "words and 987"
49+
* Output: 0
50+
* Explanation: The first non-whitespace character is &#39;w&#39;, which is not a numerical
51+
* digit or a +/- sign. Therefore no valid conversion could be performed.
52+
*
53+
* Example 5:
54+
*
55+
*
56+
* Input: "-91283472332"
57+
* Output: -2147483648
58+
* Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.
59+
* Thefore INT_MIN (-2^31) is returned.
60+
*
61+
*/
62+
pub struct Solution {}
63+
64+
// submission codes start here
65+
66+
impl Solution {
67+
pub fn my_atoi(input: String) -> i32 {
68+
let (i32_min, i32_max) = (-2_i64.pow(31), 2_i64.pow(31) - 1);
69+
let mut result: i64 = 0;
70+
let mut minus = false;
71+
// simple state machine
72+
let mut num_matched = false;
73+
for ch in input.chars().into_iter() {
74+
if !num_matched {
75+
match ch {
76+
' ' => {},
77+
'0'...'9' => {
78+
num_matched = true;
79+
result = result * 10 + ch.to_digit(10).unwrap() as i64;
80+
},
81+
'-' => { num_matched = true; minus = true; }
82+
'+' => { num_matched = true; }
83+
_ => return 0
84+
}
85+
} else {
86+
match ch {
87+
'0'...'9' => {
88+
result = result * 10 + ch.to_digit(10).unwrap() as i64;
89+
if result > i32_max { break }
90+
},
91+
_ => break
92+
}
93+
}
94+
}
95+
result = if minus { -result } else { result };
96+
if result > i32_max { return i32_max as i32; }
97+
if result < i32_min { return i32_min as i32; }
98+
return result as i32;
99+
}
100+
}
101+
102+
// submission codes end
103+
104+
#[cfg(test)]
105+
mod tests {
106+
use super::*;
107+
108+
#[test]
109+
fn test_8() {
110+
assert_eq!(Solution::my_atoi("aa".to_string()), 0);
111+
assert_eq!(Solution::my_atoi("-91283472332".to_string()), -2147483648);
112+
assert_eq!(Solution::my_atoi("words and 987".to_string()), 0);
113+
assert_eq!(Solution::my_atoi("4193 with words".to_string()), 4193);
114+
assert_eq!(Solution::my_atoi("42".to_string()), 42);
115+
assert_eq!(Solution::my_atoi("004193333".to_string()), 4193333);
116+
}
117+
}

‎src/n0009_palindrome_number.rs‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* [9] Palindrome Number
3+
*
4+
* Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
5+
*
6+
* Example 1:
7+
*
8+
*
9+
* Input: 121
10+
* Output: true
11+
*
12+
*
13+
* Example 2:
14+
*
15+
*
16+
* Input: -121
17+
* Output: false
18+
* Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
19+
*
20+
*
21+
* Example 3:
22+
*
23+
*
24+
* Input: 10
25+
* Output: false
26+
* Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
27+
*
28+
*
29+
* Follow up:
30+
*
31+
* Coud you solve it without converting the integer to a string?
32+
*
33+
*/
34+
pub struct Solution {}
35+
36+
// submission codes start here
37+
38+
impl Solution {
39+
pub fn is_palindrome(x: i32) -> bool {
40+
41+
}
42+
}
43+
44+
// submission codes end
45+
46+
#[cfg(test)]
47+
mod tests {
48+
use super::*;
49+
50+
#[test]
51+
fn test_9() {
52+
}
53+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /