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;
}
};
LeetCode 2015.7.28-2015.8.3 50,49,55,120,199,63,40
最新推荐文章于 2021-05-28 22:20:31 发布