描述

  • 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. python并发编程之多线程2------------死锁与递归锁,信号量等

    一.死锁现象与递归锁 进程也是有死锁的 所谓死锁: 是指两个或两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用, 它们都将无法推进下去.此时称系统处于死锁状态或系统 ...

  2. Fatal error: Uncaught Error: Call to undefined function mysqli_connect()

    sudo apt-get install php7.2-mysql  //版本号肯能不一样 sudo dpkg-reconfigure  php7.2-mysql sudo /etc/init.d/m ...

  3. java----javaBean

    Beanutils 工具类的下载 http://commons.apache.org/proper/commons-beanutils/ 使用 应用的时候还需要一个logging包http://com ...

  4. WebStorm中常用的快捷键及使用技巧

    ------------------------------------- 近期整理了如下个人觉得比较常用的快捷键,也请前辈给予补充.多多指教. --------------------------- ...

  5. Vue-CLI 3.x 设置反向代理

    最近在项目中使用了Vue CLI 3.0版本,项目中需要设置反向代理解决跨域问题,下面记录一下设置过程. 新建配置文件 (vue-cli3.x 官网的配置文档 https://cli.vuejs.or ...

  6. C# 不使用递归遍历目录树中的文件和文件夹

    public class StackBasedIteration { static void Main(string[] args) { // Specify the starting folder ...

  7. 【bzoj4631】踩气球 线段树

    题解: 真是很zz 我都想到线段树分治的思路了... 不过还是一道好题 首先跟线段树分治一样将区间投射到线段树上去 每次修改如果该个区间修改为0,则对他们对应的特定区间-1 这样每个区间会有一次变0, ...

  8. redcontrol for SL 中文化及样式选择

    app.xaml.cs public partial class App: Application    {        public App()        {            //指定t ...

  9. Python学习(二十一) —— 前端之JavaScript

    转载自http://www.cnblogs.com/liwenzhou/p/8004649.html 一.JavaScript概述 1.JavaScript的历史 1992年Nombas开发出C-mi ...

  10. CSS3 鲜为人知的属性-webkit-tap-highlight-color的理解

    (一)-webkit-tap-highlight-color         这个属性只用于iOS (iPhone和iPad).当你点击一个链接或者通过Javascript定义的可点击元素的时候,它就 ...