LeetCode 2016 292,258,328,326,334,329

292 Nim Game

法一:递归超时

class Solution {
public:
    bool canWinNim(int n) {
        if (n<=0) return false;
        vector<bool> f;
        f.resize(n+1);
        f[0]=true;
        f[1]=true;
        f[2]=true;
        f[3]=true;
        int i=3;
        while (i<n)
        {
            i++;
            f[i]=!(f[i-1]&&f[i-2]&&f[i-3]);
        }
        return f[n];
    }
};

法二:找规律过了

class Solution {
public:
    bool canWinNim(int n) {
        if (n<=0) return false;
        if (n%4==0) return false; else return true;
    }
};


258 Add Digits

class Solution {
public:
    int addDigits(int num) {
        if (num<=9) return num;
        int ans=num;
        while (ans>9)
        {
            int p=ans,t=0;
            while (p!=0)
            {
                t+= p%10;
                p/=10;
            }
            ans=t;
        }
        return ans;
    }
};

328 Odd Even Linked List


class Solution {
public:
    ListNode* oddEvenList(ListNode* head) {
        if (head==NULL || head->next==NULL) return head;
        ListNode* p;
        ListNode* podd;
        ListNode* peven;
        ListNode* hodd = new ListNode(0);
        ListNode* heven = new ListNode(0);
        podd=hodd;peven=heven;p=head;
        bool flag=true;
        while (p!=NULL)
        {
            if (flag)
            {
                podd->next=p;
                podd=podd->next;
            }
            else
            {
                peven->next=p;
                peven=peven->next;
            }
            p=p->next;
            flag=!flag;
        }
        podd->next=heven->next;
        peven->next=NULL;
        return hodd->next;
    }
};

326 Power of Three

法一:使用循环

class Solution {
public:
    bool isPowerOfThree(int n) {
        if (n==0) return false;
        while (n%3==0 && n!=1)
        {
            n=n/3;
        }
        if (n==1) return true;
        else return false;
    }
};

法二:不使用循环和递归,使用对数

class Solution {
public:
    bool isPowerOfThree(int n) {
        if (n==0) return false;
        double tmpans=log(n)/log(3.0);
        int p=floor(tmpans+0.5);
        int tmpn=pow(3,p);
        if (tmpn==n) return true;else return false;
    }
};

334 Increasing Triplet Subsequence

class Solution {
public:
    bool increasingTriplet(vector<int>& nums) {
        int len=nums.size();
        if (len<3) return false;
        vector<int> f(len);
        for(int i=0;i<len;i++)
        {
            int tmpfi=0;
            for(int j=0;j<i;j++)
            {
                if (nums[i]>nums[j] && f[j]>tmpfi)
                    tmpfi=f[j];
            }
            f[i]=tmpfi+1;
            if (f[i]>=3) return true;
        }
        return false;
    }
};


329 Longest Increasing Path in a Matrix

法一:超时

class Solution {
public:
    struct Node
    {
        int x,y,val;
       // Node(int a,int b,int c) : x(a),y(b),val(c) {}
    };
    vector<Node>nums;
    int longestIncreasingPath(vector<vector<int> >& matrix) {
        nums.clear();
        int ans=-1;
        int m=matrix.size();
        if (m==0) return 0;
        int n=matrix[0].size();
        for(int i=0;i<m;i++)
            for(int j=0;j<n;j++)
            {
                struct Node tmp={i,j,matrix[i][j]};
                nums.push_back(tmp);
            }
        sort(nums.begin(),nums.end(),cmp);
        int len=nums.size();
        int f[len+5]={0};
        for(int i=0;i<len;i++)
        {
            for(int j=0;j<i;j++)
            {
                if (nums[j].val>=nums[i].val) continue;
                if ((abs(nums[i].x-nums[j].x)+abs(nums[i].y-nums[j].y))!=1)
                    continue;
                if (f[j]>f[i]) f[i]=f[j];
            }
            f[i]++;
            if (f[i]>ans) ans=f[i];
        }
        return ans;
    }
    static bool cmp(Node a,Node b)
    {
        return a.val<b.val;
    }
};

法二:过了

class Solution {
public:
    struct Node
    {
        int x,y,val;
       // Node(int a,int b,int c) : x(a),y(b),val(c) {}
    };
    vector<Node>nums;
    int longestIncreasingPath(vector<vector<int> >& matrix) {
        nums.clear();
        int ans=-1;
        int m=matrix.size();
        if (m==0) return 0;
        int n=matrix[0].size();
        vector<vector<int> > fm(m,vector<int>(n));
        for(int i=0;i<m;i++)
            for(int j=0;j<n;j++)
            {
                struct Node tmp={i,j,matrix[i][j]};
                nums.push_back(tmp);
            }
        sort(nums.begin(),nums.end(),cmp);
        int len=nums.size();
        int f[len+5]={0};
        for(int i=0;i<len;i++)
        {
            // up
            int x1=nums[i].x-1,y1=nums[i].y;
            // down
            int x2=nums[i].x+1,y2=nums[i].y;
            // left
            int x3=nums[i].x,y3=nums[i].y-1;
            //right
            int x4=nums[i].x,y4=nums[i].y+1;
            if ((x1>=0)&&(matrix[x1][y1]<nums[i].val))
                f[i]=max(f[i],fm[x1][y1]);
            if ((x2<m)&&(matrix[x2][y2]<nums[i].val))
                f[i]=max(f[i],fm[x2][y2]);
            if ((y3>=0)&&(matrix[x3][y3]<nums[i].val))
                f[i]=max(f[i],fm[x3][y3]);
            if ((y4<n)&&(matrix[x4][y4]<nums[i].val))
                f[i]=max(f[i],fm[x4][y4]);
            f[i]++;
            fm[nums[i].x][nums[i].y]=f[i];
            if (f[i]>ans) ans=f[i];
        }
        return ans;
    }
    static bool cmp(Node a,Node b)
    {
        return a.val<b.val;
    }
};




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值