实现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” ...
随机推荐
- VB中的SSTab控件隐藏选项卡方法
请教下,VB中Sstab控件,如何隐藏选项卡呢? SSTab1.TabVisible(1) = False object.TabVisible(tab) [ = boolean ] tab 数值表达式 ...
- Codeforces 912 质因数折半 方格数学期望
A B #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #d ...
- c调用c++
g++ main.c math.cpp math.h中加入extern "C"
- 前端之JQuery:JQuery基本语法
jQuery基本语法 一.jQuery基础1.为什么要用jquery? 写起来简单,省事,开发效率高,兼容性好2.什么是jQuery? jQuery是一个兼容多浏览器的JavaScript库(类似py ...
- 模拟输入(ADC-A0)
ESP8266具有内置的10位ADC,只有一个ADC通道(A0引脚),即只有一个ADC输入引脚可读取来自外部器件的模拟电压 ESP8266上的ADC通道和芯片供电电压复用,也就是说我们可以将其设置为测 ...
- zrender的线性渐变
线性渐变 官方文档是这样写的 实际运用是酱紫的 在把颜色放背景中 小白一枚,路过大神,多多指教.欢迎留下宝贵意见
- sqlite3 on python for newbies
python 集成了 sqlite3 ,其接口很简单: import sqlite3 db_connection = sqlite3.connect(db_filename) db_cursor = ...
- js加密php解密(CryptoJS)碰到的坑
今天做了一个功能,需要js传密码到php文件,对js密码 进行判断,为想为这个传输过程进行解密,参考了网上的一个方法(这个方法我只是使用了,并没有太深了解0.0) 首先要引入3个js文件 (在网上可搜 ...
- php array_pop()函数 语法
php array_pop()函数 语法 作用:删除数组中的最后一个元素.博智达 语法:array_pop(array) 参数: 参数 描述 array 必需.规定数组. 说明:返回数组的最后 ...
- HDU 4758 Walk Through Squares ( Trie图 && 状压DP && 数量限制类型 )
题意 : 给出一个 n 行.m 列的方格图,现从图左上角(0, 0) 到右下角的 (n, m)走出一个字符串(规定只能往下或者往右走),向右走代表' R ' 向下走则是代表 ' D ' 最后从左上角到 ...