当前位置: 首页 > news >正文

LeetCode -- String to Integer (atoi)

题目描述:
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 this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.


就是完成字符串转整数的操作。
本题的复杂点在于情况很多需要考虑:
1.可能是负数。
2.首字母为+
3.跳过开始的'0'
4.超大整数(使用double来解析)


public class Solution {
    public int MyAtoi(string str) {
        if(string.IsNullOrWhiteSpace(str)){
            return 0;
        }
        str = str.Trim();
		
        var digits = "0123456789";
        
        if(str.Length > 1 && !digits.Contains(str[0]) && !digits.Contains(str[1])){
            return 0;
        }
        
        var s1 = "";
		var c = 0;
        if(str[0] == '-'){
            s1 = "-";
			c++;
        }
        
        // skip '+'
		if(str[0] == '+'){
			c++;
		}
		
		//skip all starting '0' 
        while(c < str.Length && str[c] == '0'){
            c ++;
        }
        
        // start from non-zero number , collect all valid digit char
        while(c < str.Length && digits.Contains(str[c])){
            s1 += str[c];
            c++;
        }
		
        double v;
        if(double.TryParse(s1, out v)){
            if(v > int.MaxValue){
				return int.MaxValue;
			}
			if(v < int.MinValue){
				return int.MinValue;
			}
			return (int)v;
        }
        return 0;
        
    }
}


相关文章:

  • JavaScript 读写文件
  • LeetCode -- Subsets
  • 也谈实体验证(Entity Validation)
  • LeetCode -- Symmetric Tree
  • 越狱 第五季 Microsoft复活
  • LeetCode -- Trap Water Rain
  • leetcode -- Unique Binary Search Trees II
  • WCF中神秘的“8731“端口和“Design_Time_Addresses”
  • LeetCode -- Unique Paths II
  • LeetCode -- Valid Anagram
  • 金旭亮博客之“分布式系统技术”资源主页
  • LeetCode -- Valid Palindrome
  • 中国联通正式公布3G资费标准
  • LeetCode -- Valid Parentheses
  • 手机无线连接(GSM/GPRS)方式
  • 2017届校招提前批面试回顾
  • Mocha测试初探
  • Unix命令
  • vue-cli在webpack的配置文件探究
  • 如何进阶一名有竞争力的程序员?
  • 实习面试笔记
  • 消息队列系列二(IOT中消息队列的应用)
  • 用quicker-worker.js轻松跑一个大数据遍历
  • 正则表达式-基础知识Review
  • #Z2294. 打印树的直径
  • $(selector).each()和$.each()的区别
  • (1)(1.19) TeraRanger One/EVO测距仪
  • (1/2) 为了理解 UWP 的启动流程,我从零开始创建了一个 UWP 程序
  • (C++20) consteval立即函数
  • (二)什么是Vite——Vite 和 Webpack 区别(冷启动)
  • (黑马出品_高级篇_01)SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式
  • (三)mysql_MYSQL(三)
  • (四)TensorRT | 基于 GPU 端的 Python 推理
  • (一)Java算法:二分查找
  • (转)Unity3DUnity3D在android下调试
  • (转)原始图像数据和PDF中的图像数据
  • **PHP二维数组遍历时同时赋值
  • .Net Remoting(分离服务程序实现) - Part.3
  • .net websocket 获取http登录的用户_如何解密浏览器的登录密码?获取浏览器内用户信息?...
  • .NET 反射的使用
  • 。Net下Windows服务程序开发疑惑
  • /使用匿名内部类来复写Handler当中的handlerMessage()方法
  • @Transactional 详解
  • [AAuto]给百宝箱增加娱乐功能
  • [Android]How to use FFmpeg to decode Android f...
  • [C]整形提升(转载)
  • [dts]Device Tree机制
  • [go] 迭代器模式
  • [GYCTF2020]Ez_Express
  • [HITCON 2017]SSRFme perl语言的 GET open file 造成rce
  • [IE9] IE9 Beta崩溃问题解决方案
  • [JS]数据类型
  • [LeetCode 687]最长同值路径
  • [LeetCode]剑指 Offer 40. 最小的k个数
  • [Oh My C++ Diary]类继承和类组合(内嵌类)初始化的不同