1.打印二叉树
程序很简单,但是其中犯了一个小错误,死活找不到,写代码要注意啊
这里左右子树,要注意是node->left,结果写成root->left

vector<int> PrintFromTopToBottom(TreeNode *root) { vector<int> res; if (NULL == root) return res; TreeNode* node; deque<TreeNode*> tmp; tmp.push_back(root); while (tmp.size()) { node = tmp.front(); res.push_back(node->val); tmp.pop_front(); if (node->left) tmp.push_back(node->left); if (node->right) tmp.push_back(node->right); } return res; }
2.求1+2+...+n

class A{ public: A(){ ++n;sum+=n; } static int getsum(){ return sum; } static void reset(){ n=0;sum=0; } ~A(){} private: static int n; static int sum; }; int A::n=0; int A::sum=0; class Solution { public: int Sum_Solution(int n) { A::reset(); //必不可少,测试集会重复运行 A* tmp=new A[n]; delete[] tmp; return A::getsum(); } };
3.二叉树镜像

class Solution { public: void Mirror(TreeNode *pRoot) { if(NULL==pRoot||(NULL==pRoot->left&&NULL==pRoot->right)) return; mirrorset(pRoot); } void mirrorset(TreeNode* root){ if(NULL==root||(NULL==root->left&&NULL==root->right)) return; TreeNode *tmp=root->left; root->left=root->right; root->right=tmp; if(root->left) mirrorset(root->left); if(root->right) mirrorset(root->right); } };
4.合并两个排序的链表
剑指offer书上用的是递归,感觉不必那么麻烦,用一个循环就可以解决。

ListNode1* Merge(ListNode1* pHead1, ListNode1* pHead2) { //避免空指针 if (NULL == pHead1 || NULL == pHead2) return NULL == pHead1 ? pHead2 : pHead1; //选取两个链表中的最小值作为链表头 bool sign[2] = { false }; ListNode1 *p1 = pHead1, *p2 = pHead2, *p3, *res; if (p1->val < p2->val) { res = p1;p3 = res;p1 = p1->next;sign[0] = true; } else { res = p2;p3 = res;p2 = p2->next;sign[1] = true; } while (p1&&p2) { if (p1->val < p2->val) { if (sign[0]) { p1 = p1->next; p3 = p3->next; } else { p3->next = p1; p1 = p1->next; p3 = p3->next; } sign[0] = true;sign[1] = false; } else { if (sign[1]) { p2 = p2->next; p3 = p3->next; } else { p3->next = p2; p2 = p2->next; p3 = p3->next; } sign[0] = false;sign[1] = true; } } if (p1) { p3->next = p1; } else if (p2) { p3->next = p2; } return res; }
5.复杂链表的复制

/* struct RandomListNode { int label; struct RandomListNode *next, *random; RandomListNode(int x) : label(x), next(NULL), random(NULL) { } }; */ class Solution { public: RandomListNode* Clone(RandomListNode* pHead) { if(NULL==pHead) return NULL; //复制链表节点,并嵌入原链表 RandomListNode *p1=pHead,*p2=pHead,*res; while(p1){ RandomListNode *temp=new RandomListNode(p1->label); temp->next=p1->next; p1->next=temp; if(NULL==temp->next) break; else p1=temp->next; } //复制随机指针 p1=pHead; while(p1){ p2=p1->random; p1=p1->next; p1->random=p2; p1=p1->next; } //拆分链表 p1=pHead;p2=p1->next;res=p2; while(p1&&NULL!=p2->next){ p1->next=p2->next; p1=p1->next; p2->next=p1->next; p2=p2->next; } p1->next=NULL; return res; } };
6.顺时针打印矩阵

void print(const vector<vector<int>>& matrix, const int& len1, const int& len2, const int& n, vector<int>& res) { int i, endx = len1 - 1 - n, endy = len2 - 1 - n; //从左至右存储 for (i = n;i <= endy;++i) { res.push_back(matrix[n][i]); } //从上至下存储 if (n < endx) { for (i = n + 1;i <= endx;++i) res.push_back(matrix[i][endy]); } //从右至左打印 if (n < endx&&n < endy) { for (i = endy - 1;i >= n;--i) res.push_back(matrix[endx][i]); } //从下至上打印 if (n < endx - 1 && n < endy) { for (i = endx - 1;i >= n + 1;--i) res.push_back(matrix[i][n]); } } vector<int> printMatrix(vector<vector<int>>& matrix) { vector<int> res; int len1 = matrix.size(), len2, n = 0; if (0 == len1) { return res; } else if (1 == len1) { return matrix[0]; } else { len2 = matrix[0].size(); } while (2 * n < len1 && 2 * n < len2) { print(matrix, len1, len2, n, res); ++n; } return res; }
在VS上写程序真的是很简单,各种智能提示,在其他IDE上写程序,就得时刻注意。。。结果还是不容易找到错误
7.不能用加减乘除做加法

class Solution { public: int Add(int num1, int num2) { int sum1,sum2; do{ sum1=num1^num2; sum2=(num1&num2)<<1; num1=sum1; num2=sum2; }while(num2!=0); return sum1; } };
日了够了,就不能一遍通过,左移右移竟然能犯错误。
8.翻转字符串
注意j值的变化,机试时不带调试,实在是无语啊

class Solution { public: string ReverseSentence(string str) { int len=str.length(); if(len<=1) return str; int i,j,left,right; //首先翻转整条语句 reverse(str,0,len-1); //逐个翻转单词 left=0;right=0; for(i=0;i<len;i++){ if(str[i]!=' '){ left=i; for(j=i+1;j<=len;j++){ if(str[j]==' '||str[j]=='