241. String to Integer
描述
- Given a string, convert it to an integer. * You may assume the string is a valid
- integer number that can be presented by a * signed 32bit integer (-231 ~ 231-1).
样例
给出 "123", 返回 123.
public class Solution {
/**
* @param str: A string
* @return: An integer
*/
public int stringToInteger(String str) {
// write your code here
return Integer.parseInt(str);
//return Integer.valueOf(str);
}
}
public class Solution {
/**
* @param str: A string
* @return: An integer
*/
public int stringToInteger(String str) {
// write your code here
int integer = 0;
int negative = 0;//1为true是负数,0为false是正数
if (str.charAt(0) == '-'){
negative = 1;
}
for (int i = negative; i < str.length(); i++){
integer = integer * 10 + str.charAt(i)-'0';
//假设str=1000
//i=0 integer=0*10+1=1;
//i=1 integer=1*10+0=10;
//i=2 integer=10*10+0=100;
//i=3 integer=100*10+0=1000;
}
if (negative == 1){
integer = -integer;
}
return integer;
}
}
241. String to Integer的更多相关文章
- 【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】7 & 8 - Reverse Integer & String to Integer (atoi)
7 - Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Notic ...
- 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 ...
- String与Integer问题
今天分享一下关于最近面试的问题,临近春节,而我在茫茫人海中奔波,今天面试了来到了中关村科技园,挺气派的,之前也是在外面看看,今天就去了,心里有点激动,恰好,正好赶上了上班时,看见它们的努力,我感到再累 ...
- LeetCode--No.008 String to Integer (atoi)
8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...
随机推荐
- Python实战一
要求:用户输入用户名和密码错误三次,就对该用户进行锁定,不让其进行登录. def match(name,pwd): '''匹配用户输入的信息,进行判断''' falg = True while fal ...
- vue @click 使用三目运算(实现动态更换绑定的函数)
转载:https://www.jianshu.com/p/ea4471c9f333 @click 错误写法 @click="dialogStatus=='create'?createData ...
- Java+selenium之WebDriver的cookie,等待等高级操作(五)
1. 操作cookie // 增加一个 name = "name",value="value" 的 cookie Cookie cookie = new Coo ...
- Parameter 'name' not found. Available parameters are [arg1, arg0, param1, param2]
解决方法: <select id="selectIf" resultType="student"> SELECT id,name,age,score ...
- 类和JSP关系
404的原因.除了路径问题,还有文件放置位置.比如如果文件在web-inf下面.浏览器是访问不到的
- pycharm安装mysql驱动包
新的环境配置pycharm的项目时,发现pycharm不能连接到mysql数据库.由于安了java环境但是还没配置相关的库,并且jetbrains家的IDE一般都是java写的,于是猜想可能是java ...
- javascript 面向对象-面试题实例
/ 从设计到模式 // 设计模式简介 // 设计 // 模式 // 分开 // 从设计到模式 // 23种设计模式 // 创建型 // 工厂模式(工厂方法模式,抽象工厂模式,建造者模式) // 单例模 ...
- [转] 用webpack的CommonsChunkPlugin提取公共代码的3种方式
方式一,传入字符串参数 new webpack.optimize.CommonsChunkPlugin(‘common.js’), // 默认会把所有入口节点的公共代码提取出来,生成一个common. ...
- 第k个互质数(二分 + 容斥)
描述两个数的a,b的gcd为1,即a,b互质,现在给你一个数m,你知道与它互质的第k个数是多少吗?与m互质的数按照升序排列. 输入 输入m ,k (1<=m<=1000000;1<= ...
- IsNullOrEmpty和IsNullOrWhiteSpace的区别
IsNullOrEmpty // string /// <summary>Indicates whether the specified string is null or an < ...