LeetCode 2015.7.28-2015.8.3 50,49,55,120,199,63,40

LeetCode 2015.7.28-2015.8.3 50,49,55,120,199,63,40

50 Pow(x, n) 2015.7.28
class Solution {
public:
    double myPow(double x, int n) {
        bool flag = false;
        if (n<0)
        {
            flag =true;
            n=-n;
        }
        double r = 1, base = x;
        while (n!=0)
        {
            if (n%2)
                r*=base;
            base*=base;
            n/=2;
        }
        if (flag) return 1.0/r;else return r;
    }
};

49 Anagrams 2015.7.28
class Solution {
public:
    vector<string> anagrams(vector<string>& strs) {
        unordered_map <string,int> exist;
        vector<string> ans;
        for(int i=0;i<strs.size();i++)
        {
            string temp = strs[i];
            sort(temp.begin(),temp.end());
            if (exist.find(temp)==exist.end())
                exist[temp]=i;
            else
            {
                if (exist[temp]!=-1)
                    ans.push_back(strs[exist[temp]]);
                ans.push_back(strs[i]);
                exist[temp]=-1;
            }
        }
        return ans;
    }
};

55 Jump Game 2015.7.28
class Solution {
public:
    bool canJump(vector<int>& nums) {
        int n=nums.size();
        bool f[n];
        f[0]=true;
        for(int i=1;i<n;i++)
        {
            f[i]=false;
            for(int j=i-1;j>=0;j--)
            {
                if (f[j] && nums[j]>=i-j)
                {
                    f[i]=true;
                    break;
                }
            }
        }
        return f[n-1];
    }
};

120 Triangle 2015.7.31
class Solution {
public:
    int minimumTotal(vector< vector<int> >& triangle) {
        int ans = 0;
        if (triangle.size()==0) return 0;
        if (triangle.size()==1) return triangle[0][0];
        for(int i=triangle.size()-2;i>=0;i--)
        {
            for(int j=0;j<=i;j++)
            {
                triangle[i][j]+=min(triangle[i+1][j],triangle[i+1][j+1]);
            }
        }
        return triangle[0][0];
    }
};

199 Binary Tree Right Side View 2015.7.31
class Solution {
public:
    vector<int> depv;
    vector<int> rightSideView(TreeNode* root) {
        if (root==NULL) return depv;
        Search(root,0);
        return depv;
    }
    void Search(TreeNode* root,int depth) {
        if (depth+1 > depv.size())
            depv.push_back(root->val);
        else
            depv[depth] = root->val;
        if (root->left!=NULL) Search(root->left,depth+1);
        if (root->right!=NULL) Search(root->right,depth+1);
        return ;
    }
};

63 Unique Paths II 2015.8.2
class Solution {
public:
    int uniquePathsWithObstacles(vector< vector<int> >& obstacleGrid) {
        if (obstacleGrid.size()==0) return 0;
        int n=obstacleGrid.size()+1;
        int m=obstacleGrid[0].size()+1;
        int grid[n][m];
        for(int i=0;i<m;i++)
            grid[0][i]=-1;
        for(int i=0;i<n;i++)
            grid[i][0]=-1;
        for(int i=0;i<obstacleGrid.size();i++)
            for(int j=0;j<obstacleGrid[0].size();j++)
                if (obstacleGrid[i][j]==1)
                    grid[i+1][j+1]=-1;
                else grid[i+1][j+1]=0;
        grid[1][0] = 1;
        for(int i=1;i<n;i++)
            for(int j=1;j<m;j++)
            {
                if (grid[i][j]<0) continue;
                if (grid[i-1][j]>=0) grid[i][j]+=grid[i-1][j];
                if (grid[i][j-1]>=0) grid[i][j]+=grid[i][j-1];
            }
        if (grid[n-1][m-1]<0) return 0;
        else
        return grid[n-1][m-1];
    }
};

40 Combination Sum II 2015.8.3
class Solution {
public:
    vector< vector<int> > ans;
    vector<int> tmp;
    vector<int> c;
    vector<int> cnt;

    vector< vector<int> > combinationSum2(vector<int>& candidates, int target) {
        int m=candidates.size();
        vector<int> tmpcnt;
        tmpcnt.resize(target+1);
        for(int i=0;i<m;i++)
            if (candidates[i]<=target)
                tmpcnt[candidates[i]]++;
        sort(candidates.begin(),candidates.end());
        for(int i=0;i<candidates.size();i++)
        {
            if (candidates[i]>target) break;
            if (tmpcnt[candidates[i]]>0)
            {
                c.push_back(candidates[i]);
                cnt.push_back(tmpcnt[candidates[i]]);
                tmpcnt[candidates[i]]=-1;
            }
        }
        int n=c.size();
        depthSearch(target,target,0,n);
        return ans;
    }
    void depthSearch(int target,int left,int index,int n)
    {
        if (left == 0)
        {
            vector<int> t(tmp.begin(),tmp.end());
            sort(t.begin(),t.end());
            ans.push_back(t);
            return ;
        }
        for(int i=index;i<n;i++)
        {
            int num = min(left / c[i],cnt[i]);
            for(int j=1;j<=num;j++)
            {
                left -=c[i]*j;
                cnt[i] -= j;
                for(int k=1;k<=j;k++)
                    tmp.push_back(c[i]);
                depthSearch(target,left,i+1,n);
                left +=c[i]*j;
                cnt[i] += j;
                for(int k=1;k<=j;k++)
                    tmp.pop_back();
            }
        }
        return;
    }
};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值