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. 证券化代币的时代已经到来,STO将引爆区块链经济

    STOs 似乎会在 2019 年取代 ICOs,即使不是完全取代,但置换的比例也会相当大.所有在美上市的公司都将按照 SEC 制定的相关规定进行交易.Vellum Capital 的 CEO 兼管理合 ...

  2. python 网页cookie的使用

    网页cookie的使用 Cookie,指某些网站为了辨别用户身份.进行session跟踪而储存在用户本地终端上的数据(通常经过加密) # opener的概念当你获取一个URL你使用一个opener(一 ...

  3. win10自带虚拟机Hyper V联网

      在控制面板里打开程序和功能     打开启用或关闭windows 功能     勾选Hyper-V     在windows 管理工具打开Hyper-V 管理器   打开虚拟交换机管理器     ...

  4. Python智能检测编码并转码

    #安装包工具 $pip3 install chardet #直接打开文件,中文显示乱码 >>> import chardet >>> f = open('test. ...

  5. 【shell脚本】通过遍历文件的一种批量执行shell命令的方法。

    在分析数据时,经常会有许多机械重复的命令带入,作为一个半路出家的程序猿,我曾经对这种工作束手无策.不像一个熟手那样举重若轻的分析,感觉自己的生信分析完全是个体力活.为了打开这样的局面,我开始学习如何批 ...

  6. JavaScript笔记 #07# 用js写算法

    算法盒子初代(为了提高学习算法的热情...) 效果图: 所有代码放在单个html中: <!DOCTYPE html> <html> <head> <meta ...

  7. Python Selenium element is not reachable by keyboard

    碰到这个问题,没法解决, 最后这么搞的 pcAction.move_by_offset(571, 534).click().perform() #激活输入框 driver.switch_to.acti ...

  8. Golang数组注意细节

    数组是多个相同类型数据的组合,一个数组一旦声明/定义了,其长度是固定的,不能动态变化. var arr[]int,这个arr就是slice切片. 数组中的元素可以是任何数据类型,包括值类型和引用类型, ...

  9. Vue小案例 之 商品管理------删除商品与提示

    实现删除商品功能 根据索引来进行删除商品: 实现删除商品的HTML: <!--显示表格--> <div class="table-warp"> <di ...

  10. AMQP 0.9.1和1.0协议差别以及rabbitmq支持情况

    RabbitMQ implements AMQP 1.0 via a plugin. However, AMQP 1.0 is a completely different protocol than ...