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字符串的每一位。(不知为啥,这个方法提交打败的人更多)
注意:
  1. 考虑正负数;用minus变量作为flag。遍历时,循环的初始值,也和minus有关,负数要从下标1开始。
  2. 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的更多相关文章

  1. 【leetcode】String to Integer (atoi)

    String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider ...

  2. No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  3. 【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 ...

  4. leetcode第八题 String to Integer (atoi) (java)

    String to Integer (atoi) time=272ms   accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...

  5. leetcode day6 -- String to Integer (atoi) &amp;&amp; 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 ...

  6. String to Integer (atoi) - 字符串转为整形,atoi 函数(Java )

    String to Integer (atoi) Implement atoi to convert a string to an integer. [函数说明]atoi() 函数会扫描 str 字符 ...

  7. Kotlin实现LeetCode算法题之String to Integer (atoi)

    题目String to Integer (atoi)(难度Medium) 大意是找出给定字串开头部分的整型数值,忽略开头的空格,注意符号,对超出Integer的数做取边界值处理. 方案1 class ...

  8. String与Integer问题

    今天分享一下关于最近面试的问题,临近春节,而我在茫茫人海中奔波,今天面试了来到了中关村科技园,挺气派的,之前也是在外面看看,今天就去了,心里有点激动,恰好,正好赶上了上班时,看见它们的努力,我感到再累 ...

  9. LeetCode--No.008 String to Integer (atoi)

    8. String to Integer (atoi) Total Accepted: 112863 Total Submissions: 825433 Difficulty: Easy Implem ...

  10. leetcode-algorithms-8 String to Integer (atoi)

    leetcode-algorithms-8 String to Integer (atoi) Implement atoi which converts a string to an integer. ...

随机推荐

  1. 在Hue中提交oozie定时任务

    可以参见下面这篇博文: 通过hue提交oozie定时任务

  2. C#使用SmtpClient发送邮件

    目的:写一个可发送邮件的DLL. 原理: 例如A使用163邮箱发送邮件给B(qq邮箱).首先A会把邮件通过SMTP(Simple Mail Transfer Protocol)协议传输到163的Smt ...

  3. [转载]SQL Server中的事务与锁

    了解事务和锁 事务:保持逻辑数据一致性与可恢复性,必不可少的利器. 锁:多用户访问同一数据库资源时,对访问的先后次序权限管理的一种机制,没有他事务或许将会一塌糊涂,不能保证数据的安全正确读写. 死锁: ...

  4. 记账本微信小程序开发一

    第一,在微信公众平台注册小程序账号并完善相关信息 第二,注册一个微信公众号,找到微信web开发工具并下载适合自己电脑的工具 第三,安装 第四,根据网上教程简单了解了开发工具的使用和布局

  5. Python基础二_操作字符串常用方法、字典、文件读取

    一.字符串常用方法: name.captitalize()                       #字符串首字母大写 name.center(50,'*')                   ...

  6. 3D模型文字动画

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  7. Tomcat配置Manager管理员

    修改文件: D:\MyDev\Tomcat\apache-tomcat-7.0.68\conf\tomcat-users.xml  配置内容: <role rolename="mana ...

  8. django模型中的关系对应

    显然,关系数据库的力量在于将表相互关联.Django提供了定义三种最常见的数据库关系类型的方法:多对一,多对多和一对一. 在说明之前,首先来理解一下这三个概念: 多对一: 两个集合a,b;集合a中的多 ...

  9. django 动态生成PDF文件

    可以通过开源的Python PDF库ReportLab来实现PDF文件的动态生成. 一.安装ReportLab ReportLab库在PyPI上提供,可以使用pip来安装: $ pip install ...

  10. oracle 11g SKIP_UNUSABLE_INDEXES参数

    SKIP_UNUSABLE_INDEXES的作用是在DML期间跳过对索引的维护,这样可以提交DML的效率,可以事后一次性重建,但是SKIP_UNUSABLE_INDEXES=y对unique inde ...