Implement atoi to convert a string to an integer.


转换很简单,唯一的难点在于需要开率各种输入情况,例如空字符串,含有空格,字母等等。

另外需在写的时候注意细节问题,完成后需要反复调试,整合。

 public class Solution {
public int myAtoi(String str) {
if (str == null || str.length() < 1)
return 0; str = str.trim(); char flag = '+'; int i = 0;
if (str.charAt(0) == '-') {
flag = '-';
i++;
} else if (str.charAt(0) == '+') {
i++;
}
double result = 0; while (str.length() > i && str.charAt(i) >= '0' && str.charAt(i) <= '9') {
result = result * 10 + (str.charAt(i) - '0');
i++;
} if (flag == '-')
result = -result; if (result > Integer.MAX_VALUE)
return Integer.MAX_VALUE; if (result < Integer.MIN_VALUE)
return Integer.MIN_VALUE; return (int) result;
}
}

LeetCode 7 -- String to Integer (atoi)的更多相关文章

  1. Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)

    Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...

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

  3. 【leetcode】String to Integer (atoi)

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

  4. [leetcode] 8. String to Integer (atoi) (Medium)

    实现字符串转整形数字 遵循几个规则: 1. 函数首先丢弃尽可能多的空格字符,直到找到第一个非空格字符. 2. 此时取初始加号或减号. 3. 后面跟着尽可能多的数字,并将它们解释为一个数值. 4. 字符 ...

  5. Leetcode 8. String to Integer (atoi)(模拟题,水)

    8. String to Integer (atoi) Medium Implement atoi which converts a string to an integer. The functio ...

  6. [LeetCode][Python]String to Integer (atoi)

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/string- ...

  7. 【LeetCode】String to Integer (atoi) 解题报告

    这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...

  8. LeetCode 8. String to Integer (atoi) (字符串到整数)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  9. [LeetCode] 8. String to Integer (atoi) 字符串转为整数

    Implement atoi which converts a string to an integer. The function first discards as many whitespace ...

随机推荐

  1. html文件里引入文件html文件

    导入通用的代码除了使用php外 iframe在很多界面使用起来比较方便 比如说要写导航 在好几个界面都要用这个导航 可以用iframe引用 实例:这个header.html是我写的一个导航界面 在in ...

  2. jmeter for循环嵌套if学习2

    if语句中勾选Evaluate选项,每执行一句都会判断result的值是否为true. 执行结果: three没有执行,到debug时变量的值变成tom了

  3. Ubuntu Server 配置网络

    ------------------ Ubuntu 14.04   x86_64 ----------------- 设置静态IP:vi /etc/network/interfaces. 然后再编辑 ...

  4. 关于SQLite的伪随机数

    --random() 的返回值范围是:[-2^63,(2^63)-1],而通常要的是最小为 0的随机数,于是就不能直接用了,解决办法: then col else -col end) result f ...

  5. Balanced Binary Tree [LeetCode]

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  6. Winform 文本框多线程赋值

    delegate void SetTextCallback(string text); private void showClientMsg(string text) { // InvokeRequi ...

  7. YY前端课程4

    1. CSS和HTML一样,也是标记语言 2. CSS有三种样式:嵌入样式.内部样式(行内样式)和外部样式(外部样式表) 3. CSS的语法:选择器+{一个或多个样式} 4. 选择器是为了找到html ...

  8. iOS:iOS中的多控制器管理

    iOS中的控制器有三种创建方式: 1.通过storyboard创建 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@" ...

  9. umask

    1. 首先我们来思考umask是什么? umask 是系统设置的权限的默认值,在etc/profile里面的shell 脚本有设置规则. 对于root用户和用户而言,不可以直接用的 需要用减法 比如 ...

  10. python 邮件

    1:文件形式的邮件 01.#!/usr/bin/env python3 02.#coding: utf-8 03.import smtplib 04.from email.mime.text impo ...