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 ...
随机推荐
- GIT的基本使用及应用场景
一.什么是GIT? Git 是一个开源的分布式版本控制软件,用以有效.高速的处理从很小到非常大的项目版本管理. GitHub.GitCafe.BitBucket和GitLab等是基于Git版本控制的远 ...
- servlet 乱码解决方法
一. servlet 发送的html 页面中文乱码 解决方法, 1.加入如下代码 response.setCharacterEncoding("UTF-8"); 2.在html页面 ...
- Linux编程学习笔记(一)
Linux的发展趋势势在必行,国内的服务器的操作系统Linux占到主导地位,不光是操作系统,还有嵌入式系统. 1. 今天就Linux的其中一个版本做一介绍,如下是Centos的版本之间的区别. ins ...
- babel-cli 的使用
1.安装babel-cli npm i babel-cli -D 2.实现npm的初始化 npm init -y 3.配置package.json { "name": " ...
- Web的几种上传方式总结
问题 文件上传在WEB开发中应用很广泛. 文件上传是指将本地图片.视频.音频等文件上传到服务器上,可以供其他用户浏览或下载的过程. 以下总结了常见的文件(图片)上传的方式和要点处理. 表单上传 这是传 ...
- Windows Server 2008/2012 计划任务配置执行bat
首先Windows Server 2008不同于其他服务器操作系统和Windows Server 2003有着很大的区别,计划任务的名称是“任务计划程序”不在控制面板里,而是在“管理工具”里.由于服务 ...
- ES标准
精确来说,ES1 ~ ES5 中的数字是 ECMA-262 标准的版本号(edition). 即:Standard ECMA-262, 1st Edition(其实第一个版本是没有版本号的)Stand ...
- order by 导出数据不一致的问题
转自 http://blog.csdn.net/wwwwei_csdn/article/details/78181197 前言 数据库分页是后台经常要使用的技术手段,有时候进行数据库查询会根据业务 ...
- 【Android】GestureDetector类及其用法
当用户触摸屏幕的时候,会产生许多手势,例如down,up,scroll,filing等等. 一般情况下,我们知道View类有个View.OnTouchListener内部接口,通过重写他的onTouc ...
- 【Android】Android WebView 网页输入框获取焦点
webView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEv ...