zoukankan      html  css  js  c++  java
  • leetcode每日刷题计划-简单篇day1

    orzorz开始刷题

    争取坚持每周平均下来简单题一天能做两道题吧

    非常简单的题奇奇怪怪的错误orz越不做越菜

    Num 7 整数反转 Reverse Integer

    刚开始多给了一个变量来回折腾占地方,没注意到溢出

    因为有溢出,在返回的时候(int) ans

    开始设置的时候设置long ans,不然遇到溢出直接报错了。

    class Solution {
    public:
        int reverse(int x) {
            long ans=0;
            while(x!=0)
            {
                ans=ans*10+x%10;
                x=x/10;
            }
            if(ans<-2147483648 || ans> 2147483647)
                return 0;
            return (int)ans;
        }
    };
    View Code

    第一反应的比较麻烦的做法,刚开始ten的设置int疯狂报错orz没考虑到既然基础那多半10^n也溢出哇

    已经改对了

    class Solution {
    public:
        long ten(int x)
        {
            long a=1;
            while(x!=1)
            {
                x--;
                a=a*10;
            }
            return a;
        }
        int reverse(int x) {
            int x1=x;
            long y,count;
            y=0;
            count=0;
            while(x!=0)
            {
                x=x/10;
                count++;
            }
            while(x1!=0)
            {
                /**if(((x1%10)*ten(count)<-2147493648)||((x1%10)*ten(count)>2147483647))
                   return 0;*/
                y+=x1%10*ten(count--);
                x1=x1/10;
            }
            if(y < -2147483648 || y > 2147483647)
            {
                return 0;
            }
            return (int)y;
        }
    };
    View Code

    更大范围应该是变成字符,在medium里,改天做呀~

    Num 9 回文数 Palindrome Number

    一次过

    没啥说的

    class Solution {
    public:
        bool isPalindrome(int x) {
            if(x<0)
                return false;
            int count=0;
            int a[100];
            while(x!=0)
            {
                a[count++]=x%10;
                x=x/10;
            }
            if(count==1)
                return true;
            for(int i=0;i<count/2;i++)
            {
                if(a[i]!=a[count-1-i])
                    return false;
            }
            return true;
        }
    };
    View Code
    时间才能证明一切,选好了就尽力去做吧!
  • 相关阅读:
    vue与react的区别
    for(key in obj)
    github上传项目
    第一个blog
    黑客几乎绝不用鼠标?
    中国白帽黑客调查
    编程新手六部曲
    程序猿看了要xiao了
    程序是如何执行的?
    IT 技术人需要思考的 15 个问题
  • 原文地址:https://www.cnblogs.com/tingxilin/p/10685728.html
Copyright © 2011-2022 走看看