1
輸入兩個整數序列,第一個序列表示棧的壓入順序,請判斷第二個序列是否爲該棧的彈出順序。假設壓入棧的所有數字均不相等。例如序列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) { if(pushV.size() == 0) return false; vector<int> stack; for(int i = 0,j = 0 ;i < pushV.size();){ stack.push_back(pushV[i++]); while(j < popV.size() && stack.back() == popV[j]){ stack.pop_back(); j++; } } return stack.empty(); } }; |
2
從上往下打印出二叉樹的每個節點,同層節點從左至右打印。
解析:一時想不起二叉樹的層次遍歷.....腦子還真是笨。要判斷好如果左右子樹不是空才入對列。
class Solution {
public:
vector<int> PrintFromTopToBottom(TreeNode *root) {
queue<TreeNode*> q;
vector<int>result;
if(root==NULL)
return result;
q.push(root);
while(!q.empty()){
TreeNode*p=q.front();
q.pop();
result.push_back(p->val);
if(p->left!=NULL)
q.push(p->left);
if(p->right!=NULL)
q.push(p->right);
}
return result;
}
};
3
輸入一個整數數組,判斷該數組是不是某二叉搜索樹的後序遍歷的結果。如果是則輸出Yes,否則輸出No。假設輸入的數組的任意兩個數字都互不相同
解析:本題雖然想到了,序列中最後一個肯定爲根,然後前面一部分爲右子樹(大於根),另一部分爲左子樹(小於根)。也想到遞歸解決,但是沒想好到底怎麼遞歸。
class Solution {
bool judge(vector<int>& a, int l, int r){
if(l >= r) return true;
int i = r;
while(i > l && a[i - 1] > a[r]) --i;
for(int j = i - 1; j >= l; --j) if(a[j] > a[r]) return false;
return judge(a, l, i - 1) && (judge(a, i, r - 1));
}
public:
bool VerifySquenceOfBST(vector<int> a) {
if(!a.size()) return false;
return judge(a, 0, a.size() - 1);
}
};
4
輸入一顆二叉樹和一個整數,打印出二叉樹中結點值的和爲輸入整數的所有路徑。路徑定義爲從樹的根結點開始往下一直到葉結點所經過的結點形成一條路徑。
class Solution {
public:
vector<vector<int> >allRes;
vector<int> tmp;
void dfsFind(TreeNode * node , int left){
tmp.push_back(node->val);
if(left-node->val == 0 && !node->left && !node->right)
allRes.push_back(tmp);//找到和爲所給整數的,加入結果集。
else {
if(node->left) dfsFind(node->left, left-node->val);
if(node->right) dfsFind(node->right, left-node->val);
}
tmp.pop_back(); //回退。
}
public:
vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
if(root) dfsFind(root, expectNumber);
return allRes;
}
};
5
輸入一個複雜鏈表(每個節點中有節點值,以及兩個指針,一個指向下一個節點,另一個特殊指針指向任意一個節點),返回結果爲複製後複雜鏈表的head。(注意,輸出結果中請不要返回參數中的節點引用,否則判題程序會直接返回空)
class Solution {
public:
RandomListNode* Clone(RandomListNode* pHead)
{
if(pHead==NULL)
return NULL;
RandomListNode *pCur = pHead;
//複製next 如原來是A->B->C 變成A->A'->B->B'->C->C'
while(pCur!=NULL){
RandomListNode *node = new RandomListNode(pCur->label);
node->next = pCur->next;
pCur->next = node;
pCur = node->next;
}
pCur = pHead;
//複製random pCur是原來鏈表的結點 pCur.next是複製pCur的結點
while(pCur!=NULL){
if(pCur->random!=NULL)
pCur->next->random = pCur->random->next;
pCur = pCur->next->next;
}
RandomListNode *head = pHead->next;
RandomListNode *cur = head;
pCur = pHead;
//拆分鏈表
while(pCur!=NULL){
pCur->next = pCur->next->next;
if(cur->next!=NULL)
cur->next = cur->next->next;
cur = cur->next;
pCur = pCur->next;
}
return head;
}
};
6
輸入一棵二叉搜索樹,將該二叉搜索樹轉換成一個排序的雙向鏈表。要求不能創建任何新的結點,只能調整樹中結點指針的指向。
本題遞歸,中序遍歷二叉樹,記錄上一個節點,本節點左子樹指向上一個節點,上一個節點右子樹指向本節點。
class Solution { public: TreeNode*pre=NULL; TreeNode*lastLeft=NULL; TreeNode* Convert(TreeNode* pRootOfTree) { if(pRootOfTree==NULL) return pRootOfTree;
Convert(pRootOfTree->left); pRootOfTree->left=pre; if(pre!=NULL){ pre->right=pRootOfTree; }
pre=pRootOfTree;
lastLeft=lastLeft==NULL?pRootOfTree:lastLeft;//找到結果的開始地方。要麼最左下,要麼根節點。 Convert(pRootOfTree->right); return lastLeft; } }; |
7
求出1~13的整數中1出現的次數,並算出100~1300的整數中1出現的次數?爲此他特別數了一下1~13中包含1的數字有1、10、11、12、13因此共出現6次,但是對於後面問題他就沒轍了。ACMer希望你們幫幫他,並把問題更加普遍化,可以很快的求出任意非負整數區間中1出現的次數。
/*
設N = abcde ,其中abcde分別爲十進制中各位上的數字。
如果要計算百位上1出現的次數,它要受到3方面的影響:百位上的數字,百位以下(低位)的數字,百位以上(高位)的數字。
① 如果百位上數字爲0,百位上可能出現1的次數由更高位決定。比如:12013,則可以知道百位出現1的情況可能是:100~199,1100~1199,2100~2199,,...,11100~11199,一共1200個。可以看出是由更高位數字(12)決定,並且等於更高位數字(12)乘以 當前位數(100)。
② 如果百位上數字爲1,百位上可能出現1的次數不僅受更高位影響還受低位影響。比如:12113,則可以知道百位受高位影響出現的情況是:100~199,1100~1199,2100~2199,,....,11100~11199,一共1200個。和上面情況一樣,並且等於更高位數字(12)乘以 當前位數(100)。但同時它還受低位影響,百位出現1的情況是:12100~12113,一共114個,等於低位數字(113)+1。
③ 如果百位上數字大於1(2~9),則百位上出現1的情況僅由更高位決定,比如12213,則百位出現1的情況是:100~199,1100~1199,2100~2199,...,11100~11199,12100~12199,一共有1300個,並且等於更高位數字+1(12+1)乘以當前位數(100)。
*/
public class Solution {
public int NumberOf1Between1AndN_Solution(int n) {
int count = 0;//1的個數
int i = 1;//當前位
int current = 0,after = 0,before = 0;
while((n/i)!= 0){
current = (n/i)%10; //高位數字
before = n/(i*10); //當前位數字
after = n-(n/i)*i; //低位數字
//如果爲0,出現1的次數由高位決定,等於高位數字 * 當前位數
if (current == 0)
count += before*i;
//如果爲1,出現1的次數由高位和低位決定,高位*當前位+低位+1
else if(current == 1)
count += before * i + after + 1;
//如果大於1,出現1的次數由高位決定,//(高位數字+1)* 當前位數
else{
count += (before + 1) * i;
}
//前移一位
i = i*10;
}
return count;
}
}
8
c++如何將int轉爲string
for(int i=0;i<numbers.size();i++ ){
stringstream ss;
ss<<numbers[i];
string s = ss.str();
strNum.push_back(s);
}