LeeCode-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.
class Solution {
public:
int myAtoi(string str) {
long long cur=;
int num=,i=;
int flag1=,flag2=;
while(str[i]!='\0' && str[i]==' ') i++;
if(str[i]=='-') flag1++,i++;
else if(str[i]=='+') flag2++,i++;
for(; str[i]!='\0'; i++)
{
if(str[i]>='' && str[i]<='')
{
if(flag1==)
{
cur=cur*-(str[i]-'');
if(cur<-) return -;
}
else if(flag1==) cur=-str[i]+'',flag1++;
else
{
cur=cur*+(str[i]-'');
if(cur>) return ;
}
}
else break;
}
num=(int)cur;
return num;
}
};
LeeCode-String to Integer (atoi)的更多相关文章
- 【leetcode】String to Integer (atoi)
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- leetcode第八题 String to Integer (atoi) (java)
String to Integer (atoi) time=272ms accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...
- leetcode day6 -- String to Integer (atoi) && Best Time to Buy and Sell Stock I II III
1. String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...
- String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )
String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...
- Kotlin实现LeetCode算法题之String to Integer (atoi)
题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...
- LeetCode--No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
- leetcode-algorithms-8 String to Integer (atoi)
leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...
- LeetCode: String to Integer (atoi) 解题报告
String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...
- 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
随机推荐
- 《UNIX网络编程》之多客户连接服务端,可重用套接字对
该网络编程之客户端与服务端程序模板支持: 1. 多客户端同时连接服务端,即服务程序可以同时为多个客户端服务: 2. 服务端支持套接字对重用,即即使处于TIME_WAIT状态,仍可支持服务端重启: 3. ...
- JS日期格式化函数性能优化篇
最近开发的软件中需要用到日志功能,其中有一个重要功能是显示日期和时间.于是网上搜了一把,搜到大量的日期格式化函数,不过比较了下,感觉代码都不够优雅,而且性能都不给力.对线上一些代码进行了评测,以下是一 ...
- SharedPreferences的工具类,使用起来方便、快捷
SharedPreferences的工具类,使用起来方便.快捷:上代码:import android.content.Context;import android.content.SharedPref ...
- memcache和memcached
一:Memcached.memcached.memcache. 其中首字母大写的Memcached,指的是Memcached服务器,就是独立运行Memcached的后台服务器,用于存储数据的“数据库” ...
- hdu 2189
//hdu2189 题意大概就是给n个人,分成多组,要求每组人数都是素数,求有多少种... 解法就是先把150以内的素数全部存入一个数组,然后利用a[j+b[i]]+=a[j];这道题一开始没理解 ...
- (转)select 1 from ... sql语句中的1代表什么意思? .
select 1 from ..., sql语句中的1代表什么意思?查出来是个什么结果? select 1 from table;与select anycol(目的表集合中的任意一行 ...
- DHCP租约时间工作原理
问题: 很多用户在使用路由器的DHCP服务器过程中都有一个疑问,DHCP有个设置项目是设置DHCP地址的租约时间,如果设置的比较短,是否会出现租约时间到了以后会重新去获取ip地址,造成用户断网? ...
- 使用 pm2 来守护 NoderCMS
pm2 是一个带有负载均衡功能的Node应用的进程管理器,使用 pm2 可以帮助你守护和监控 NoderCMS 的正常运行, 基于Node.js+MongoDB的轻量级内容管理系统NoderCMS ...
- ubuntu 安装 maven3
sudo add-apt-repository ppa:natecarlson/maven3 sudo apt-get update && sudo apt-get install m ...
- C#操作MYSQL遇到0000-00-00日期报错的原因
今天在做一个C#连接MYSQL数据库,并读取数据库的内容,遇到了0000-00-00日期转换报错:unable to convert MySQL date/time value to System.D ...