How to convert a string to an integer in LeetCode?

2026-04-28 03:231阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计185个文字,预计阅读时间需要1分钟。

How to convert a string to an integer in LeetCode?

java/** * 8. String to Integer (atoi) * @param str * @return * 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. */

How to convert a string to an integer in LeetCode?

StringtoInteger.java

/** * 8. String to Integer (atoi) * @param str * @return * 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. * There are some problems */ public static int atoi(String str) { str = str.trim(); if(str.length()==0){ return 0; } int base = 1; if(str.startsWith("-")){ base = -1; str = str.substring(1); } int dev = 1; for(int i=1;i (2<<29)){ if(base==1){ return Integer.MAX_VALUE; } return Integer.MIN_VALUE; } } int result = 0; for(int i=0,j=dev;i

本文共计185个文字,预计阅读时间需要1分钟。

How to convert a string to an integer in LeetCode?

java/** * 8. String to Integer (atoi) * @param str * @return * 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. */

How to convert a string to an integer in LeetCode?

StringtoInteger.java

/** * 8. String to Integer (atoi) * @param str * @return * 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. * There are some problems */ public static int atoi(String str) { str = str.trim(); if(str.length()==0){ return 0; } int base = 1; if(str.startsWith("-")){ base = -1; str = str.substring(1); } int dev = 1; for(int i=1;i (2<<29)){ if(base==1){ return Integer.MAX_VALUE; } return Integer.MIN_VALUE; } } int result = 0; for(int i=0,j=dev;i