/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
*/
class Solution {
public:
int kth;
TreeNode * ans;
void dfs(TreeNode * p){
if(p==NULL||kth<1)return;
dfs(p->left);
if(kth==1)ans=p;
if(--kth>0)dfs(p->right);
}
TreeNode* KthNode(TreeNode* pRoot, int k)
{
if(pRoot==NULL)return NULL;
ans=NULL;
kth=k;
dfs(pRoot);
return ans;
}
};
再附上非遞歸的寫法..可以更理解這個過程
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
*/
class Solution {
public:
TreeNode* KthNode(TreeNode* pRoot, int k)
{
if(pRoot==NULL)return NULL;
stack<TreeNode *>sta;
TreeNode * p=pRoot;
while(!sta.empty()||p)
{
while(p)
{
sta.push(p);
p=p->left;
}
TreeNode * top=sta.top();sta.pop();
if((--k)==0)return top;
p=top->right;
}
return NULL;
}
};
(1)二維數組中的查找
class Solution {
public:
bool Find(int target, vector<vector<int> > array) {
int leny=array[0].size()-1;
int lenx=array.size()-1;
if(lenx==-1||leny==-1)return false;
int y=0,x=lenx;
while(x>=0&&y<=leny&&array[x][y]!=target)
{
if(array[x][y]<target)y++;
else x--;
}
if(x>=0&&y<=leny)return true;return false;
}
};
(2)替換空格
class Solution {
public:
void replaceSpace(char *str,int length) {
char ans[length*3];
int k=0;
for(int i=0;i<length;i++)
{
if(str[i]==' ')
{
ans[k++]='%';
ans[k++]='2';
ans[k++]='0';
}
else
{
ans[k++]=str[i];
}
}
for(int i=0;i<k;i++)
{
str[i]=ans[i];
}
str[k]='\n';
}
};
(3)用兩個棧實現隊列
class Solution
{
public:
void push(int node) {
stack1.push(node);
}
int pop() {
while(!stack1.empty())
{
stack2.push(stack1.top());
stack1.pop();
}
int a=stack2.top();
stack2.pop();
while(!stack2.empty())
{
stack1.push(stack2.top());
stack2.pop();
}
return a ;
}
private:
stack<int> stack1;
stack<int> stack2;
};
(4)旋轉數組最小值
class Solution {
public:
int minNumberInRotateArray(vector<int> rotateArray) {
if(rotateArray.size()==0)return 0;
int ans=rotateArray[0];
int l=0,r=rotateArray.size()-1;
while(r>l)
{
int mid=(r+l)/2;
if(rotateArray[mid]>rotateArray[l]){l=mid;continue;}
if(rotateArray[mid]<rotateArray[r]){r=mid;continue;}
if(rotateArray[l]>rotateArray[r])break;
}
ans=min(ans,rotateArray[r]);
return ans;
}
};
(5)斐波那契數列
class Solution {
public:
int Fibonacci(int n) {
if(n==0)return 0;
if(n<=2)return 1;
return Fibonacci(n-1)+Fibonacci(n-2);
}
};
480ms 771k
class Solution {
public:
int Fibonacci(int n) {
if(n==0)return 0;
if(n<=2)return 1;
int a=1,b=1;
n-=2;
while(n--)
{
a=a+b;
b=a-b;
}
return a;
}
};
5ms 480
不僅避免了遞歸調用的棧消耗而且時間消耗也短了很多...仔細想想的話上面的遞歸是很蠢的.相當於每一個都從n回推到0和1,真的很蠢.我很抱歉.....
(6)跳臺階
class Solution {
public:
int jumpFloor(int number) {
if(number==1)return 1;
if(number==2)return 2;
return jumpFloor(number-1)+jumpFloor(number-2);
}
};
608 488k....這裏....改成非遞歸試試.
class Solution {
public:
int jumpFloor(int number) {
if(number==1)return 1;
if(number==2)return 2;
number-=2;
int a=2,b=1;
while(number--)
{
a=a+b;
b=a-b;
}
return a;
}
};
4 492
(7)變態跳臺階
class Solution {
public:
int jumpFloorII(int number) {
return 1<<(number-1);
}
};
(8)矩形覆蓋
class Solution {
public:
int rectCover(int number) {
int n=number;
int a=2;
int b=1;
if(n<=0)return 0;
if(n==1)return 1;
if(n==2)return 2;
else
{
n-=2;
while(n--)
{
b=a+b;
swap(a,b);
}
return a;
}
}
};
(9)二進制中1的個數
class Solution {
public:
int NumberOf1(int n) {
int ans=0;
for(int i=0;i<=31;i++)
{
if((1<<i)&n)ans++;
}
return ans;
}
};
(8)實現pow
class Solution {
public:
double Power(double base, int exponent) {
if(base==0)return 0;
if(exponent==0)return 1;
if(exponent<0){exponent*=-1;base=1.0/base;}
double ans=1.0;
while(exponent)
{
if(exponent%2==1){ans*=base;exponent--;}
if(exponent!=0){
exponent/=2;
base=base*base;
}
}
return ans;
}
};
不想寫了 我要吐了 剩下的再說吧。。。。反正暑期實習沒機會了 4.20前報名 我人傻了 明明6月底纔去,,這麼早,,看來就業形勢嚴峻啊。。。。