www.icesr.com
IT运维工程师的摇篮

leetcode08- String to Integer (atoi)之Java版本

我的leetcode之旅,该篇章主要完成使用Java实现算法。这是第8篇String to Integer (atoi)

全部代码下载:Github链接:github链接,点击惊喜;写文章不易,欢迎大家采我的文章,以及给出有用的评论,当然大家也可以关注一下我的github;多谢;

1.题目简介:

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.

spoilers alert… click to show requirements for atoi.

Requirements for atoi:

The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.

The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.

If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.

If no valid conversion could be performed, a zero value is returned. If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

2.我的思路:

1.题目是挺easy的,主要是对一些特殊输出进行考虑
2.特殊状况详情见我的注释

3.我的AC代码

<code><span class="hljs-keyword">package</span> com.rlovep.int1;
<span class="hljs-javadoc">/**
 * String to Integer (atoi)
 * 我的思路:
 * 题目是挺easy的,主要是对一些特殊输出进行考虑
 * 特殊状况详情见我的注释
 *<span class="hljs-javadoctag"> @author</span> peace
 *
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Atoi</span> {</span>
     <span class="hljs-keyword">public</span>  <span class="hljs-keyword">static</span> <span class="hljs-keyword">int</span> <span class="hljs-title">myAtoi</span>(String str) {
         <span class="hljs-keyword">long</span>  res=<span class="hljs-number">0</span>; <span class="hljs-comment">//使用long型主要是为了防止超过int的限制</span>
         <span class="hljs-keyword">long</span> out=<span class="hljs-number">2147483648</span>L;<span class="hljs-comment">//当res技术大于out时证明已经过量,可以结束对剩余的转型</span>
         <span class="hljs-keyword">if</span>(str==<span class="hljs-keyword">null</span>||<span class="hljs-string">""</span>.equals(str))<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<span class="hljs-comment">//对字符串为null和空的考虑</span>
         <span class="hljs-keyword">int</span> i=<span class="hljs-number">0</span>;<span class="hljs-comment">//记录字符位置索引</span>
         str=str.trim();<span class="hljs-comment">//去除开头和结尾的空格字符</span>
         <span class="hljs-keyword">int</span> length=str.length();<span class="hljs-comment">//获得字符串长度</span>
         <span class="hljs-keyword">boolean</span> flag=<span class="hljs-keyword">true</span>;
         <span class="hljs-keyword">if</span>(str.charAt(i)==<span class="hljs-string">'+'</span>||str.charAt(i)==<span class="hljs-string">'-'</span>){
             <span class="hljs-keyword">if</span>(length&lt;<span class="hljs-number">2</span>)<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<span class="hljs-comment">//当字符串只有一个符号位时,直接返回</span>
             <span class="hljs-keyword">if</span>(str.charAt(i)==<span class="hljs-string">'-'</span>) flag=<span class="hljs-keyword">false</span>;<span class="hljs-comment">//正负号判断标志</span>
             i++;
         }
         <span class="hljs-keyword">for</span>(;i&lt;length;i++)
         {
             <span class="hljs-keyword">int</span> c=str.charAt(i)-<span class="hljs-string">'0'</span>;<span class="hljs-comment">//获得转型的整数</span>
             <span class="hljs-keyword">if</span>(c&gt;=<span class="hljs-number">0</span>&amp;&amp;c&lt;=<span class="hljs-number">9</span>){
                  res=res*<span class="hljs-number">10</span>+c;<span class="hljs-comment">//进行转换</span>
                  <span class="hljs-keyword">if</span>(res&gt;out)<span class="hljs-keyword">break</span>;<span class="hljs-comment">//判断是否过量</span>
             }<span class="hljs-keyword">else</span>{
                 <span class="hljs-keyword">break</span>;<span class="hljs-comment">//出现非法字符,结束转型</span>
             }
         }
         <span class="hljs-keyword">if</span>(flag==<span class="hljs-keyword">false</span>)res=-res;
         <span class="hljs-keyword">if</span>(res&gt;<span class="hljs-number">2147483647</span>)res=<span class="hljs-number">2147483647</span>;<span class="hljs-comment">//正数过量时输出正数最大值</span>
         <span class="hljs-keyword">if</span>(res&lt;-<span class="hljs-number">2147483648</span>)res=-<span class="hljs-number">2147483648</span>;<span class="hljs-comment">//负数过量时输出负数最大值</span>
         <span class="hljs-keyword">return</span> (<span class="hljs-keyword">int</span>)res;
    }
     <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span>(String[] args) {
        System.out.println(myAtoi(<span class="hljs-string">"9223372036854775809"</span>));
    }
}</code>

好的本章介绍到这里 来自伊豚wpeace(blog.wpeace.cn)

未经允许不得转载:冰点网络 » leetcode08- String to Integer (atoi)之Java版本

分享到:更多 ()

评论 抢沙发

评论前必须登录!