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' )
代码(思路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. ...
随机推荐
- Eclipse创建maven工程后没有build path解决方案
1.修改maven工程下的.project文件为如下内容 <?xml version="1.0" encoding="UTF-8"?> <pr ...
- Step4:SQL Server 跨网段(跨机房)复制
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 解决方案(Solution) 搭建过程(Process) 注意事项(Attention) 参考 ...
- PS火焰文字制作
火焰文字制作: 最终效果 第一步: 新建图层,并输入文字(这里不做详细解说)
- strpos 的正确使用方式
首先简单介绍下strpos函数: strpos函数是查找某个字符在字符串中的位置:这里需要明确这个函数的作用: 这个函数得到的是位置:如果存在:返回数字:否则返回的是false: 而很多时候我们拿这个 ...
- js 解密 16进制转10进制,再取ascii码的对应值
如:\x64 对应 16进制 0x64 转10进制就是 0x64.toString(10) == 100, 查对应的ascii码表得到 ‘d' <div id=code style='displ ...
- Redis可以用来做什么?(摘自http://www.lianpenglin.cc廉鹏林博客)
Redis 是互联网技术领域使用最为广泛的存储中间件,它是「Remote Dictionary Service」的首字母缩写,也就是「远程字典服务」.Redis 以其超高的性能.完美的文档.简洁易懂的 ...
- 批处理:根据进程名称查询进程,如果有进程就输出up没有就输出donw
需求:windows系统上 根据进程名称查询进程,如果有进程就输出 up ,没有就输出 donw. ::Final interpretation is owned by chenglee ::@e ...
- 报错ORA-19809 ORA-19804
现象 问题检查 查看群集状态,发现归档挂起 [oracle@jydb1 ~]$ srvctl status database -d orcl -v 查看空间使用情况 #大小查看 SQL> sho ...
- 基础_模型迁移_CBIR_augmentation
在之前我们做过这样的研究:5图分类CBIR问题 各不相同的 5类的图形,每类100张 import numpy as npfrom keras.datasets import mnistimport ...
- 基础_cifar10_model
今天进一步在cifar10数据集上解决几个问题: 1.比较一下序贯和model,为什么要分成两块: 2.同样的条件下,我去比较一下序贯和model.这个例子作为今天的晚间运行. 1.比较一下序贯和mo ...