题目描述
输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
class Solution {
public:
vector<int> printMatrix(vector<vector<int> > matrix) {
vector<int> result;
if(!matrix.empty()){
int height = matrix.size();
int width = matrix[0].size();
int i = width<height?width:height;
int temp = (i+1)/2;
i = 0;
while(i < temp){
for(int j = i; j < width-i; j++){
result.push_back(matrix[i][j]);
}
for(int j = i+1; j < height-i-1; j++){
result.push_back(matrix[j][width-i-1]);
}
for(int j = width-i-1; j >= i && height-i-1 != i; j--){
result.push_back(matrix[height-i-1][j]);
}
for(int j = height-i-2; j > i && width-i-1 != i; j--){
result.push_back(matrix[j][i]);
}
i++;
}
}
return result;
}
};
题目描述
定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。
class Solution {
public:
void push(int value) {
if(tempStack.empty()){
minStack.push(value);
}else{
int minTop = minStack.top();
minTop = minTop<value?minTop:value;
minStack.push(minTop);
}
tempStack.push(value);
}
void pop() {
if(!tempStack.empty()){
tempStack.pop();
minStack.pop();
}
}
int top() {
if(!tempStack.empty()){
return tempStack.top();
}
return -1;
}
int min() {
if(!minStack.empty()){
return minStack.top();
}
return -1;
}
private:
stack<int> tempStack;
stack<int> minStack;
};
题目描述
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)
class Solution {
public:
bool IsPopOrder(vector<int> pushV,vector<int> popV) {
int len = pushV.size();
if(len != popV.size() || len == 0){
return false;
}
stack<int> tempStack;
int pushIndex = 0;
int popIndex = 0;
while(pushIndex < len && popIndex < len){
if(!tempStack.empty() && popV[popIndex] == tempStack.top()){
popIndex++;
tempStack.pop();
}else{
tempStack.push(pushV[pushIndex++]);
}
}
while(popIndex < len){
if(!tempStack.empty() && popV[popIndex] == tempStack.top()){
popIndex++;
tempStack.pop();
}else{
return false;
}
}
return true;
}
};
题目描述
从上往下打印出二叉树的每个节点,同层节点从左至右打印。
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
vector<int> PrintFromTopToBottom(TreeNode* root) {
queue<TreeNode*> tempQueue;
vector<int> result;
TreeNode* current = NULL;
if(root != NULL){
tempQueue.push(root);
}
while(!tempQueue.empty()){
current = tempQueue.front();
result.push_back(current -> val);
if(current -> left != NULL){
tempQueue.push(current -> left);
}
if(current -> right != NULL){
tempQueue.push(current -> right);
}
tempQueue.pop();
}
return result;
}
};
题目描述
输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。
class Solution {
public:
bool VerifySquenceOfBST(vector<int> sequence) {
int len = sequence.size();
if(len == 0){
return false;
}
if(len == 1){
return true;
}
return checkBST(sequence,0,len-1);
}
bool checkBST(vector<int>sequeue,int start,int end){
if(start == end){
return true;
}
int temp = sequeue[end];
int mid = start;
while(sequeue[mid] <= temp && mid < end){
mid++;
}
int i = mid;
while(sequeue[i] > temp && i < end){
i++;
}
if(i != end){
return false;
}
if(start < mid && mid < end){
return checkBST(sequeue,start,mid-1)&&checkBST(sequeue,mid,end-1);
}else if(start < mid){
return checkBST(sequeue,start,mid-1);
}
return checkBST(sequeue,mid,end-1);
}
};
题目描述
输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。(注意: 在返回值的list中,数组长度大的数组靠前)
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
vector<vector<int>> result;
if(root != NULL){
stack<TreeNode*> tempStack;
tempStack.push(root);
TreeNode* current = NULL;
vector<int> tempLine;
int sum = 0;
while(!tempStack.empty()){
current = tempStack.top();
tempStack.pop();
tempLine.push_back(current -> val);
sum += current -> val;
if(sum < expectNumber){
if(current -> left != NULL){
tempStack.push(current -> left);
}
if(current -> right != NULL){
tempStack.push(current -> right);
}
}else{
if(sum == expectNumber && current -> left == NULL && current -> right == NULL){
result.push_back(tempLine);
}
sum -= current -> val;
tempLine.erase(tempLine.end()-1);
}
}
sort(result.begin(),result.end());
}
return result;
}
};
题目描述
输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空)
class Solution {
public:
RandomListNode* Clone(RandomListNode*& pHead)
{
RandomListNode* newHead = NULL;
if (pHead != NULL){
cout << "pHead != NULL\n";
RandomListNode* current = pHead;
RandomListNode* temp = NULL;
while (current != NULL){
temp = new RandomListNode(current->label);
temp->next = current->next;
current->next = temp;
current = temp->next;
}
current = pHead;
while (current != NULL){
temp = current->next;
if (current->random != NULL){
temp->random = current->random->next;
}
current = current->next->next;
}
current = pHead;
newHead = pHead->next;
while (current != NULL){
temp = current->next;
current->next = temp->next;
if (temp->next != NULL){
temp->next = temp->next->next;
}
current = current->next;
}
}
return newHead;
}
};