Lintcode241-String to Integer - Naive
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).
Example
Example 1:
Input: "123"
Output: 123
Explanation:
return the Integer.
Example 2:
Input: "-2"
Output: -2
Explanation:
return the Integer.
思路1:用函数
思路2: 下标由小到大,依次取String字符串的每一位。(不知为啥,这个方法提交打败的人更多)
注意:
考虑正负数;用minus变量作为flag。遍历时,循环的初始值,也和minus有关,负数要从下标1开始。
- num = num * 10 + str.charAt(i) - '0';
- // example 1
char a = '3'; - char b = '5';
- char c = a + b;
// example 2- char a = '3';
- int b = 5;
- int c = a + b;
- int d = a - '0' + b;
example 1: c = 33 + 35
example 2: c = 33 + 5 = 38; d = 33 - 30 + 5 = 8
所以char加减运算时,会自动转化为 ASCII码。如果想取char的实际数值,要 -'0'. (如char a = '3', 想取3,则 a - '0' ) - // example 1
代码(思路1):
- public int stringToInteger(String str) {
- return Integer.parseInt(str);
- }
代码(思路2):
- public int stringToInteger(String str) {
- int num = 0;
- int minus = 0;
- if (str.charAt(0) == '-') {
- minus = 1;
- }
- for (int i = minus; i < str.length(); i++){
- num = num * 10 + str.charAt(i) - '0';
- }
- if (minus == 1) {
- return -num;
- } else{
- return num;
- }
- }
Lintcode241-String to Integer - Naive的更多相关文章
- 【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 ...
- leetcode-algorithms-8 String to Integer (atoi)
leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...
随机推荐
- python pynssql创建表,删除表,插入数据,查询
import pymssql server='10.194.**.***:*****' user='sa' password='******' database='******' #连接 conn=p ...
- Mini-Batch 、Momentum、Adam算法的实现
Mini-Batch 1. 把训练集打乱,但是X和Y依旧是一一对应的 import numpy as np a = np.random.randn(3,3) print(a) b = list(np. ...
- Xfire基础
XFire 是与Axis 2并列的新一代Web Service框架,通过提供简单的API支持Web Service各项标准协议,能够快速地开发Web Service应用.和其他Web服务引擎相比,XF ...
- vue各种实例集合
注意:以下所有示例基于vue 2.x.Vuex 2.x. vm.$mount()-挂载: <body> <div id="a"> </div> ...
- 在MyEclipse中使用javadocAPI文档
开始啦 1.打开MyEclipse,选中要导出的项目,右击Exprot弹出窗口,选择java----javadoc点击next到下一界面. 2.选出要导出的项目或要添加的项目,在browse中选择路径 ...
- SpringMVC之数据绑定
SpringMVC之数据绑定 #数据绑定:Spring MVC会根据客户端请求参数的不同,将请求信息以一定的方式转换并绑定 到控制器类中的方法参数上. #说明:这里的“以一定的方式”应该指的是什么?过 ...
- 使用nosql实现页面静态化的一个小案列
页面静态化,其实就是将动态生成的php页面,变成静态的HTML页面,让用户直接访问.有一下几方面好处: 1,首先就是访问速度,不需要去访问数据库,或者缓存来获取哪些数据,浏览器直接加载渲染html页即 ...
- Golang接口简单了解
在Golang中,一个类只需要实现了接口要求的所有函数,我们就说这个类实现了该接口. package main import "fmt" type Animal interface ...
- Centos部署flask项目
必备: Python2.7(ok) MySQL(ok) git supervisor virtualenv Gunicorn 阿里云服务器(最便宜的就好) 域名(国内万网,国外goDaddy) 我的P ...
- 深度挖掘,Html5的 Range 滑动刻度的坑,兼容全平台,将任性进行到底!
最近2天一直在弄一个滑动的刻度效果,由于项目是基于Web App开发的,于是考虑到 移动端和pc端 的兼容性问题,考虑的比较多,尝试采用 Html5的Range 来做,目前已经兼容 pc端和移动端! ...