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;
}
};