LeetCode 2016 8,357,367,69,378

8 String to Integer (atoi)

class Solution {
public:
    int myAtoi(string str)
    {
        int ans=0,len=str.length(),flag=1;
        if (len==0) return ans;
        int st=0;
        bool cntflag=false;
        while (str[st]==' ') st++;
        if (str[st]=='-')
        {
            flag=-1;
            st++;
            cntflag=true;
        }
        if (str[st]=='+')
        {
            if (cntflag) return ans;
            else st++;
        }
        string strext="";
        for(int i=st;i<len;i++)
        {
            int tmp=str[i]-'0';
            if ((tmp>=0) && (tmp<=9))
                strext=strext+str[i];
            else break;
        }
        int lenext=strext.length();
        if (lenext>10)
        {
            if (flag>0)
                return 2147483647; else return -2147483648;
        }
        if (lenext==10)
        {
            if ((flag>0)&&(strext>="2147483647")) return 2147483647;
            if ((flag<0)&&(strext>="2147483648")) return -2147483648;
        }
        for(int i=0;i<lenext;i++)
        {
            ans=ans*10+strext[i]-'0';
        }
        return ans*flag;
    }
};


357 Count Numbers with Unique Digits

题目中的提示:
Let f(k) = count of numbers with unique digits with length equals k.
f(1) = 10, ..., f(k) = 9 * 9 * 8 * ... (9 - k + 2) [The first factor is 9 because a number cannot start with 0].


class Solution
{
public:
    int countNumbersWithUniqueDigits(int n)
    {
        int res=0;
        if (n==0) return 1;
        if (n==1) return 10;
        if (n==2) return 91;
        res=91;
        for(int i=3;i<=n;i++)
        {
            int startj = 9-i+2;
            int ans=9;
            for(int j=startj;j<=9;j++)
                ans*=j;
            res+=ans;
        }
        return res;
    }
};


367 Valid Perfect Square

搜了牛顿法求平方根。

class Solution
{
public:
    int sqr(double n)
    {
        double k=1.0;
        while(fabs(k*k-n)>1e-9)
        {
            k=(k+n/k)/2;
        }
        return trunc(k);
    }

    bool isPerfectSquare(int num)
    {
        int tmp=sqr(num);
        if (tmp*tmp==num) return true;else return false;
    }
};

69 Sqrt(x)

超时了,精度不要那么大,小一点就行了。
牛顿法求平方根。

class Solution {
public:
    int mySqrt(int x) 
    {
        int n=x;
        double k=1.0;
        while(fabs(k*k-n)>1e-3)
        {
            k=(k+n/k)/2;
        }
        return trunc(k);
    }
};

378 Kth Smallest Element in a Sorted Matrix

class Solution {
public:
    int kthSmallest(vector<vector<int> >& matrix, int k)
    {
        int n=matrix.size();
        int m=n*n;
        vector<int> mat;
        //mat.resize(m);
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
            {
               // cout<<matrix[i][j]<<endl;
                mat.push_back(matrix[i][j]);
            }
        sort(mat.begin(),mat.end());
        //for(int i=0;i<m;i++)
            //cout<<mat[i]<<endl;
        return mat[k-1];
    }
};










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值