//
// Solution.cpp
// LPCPlusPlusProject
//
// Created by 鵬 劉 on 2018/3/21.
// Copyright ? 2018年 鵬 劉. All rights reserved.
//
#include "Solution.hpp"
//迭代求鏈表逆序
void Solution:: reverseList(ListNode *head) {
ListNode *pre = NULL;
ListNode *current = head;
ListNode *next = NULL;
while (current != NULL) {
next = current -> next;
current -> next = pre;
pre = current;
current = next;
}
head = pre;
}
//遞歸求鏈表逆序
void Solution:: recursiveReverseList(ListNode *head) {
if (head -> next == NULL) {
return;
}
ListNode *next = head -> next;
recursiveReverseList(next);
head -> next -> next = head;
head -> next = NULL;
}
//遞歸求階乘
int Solution:: recursiveFactorial(int n) {
if (n == 0||n == 1) {
return 1;
} else {
return n*recursiveFactorial(n-1);
}
}
//非遞歸求階乘
int Solution:: noRecursiveFactorial(int n) {
int result = 1;
for (int i = 1; i <= n; i ++) {
result = result*i;
}
return result;
}
//上下左右遞增二維數(shù)組求某數(shù)是否存在
bool Solution:: Find(int target, vector<vector<int> > array) {
long int Row = array.size();
long int Col = array[0].size();
for (long int i = 0, j = Col - 1; i < Row && j >= 0;) {
if (array[i][j] > target) {
j--;
} else if (array[i][j] == target) {
return true;
} else {
i++;
}
}
return false;
}
//請(qǐng)實(shí)現(xiàn)一個(gè)函數(shù),將一個(gè)字符串中的空格替換成“%20”。例如辈挂,當(dāng)字符串為We Are Happy.則經(jīng)過替換之后的字符串為We%20Are%20Happy。
void Solution:: replaceSpace(char *str) {
long int lenth = strlen(str);
int count = 0;
for (long int i = 0; i < lenth; i++) {
if (str[i] == ' ') {
count++;
}
}
for (long int i = lenth - 1; i >= 0; i--) {
if (str[i] != ' ') {
str[i+2*count] = str[i];
} else {
count--;
str[i+2*count] = '%';
str[i+2*count+1] = '2';
str[i+2*count+2] = '0';
}
}
printf("現(xiàn)在的str is %s\n",str);
}
//快速排序
void Solution:: fastSort(int *arr,int left,int right) {
int i = left; int j = right;
if (left > right) {
return;
}
int key = arr[left];
while (i != j) {
while (arr[j] >= key && j > i) {
j--;
}
while (arr[i] <= key && j > i) {
i++;
}
//找到左邊大于基準(zhǔn)數(shù)的跟右邊小于基準(zhǔn)數(shù)的交換位置
if (j > i) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// printf("快排后");
// for (int i = 0; i < 7; i++) {
// printf("%i,",arr[i]);
// }
// printf("\n");
}
//交換基準(zhǔn)數(shù)
arr[left] = arr[i];
arr[i] = key;
// printf("快排后");
// for (int i = 0; i < 7; i++) {
// printf("%i,",arr[i]);
// }
// printf("\n");
fastSort(arr, left, i-1);
fastSort(arr, i+1, right);
}
//非遞歸斐波那契數(shù)列求第n位數(shù)值
int Solution:: fibonacci(int n) {
int result = 0;
int a = 1;
int b = 1;
if (n == 0||n == 1) {
result = a;
return result;
}
for (int i = 2; i <= n; i++) {
result = a + b;
a = b;
b = result;
}
return result;
}
//遞歸斐波那契數(shù)列求第n位數(shù)值
int Solution:: recursiveFibonacci(int n) {
if (n == 0||n == 1) {
return 1;
}
return recursiveFibonacci(n - 1) + recursiveFibonacci(n - 2);
}
//遞歸求解楊輝三角
int Solution:: recursiveTriangle(int i,int j) {
if (j == 0 || i == j) {
return 1;
}
return recursiveTriangle(i - 1, j - 1) + recursiveTriangle(i - 1, j);
}
/*
9
/ \
4 8
/ / \
3 6 7
/ \ \
2 1 5
*/
/**
* 前序遍歷【根左右】根在最前
* 中序遍歷【左根右】根在中間
* 后續(xù)遍歷【左右根】根在最后
*/
void Solution:: initTree() {
TreeNode H = TreeNode(1);
TreeNode G = TreeNode(2);
TreeNode D = TreeNode(3);
D.left = &G;
D.right = &H;
TreeNode B = TreeNode(4);
B.left = &D;
TreeNode I = TreeNode(5);
TreeNode E = TreeNode(6);
E.right = &I;
TreeNode F = TreeNode(7);
TreeNode C = TreeNode(8);
C.left = &E;
C.right = &F;
TreeNode A = TreeNode(9);
A.left = &B;
A.right = &C;
this->PrintPreOrder(&A);
printf("二叉樹前序遍歷(遞歸):");
this->PrintPreOrderReverse(&A);
printf("\n");
this->PrintInOrder(&A);
printf("二叉樹中序遍歷(遞歸):");
this->PrintInOrderReverse(&A);
printf("\n");
// this->PrintPostOrder(&A);
printf("二叉樹后序遍歷(遞歸):");
this->PrintPostOrderReverse(&A);
printf("\n");
this->PrintFromTopToBottom(&A);
this->PrintFromBottomToTop(&A);
}
void Solution:: PrintPreOrder(TreeNode *root) {
printf("二叉樹前序遍歷(非遞歸):");
stack<TreeNode*> s;
TreeNode *t = root;
s.push(t);
while (!s.empty()) {
t = s.top();
if (t) {
printf("%i ",t -> val);
s.pop();
if (t -> right) {
s.push(t -> right);
}
if (t -> left) {
s.push(t -> left);
}
}
}
printf("\n");
}
void Solution:: PrintPreOrderReverse(TreeNode *root) {
if (root) {
printf("%i ",root -> val);
PrintPreOrderReverse(root -> left);
PrintPreOrderReverse(root -> right);
}
}
void Solution:: PrintInOrder(TreeNode *root) {//
printf("二叉樹中序遍歷(非遞歸):");
stack<TreeNode*> s;
TreeNode *t = root;
while (t != NULL || !s.empty()) {
while(t != NULL) {
s.push(t);
t = t->left;
}
if(!s.empty()) {
t = s.top();
printf("%i ",t -> val);
s.pop();
t = t->right;
}
}
printf("\n");
}
void Solution:: PrintInOrderReverse(TreeNode *root) {
if (root) {
PrintInOrderReverse(root -> left);
printf("%i ",root -> val);
PrintInOrderReverse(root -> right);
}
}
void Solution:: PrintPostOrder(TreeNode *root) {
printf("二叉樹后序遍歷(非遞歸):");
stack<TreeNode*> s;
TreeNode *t = root;
TreeNode *r = NULL;
while (t != NULL || !s.empty()) {
while(t != NULL) {
s.push(t);
t = t->left;
}
if (!s.empty()) {
t = s.top();
if (t -> right&&t -> right != r) {
t = t -> right;
} else {
t = s.top();
printf("%i ",t -> val);
r = t;
s.pop();
}
}
}
printf("\n");
}
void Solution:: PrintPostOrderReverse(TreeNode *root) {
if (root) {
PrintPostOrderReverse(root -> left);
PrintPostOrderReverse(root -> right);
printf("%i ",root -> val);
}
}
vector <int> Solution:: PrintFromTopToBottom(TreeNode *root) {
printf("二叉樹從上到下 從左往右(非遞歸):");
queue <TreeNode*> q;
q.push(root);
vector <int> r;
while(!q.empty()){
root = q.front(); q.pop();
if(!root) continue;
r.push_back(root -> val);
printf("%i ",root -> val);
q.push(root -> left);
q.push(root -> right);
}
printf("\n");
return r;
}
vector <int> Solution:: PrintFromBottomToTop(TreeNode *root) {
printf("二叉樹從下到上 從右往左(非遞歸):");
queue <TreeNode*> q;
q.push(root);
stack <TreeNode*> s;
vector <int> r;
while(!q.empty()){
root = q.front(); q.pop();
if(!root) continue;
s.push(root);
q.push(root -> left);
q.push(root -> right);
}
while (!s.empty()) {
TreeNode *node = s.top();
r.push_back(node -> val);
printf("%i ",node -> val);
s.pop();
}
printf("\n");
return r;
}
void Solution:: PringDiamond() {
printf("/\\\n\\/\n");
}
2018-05-13
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來轨功,“玉大人旭斥,你說我怎么就攤上這事」沤В” “怎么了垂券?”我有些...
- 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)羡滑。 經(jīng)常有香客問我菇爪,道長(zhǎng),這世上最難降的妖魔是什么柒昏? 我笑而不...
- 正文 為了忘掉前任凳宙,我火速辦了婚禮,結(jié)果婚禮上职祷,老公的妹妹穿的比我還像新娘氏涩。我一直安慰自己,他們只是感情好堪旧,可當(dāng)我...
- 文/花漫 我一把揭開白布削葱。 她就那樣靜靜地躺著,像睡著了一般淳梦。 火紅的嫁衣襯著肌膚如雪析砸。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼胁塞,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起啸罢,我...
- 序言:老撾萬榮一對(duì)情侶失蹤编检,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后扰才,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體允懂,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年衩匣,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了蕾总。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
- 正文 年R本政府宣布映之,位于F島的核電站拦焚,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏杠输。R本人自食惡果不足惜赎败,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望蠢甲。 院中可真熱鬧僵刮,春花似錦、人聲如沸鹦牛。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽曼追。三九已至窍仰,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間礼殊,已是汗流浹背驹吮。 一陣腳步聲響...
- 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像婚陪,于是被迫代替她去往敵國和親族沃。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 有這樣一份執(zhí)職業(yè),要干很多體力活脆淹,要有耐心還要有責(zé)任心智润,要懂得金融,烹飪未辆,醫(yī)療還要具備教育與和人際關(guān)系交往的能力窟绷,...
- (稻盛哲學(xué)學(xué)習(xí)會(huì))打卡第65天 姓名:孔麗春 部門:CR 組別:反省二組 【知~學(xué)習(xí)】 誦讀《活法》第五章 與其追...
- 第18章回顧 伽羅大陸是一個(gè)巨大的島嶼遗契,這里曾經(jīng)是神族辐棒、龍族、人族與魔族的樂園牍蜂,但自從魔族的野心日益擴(kuò)大想要鏟除其...