首页 > 其他分享 >用Rust刷leetcode第八题

用Rust刷leetcode第八题

时间:2022-11-07 11:34:39浏览次数:48  
标签:ch whitespace numerical character 第八 result leetcode Rust matched


Problem

Implement ​​atoi​​​ which converts a string to an integer.
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.
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.
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.

If no valid conversion could be performed, a zero value is returned.

Note:

  • Only the space character ​​' '​​ is considered as whitespace character.
  • Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  2^31 ^− 1]. If the numerical value is out of the range of representable values, INT_MAX (2^31 ^− 1) or INT_MIN (−231) is returned.

Example

Example 1:

Input: “42”
Output: 42

Example 2:

Input: " -42"
Output: -42
Explanation: The first non-whitespace character is ‘-’, which is the minus sign.
  Then take as many numerical digits as possible, which gets 42.

Example 3:

Input: “4193 with words”
Output: 4193
Explanation: Conversion stops at digit ‘3’ as the next character is not a numerical digit.

Example 4:

Input: “words and 987”
Output: 0
Explanation: The first non-whitespace character is ‘w’, which is not a numerical
  digit or a +/- sign. Therefore no valid conversion could be performed.

Example 5:

Input: “-91283472332”
Output: -2147483648
Explanation: The number “-91283472332” is out of the range of a 32-bit signed integer.
  Thefore INT_MIN (−231) is returned.

Solution

impl Solution {
pub fn my_atoi(str: String) -> i32 {
let (min, max) = (-2_i64.pow(31), 2_i64.pow(31) - 1);
let mut result: i64 = 0;
let mut minus = false;
let mut matched = false;

for ch in str.chars().into_iter() {
if !matched {
match ch {
' ' => {},
'0'..='9' => {
matched = true;
result = result*10 + ch.to_digit(10).unwrap() as i64;
},
'-' => {
matched = true;
minus = true;
},
'+' => {
matched = true;
},
_ => return 0,
}
} else {
match ch {
'0'..='9' => {
result = result*10 + ch.to_digit(10).unwrap() as i64;
if result > max {
break;
}
},
_ => break,
}
}
}

if minus {
result = -result
}

if result > max {
return max as i32;
}

if result < min {
return min as i32;
}

return result as i32;
}
}


标签:ch,whitespace,numerical,character,第八,result,leetcode,Rust,matched
From: https://blog.51cto.com/u_15862521/5828354

相关文章

  • [leetcode每日一题]11.7
    816. 模糊坐标我们有一些二维坐标,如 ​​"(1,3)"​​ 或 ​​"(2,0.5)"​​,然后我们移除所有逗号,小数点和空格,得到一个字符串​​S​​。返回所有可能的原始字符串到......
  • 023 通过链表学Rust之非安全方式实现链表1
    介绍视频地址:https://www.bilibili.com/video/av78062009/相关源码:https://github.com/anonymousGiga/Rust-link-list链表定义我们重新定义链表如下:pubstructList<T>{......
  • 022 通过链表学Rust之为什么要非安全的单链表
    介绍视频地址:https://www.bilibili.com/video/av78062009/相关源码:https://github.com/anonymousGiga/Rust-link-list详细内容前面我们都是使用安全的Rust编程来实现链表,但......
  • 025 通过链表学Rust之使用栈实现双端队列
    介绍视频地址:https://www.bilibili.com/video/av78062009/相关源码:https://github.com/anonymousGiga/Rust-link-list详细内容本节我们使用栈来实现双端队列。实现栈栈的实......
  • 024 通过链表学Rust之非安全方式实现链表2
    介绍视频地址:https://www.bilibili.com/video/av78062009/相关源码:https://github.com/anonymousGiga/Rust-link-list详细内容本节实现剩余的迭代器、Drop等。IntoIter实现......
  • 用Rust实现一个多线程的web server
    在本文之前,我们用Rust实现一个单线程的webserver的例子,但是单线程的webserver不够高效,所以本篇文章就来实现一个多线程的例子。单线程webserver存在的问题请求只能串行处......
  • 用Rust创建一个简单的webserver
    目的本节的例子教大家用Rust语言创建一个简单的webserver程序。webserver中涉及到的两个主要协议是超文本传输协议(HypertextTransferProtocol,HTTP)和传输控制协议(Tran......
  • [LeetCode] 1678. Goal Parser Interpretation
    Youowna GoalParser thatcaninterpretastring command.The command consistsofanalphabetof "G", "()" and/or "(al)" insomeorder.TheGoalPar......
  • leetcode 54. 螺旋矩阵 js高效实现
    给你一个 m 行 n 列的矩阵 matrix ,请按照 顺时针螺旋顺序 ,返回矩阵中的所有元素。示例1:  输入:matrix=[[1,2,3],[4,5,6],[7,8,9]]输出:[1,2,3,6,9,8,7,4,5]......
  • LeetCode刷题记录.Day7
    有效的字母异位词题目链接242.有效的字母异位词-力扣(LeetCode)classSolution{public:boolisAnagram(strings,stringt){intrecord[26]={0};......