描述

  • 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的更多相关文章

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

随机推荐

  1. list的add()方法与addAll()方法简介

    简单描述:月读别人的代码,发现了一个有意思的东西,list的一个方法,addAll(),然后就去度娘了一下,发现这个还挺有用的. 吐槽一下:为什么自己没发现这个方法呢?因为平时自己写list的时候,基 ...

  2. js中的“==”和“===”的区别

    简单来说: == 代表相同, ===代表严格相同, 为啥这么说呢, 这么理解: 当进行双等号比较时候: 先检查两个操作数数据类型,如果相同, 则进行===比较, 如果不同, 则愿意为你进行一次类型转换 ...

  3. Appium Python API 中文版

    Appium_Python_Api文档 1.contextscontexts(self): Returns the contexts within the current session. 返回当前会 ...

  4. AI学习吧-公钥私钥、沙箱环境

    公钥私钥 公钥.私钥 可以互相解密 应用:数字签名和加密数据 数字签名:使用私钥加密,公钥解密 加密数据:使用公钥加密,私钥解密泄密时:当有人拿走了你的公钥,你可以到CI证书中心,使用你的私钥和公钥办 ...

  5. Python初探list

    今天要说一个新概念--list,中文可以翻译成列表,是用来处理一组有序项目的数据结构.想象一下你的购物清单.待办工作.手机通讯录等等,它们都可以看作是一个列表.说它是新概念也不算确切,因为我们之前已经 ...

  6. nodejs 环境安装

    参考网站 http://www.runoob.com/nodejs/nodejs-http-server.html https://github.com/nodesource/distribution ...

  7. 解决bootstrap多模态框跳转时页面左移问题

    衍生问题暂未发现.... 忽略左右跳动视觉差 解决方法: 在bootstrap的js搜索padding-right,然后找到“+this.scrollbarWidth”,删掉即可.

  8. 如何设置Navicat的显示字体与字体大小?

    方法/步骤     打开Navicat   点击[工具]菜单,再选择[选项]   在[选项]界面,点击[外观]下的[字体]   设置网格字体和大小   设置编辑器字体和大小   设置命令列界面字体和大 ...

  9. CentOS6—HAProxy安装与配置

    概述 Haproxy下载地址:http://pkgs.fedoraproject.org/repo/pkgs/haproxy/ 关闭SElinux.配置防火墙 1.vi /etc/selinux/co ...

  10. [转]JAVA实现SFTP实例

    http://www.cnblogs.com/chen1987lei/archive/2010/11/26/1888384.html 最近写的一个JAVA实现SFTP的实例: /** Created ...