Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >string-to-integer-atoi ---> leetcode

string-to-integer-atoi ---> leetcode

作者头像
用户2436820
修改于 2023-09-20 10:24:13
修改于 2023-09-20 10:24:13
46700
代码可运行
举报
运行总次数:0
代码可运行

Runtime: 0 ms, faster than 100.00% of Rust online submissions for String to Integer (atoi). Memory Usage: 2.4 MB, less than 100.00% of Rust online submissions for String to Integer (atoi). Next challenges: string-to-integer-atoi 思想:状态机

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
pub fn my_atoi(str: String) -> i32 {
        let (i32_min, i32_max) = (-2_i64.pow(31), 2_i64.pow(31) - 1);
        let mut num_match = false;
        let mut result: i64 = 0;
        let mut minus = false;
        for ch in str.chars().into_iter() {
            if !num_match {
                match ch {
                    ' ' => {}
                    '0'...'9' => {
                        num_match = true;
                        result = result * 10 + ch.to_digit(10).unwrap() as i64;
                    }
                    '-' => {
                        num_match = true;
                        minus = true;
                    }
                    '+' => {
                        num_match = true;
                    }
                    _ => return 0,
                }
            } else {
                match ch {
                    '0'...'9' => {
                        result = result * 10 + ch.to_digit(10).unwrap() as i64;
                        if result > i32_max {
                            break;
                        }
                    }
                    _ => break,
                }
            }
        }
        result = if minus { -result } else { result };
        if result > i32_max {
            return i32_max as i32;
        }
        if result < i32_min {
            println!("cccc:{}", result);
            println!("cccc:{}", i32_min);

            return i32_min as i32;
        }
        return result as i32;
    }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
一起学Rust-实战leetcode(六)
题目截图来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/string-to-integer-atoi/
江湖安得便相忘
2019/10/08
7310
一起学Rust-实战leetcode(六)
C++版 - Leetcode 8: String to Integer (myAtoi,C库函数atoi模拟) (剑指offer 面试题49) 解题报告
提交网址: https://leetcode.com/problems/string-to-integer-atoi/
Enjoy233
2019/03/05
8410
String - 8. String to Integer (atoi)
Implement atoi which converts a string to an integer.
ppxai
2020/09/23
5140
Rust开发⼲货集(1)--迭代器与消费器
在 Rust 中,"转移所有权"(Ownership Transfer)是一种核心概念,它涉及变量和数据的所有权从一个实体转移到另一个实体。这种机制帮助 Rust 在编译时期管理内存安全,避免悬挂指针和内存泄漏等问题。
fliter
2024/01/09
1950
【LeetCode】8. 字符串转换整数 (atoi)
当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。
韩旭051
2020/06/23
5500
【LeetCode】8. 字符串转换整数 (atoi)
Python 版 LeetCode 刷题笔记 #8 字符串转换整数 (atoi)
今天趁着有兴致多刷一道,中等难度的题,这种题考虑的情况比较多,写完之后只能靠提交了看测试结果,针对返回的特殊测试用例来完善代码。大概提交了四次,我的代码通过了。题目挺长,走起~
TTTEED
2020/07/08
1K0
leetcode 8 String to Integer (atoi)
String to Integer (atoi)Total Accepted:52232 Total Submissions:401038 My Submissions
流川疯
2019/01/18
5230
Leetcode String to Integer (atoi)
Implement atoi which converts a string to an integer.
2018/09/04
4650
L_00008_MyAtoi:Atoi经典解法
https://leetcode-cn.com/problems/string-to-integer-atoi/
mingjie
2022/05/12
2010
Leetcode-8.字符串转换整数 (atoi)
首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。接下来的转化规则如下:
悠扬前奏
2020/05/18
7010
一起学Rust-实战leetcode(五)
假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−2^31, 2^31 − 1]。请根据这个假设,如果反转后整数溢出那么就返回 0。
江湖安得便相忘
2019/09/25
9950
一起学Rust-实战leetcode(五)
【leetcode】String to Integer (atoi)
Implement atoi to convert a string to an integer.
阳光岛主
2019/02/19
6130
008. 字符串转换整数 (atoi) | Leetcode题解
首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。接下来的转化规则如下:
苏南
2020/12/16
5800
008. 字符串转换整数 (atoi) | Leetcode题解
太优雅了!Rust 200 行代码实现表达式解析
表达式解析、计算是一种基本和常见的任务,例如最常见的算术表达式,计算的方法有很多,比如逆波兰表达式、LL、LR 算法等等。
roseduan
2024/04/30
1960
太优雅了!Rust 200 行代码实现表达式解析
String to Integer (atoi)
Implement atoi to convert a string to an integer.
leehao
2025/02/10
570
2023-02-14:魔物了占领若干据点,这些据点被若干条道路相连接, roads[i] = [x, y] 表示编号 x、y 的两个据点通过一条道路连接。 现在
输入:cost = 1,2,3,4,5,6,roads = [0,1,0,2,1,3,2,3,1,2,2,4,2,5]。
福大大架构师每日一题
2023/02/14
2540
2023-02-14:魔物了占领若干据点,这些据点被若干条道路相连接, roads[i] = [x, y] 表示编号 x、y 的两个据点通过一条道路连接。 现在
Rust vs Go:常用语法对比(二)
题图来自 Calling Rust code from Go - the Gambiarra way[1]
fliter
2023/09/05
3810
Rust vs Go:常用语法对比(二)
Rustlings练习-vec、String、hashmap、error_handling
用直接声明是一个[i32,n]的类型,用vec!()宏可以声明一个Vec<T>类型的变量.
用户7267083
2022/12/08
8560
一文快速理解Rust语言扩展trait
科学无非就是在自然界的多样性中寻求统一性(或者更确切地说,是在我们经验的多样性中寻求统一性)。用 Coleridge 的话说,诗歌、绘画、艺术,同样是在多样性中寻求统一性
草帽lufei
2024/05/08
1490
一文快速理解Rust语言扩展trait
leetcode: 8. String to Integer (atoi)
Problem # Implement atoi to convert a string to an integer. # # 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. # # Notes: # It is intended for
JNingWei
2018/09/28
3820
相关推荐
一起学Rust-实战leetcode(六)
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验