实现atoi
public class Solution {
public int atoi(String str) {
str = str.trim();
int result = 0;
boolean isPos = true;
for(int i=0; i<str.length(); i++) {
char c = str.charAt(i);
if(i==0 && (c == '+' || c == '-')) {
isPos = c == '+' ? true : false;
} else if(c >= '0' && c <= '9') {
if(result > (Integer.MAX_VALUE- (c-'0'))/10) {
return isPos ? Integer.MAX_VALUE : Integer.MIN_VALUE;
}
result = result * 10 + c - '0';
} else {
return isPos ? result : -result;
}
}
return isPos ? result : -result;
}
}
实现atoi的更多相关文章
- [LeetCode] String to Integer (atoi) 字符串转为整数
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 编写atoi库函数
看到很多面试书和博客都提到编写atoi函数,在很多面试中面试官都会要求应聘者当场写出atoi函数的实现代码,但基本很少人能写的完全正确,倒不是这道题有多么高深的算法,有多么复杂的数据结构,只因为这道题 ...
- 行程编码(atoi函数)
#include<iostream> #include<string> #include<vector> using namespace std; void jie ...
- No.008:String to Integer (atoi)
问题: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- c/c++面试题(8)memcopy/memmove/atoi/itoa
1.memcpy函数的原型: void* memcpy(void* dest,cosnt void* src,size_t n); 返回值:返回dest; 功能:从源内存地址src拷贝n个字节到des ...
- LeetCode 7 -- String to Integer (atoi)
Implement atoi to convert a string to an integer. 转换很简单,唯一的难点在于需要开率各种输入情况,例如空字符串,含有空格,字母等等. 另外需在写的时候 ...
- [LeetCode] 8. String to Integer (atoi)
Implement atoi to convert a string to an integer. public class Solution { public int myAtoi(String s ...
- atoi()函数
原型:int atoi (const char *nptr) 用法:#include <stdlib.h> 功能:将字符串转换成整型数:atoi()会扫描参数nptr字符串,跳过前 ...
- [Leetcode]String to Integer (atoi) 简易实现方法
刚看到题就想用数组做,发现大多数解也是用数组做的,突然看到一个清新脱俗的解法: int atoi(const char *str) { ; int n; string s(str); istrings ...
- 【leetcode】atoi (hard) ★
虽然题目中说是easy, 但是我提交了10遍才过,就算hard吧. 主要是很多情况我都没有考虑到.并且有的时候我的规则和答案中的规则不同. 答案的规则: 1.前导空格全部跳过 “ 123” ...
随机推荐
- Codeforces 960 二进制构造子序列 完全二叉树shift模拟 主席树/MAP DP
A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...
- Dinic最大流 || Luogu P3376 【模板】网络最大流
题面:[模板]网络最大流 代码: #include<cstring> #include<cstdio> #include<iostream> #define min ...
- ubuntu 16.04 安装后需要做的事情
1. 更改软件源 sudo gedit /etc/apt/source.list 在底部加入:(如果可以,把Ubuntu官方源注释掉“#_____”) # deb cdrom:[Ubuntu 16.0 ...
- PHP简单的爬虫–原型
1.PHP简单的爬虫–原型 爬虫的原理: 给定原始的url: 分析链接,根据设置的正则表达式获取链接中的内容: 有的会更新原始的url再进行分析链接,获取特定内容,周而复始. 将获取的内容保存在数据库 ...
- Git回滚到指定的commit
查看历史commint $ git log (可以记下sha码) 回退命令: $ git reset --hard HEAD^ 回退到上个版本$ git reset --hard HEAD~3 回退到 ...
- python-第三方包的安装和升级和卸载
安装源: 豆瓣 http://pypi.douban.com/simple/ 本地安装: egg文件: 使用settools自带的安装脚本easy_install进行安装 whl文件: ...
- R语言-三种方法绘制单位圆
与一般开发语言不同,R以数据统计分析和绘图可视化为主要卖点.本文是第一篇博客,解决一个简单的绘图问题,以练手为目的. 以下直接给出三种单位圆的画法: 方法1 f=seq(,*pi,0.001) x=s ...
- CSS3——PC以及移动端页面适配方法(响应布局)
响应布局就是不同宽度应用不同的样式块,每个样式块对应的是该宽度下的布局方式,从而使页面适应不同宽度. <!DOCTYPE html> <html lang="en" ...
- OmniGraffle 7使用的探索
进去后可以将界面简化为4个主要区域:工具栏.工具栏.检查器和画布. 1.画布是在项目中创建.编辑和移动对象的地方 2.删除画布 选择编辑 画布删除画布 3.OmniGraffle项目至少需要一个画布 ...
- 面向对象this关键字和概述和应用
面向对象this关键字和概述和应用 package thisdemo; /** * 面向对象this关键字和概述和应用 * */ //定义老师类 class Teacher { private Str ...