问题描述

Implement atoi to convert a string to an integer.

Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.

Notes: It is intended for this problem to be specified vaguely (ie, no given input specs). You are responsible to gather all the input requirements up front.

Update (2015-02-10):

The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.

原题链接:https://leetcode.com/problems/string-to-integer-atoi/

Analysis

public int myAtoi(String str) {
if(str == null || str.length() == 0){
return 0;
}
char[] chs = str.trim().toCharArray();
int isPositive = 1, index = 0;
if(chs[0] == '-'){
isPositive = -1;
index = 1;
}
if(chs[0] == '+'){
isPositive = 1;
index = 1;
}
long res = 0;
for(; index < chs.length; index++){
if(chs[index] < '0' || chs[index] > '9' ){
return (int)res;
}
res = res * 10 + isPositive * (chs[index] - '0');
if(res > Integer.MAX_VALUE){
return Integer.MAX_VALUE;
}
if(res < Integer.MIN_VALUE){
return Integer.MIN_VALUE;
}
}
return (int)res;
}

467. [leetcode] Unique Substrings in Wraparound String的更多相关文章

  1. [LeetCode] Unique Substrings in Wraparound String 封装字符串中的独特子字符串

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  2. Leetcode: Unique Substrings in Wraparound String

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  3. 【LeetCode】467. Unique Substrings in Wraparound String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/unique-s ...

  4. LeetCode 467. Unique Substrings in Wraparound String

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  5. 【LeetCode】467. Unique Substrings in Wraparound String

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  6. 467. Unique Substrings in Wraparound String

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  7. [Swift]LeetCode467. 环绕字符串中唯一的子字符串 | Unique Substrings in Wraparound String

    Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz" ...

  8. 动态规划-独特的子字符串存在于Wraparound String总个数 Unique Substrings in Wraparound String

    2018-09-01 22:50:59 问题描述: 问题求解: 如果单纯的遍历判断,那么如何去重保证unique是一个很困难的事情,事实上最初我就困在了这个点上. 后来发现是一个动态规划的问题,可以将 ...

  9. 467 Unique Substrings in Wraparound String 封装字符串中的独特子字符串

    详见:https://leetcode.com/problems/unique-substrings-in-wraparound-string/description/ C++: class Solu ...

随机推荐

  1. 好代码是管出来的——使用Git来管理源代码

    软件开发过程中一个重要的产出就是代码,软件的编码过程一般是由一个团队共同完成,它是一个并行活动,为了保证代码在多人开发中能够顺利完成,我们需要使用代码版本控制工具来对代码进行统一存储,并追踪每一份代码 ...

  2. 使用Coding Pages托管网站

    作者:荒原之梦 Coding官网: https://coding.net Coding Pages官网页面: https://coding.net/pages/ 具体过程如下: 1 注册Coding账 ...

  3. call(),apply()方法解析(一)

    1.call()和apply()的作用是改变this指向,区别是传参列表不同(前者连续参数,后者为参数数组) 2.方法定义: function.apply(thisObj[, argArray]) f ...

  4. jennifersoft,phantomjs

    http://jennifersoft.com/en/ Real Value of APM (Application Performance Monitoring) http://npm.taobao ...

  5. HQL: The Hibernate Query Language

    Chapter 14. HQL: The Hibernate Query Language 14.1. Case Sensitivity 14.2. The from clause 14.3. Ass ...

  6. Java Persistence/ManyToMany

    A ManyToMany relationship in Java is where the source object has an attribute that stores a collecti ...

  7. vue的传参方式和router使用技巧

    vue传参方法一 1,路由配置 { path: '/describe/:id', name: 'Describe', component: Describe } 2,使用方法 // 直接调用$rout ...

  8. Selenium 指定浏览器位置

    在脚本开头要指定浏览器位置. public static void main(String[] args) throws InterruptedException, IOException { Sys ...

  9. java基础小项目练习之1----3天做出飞机大战

    Shoot射击游戏第一天一. 关键问题(理论):1. 简述FlyingObject.Enemy.Award.Airplane.Bee.Bullet.Hero之间的继承与实现关系2. 简述Hero类构造 ...

  10. socketWriter.go

    package blog4go import ( "bytes" "fmt" "net" "sync" ) // Soc ...