11.輸入一個整數,輸出該數二進制表示中1的個數。其中負數用補碼錶示。數組
class Solution {
public: int NumberOf1(int n) { int count = 0; while(n){ count++; n = n & (n-1); } return count; } };
12.給定一個double類型的浮點數base和int類型的整數exponent。求base的exponent次方。數據結構
class Solution {
public: double Power(double base, int exponent) { if(exponent == 0) return 1; if(exponent == 1) return base; int sign = 1; if(exponent < 0) { sign = -1; exponent = - exponent; } double res; res = Power(base,exponent >> 1); res *= res; if(exponent >> 1 & 1){ res *= base; } if(sign == -1){ res = 1.0 / res; } return res; } };
13.輸入一個整數數組,實現一個函數來調整該數組中數字的順序,使得全部的奇數位於數組的前半部分,全部的偶數位於位於數組的後半部分,並保證奇數和奇數,偶數和偶數之間的相對位置不變。函數
class Solution {
public: void reOrderArray(vector<int> &array) { int n = array.size(); int pEven = 0; //第一個偶數的位置 for (int i = 0; i < n; i++) { if (array[i] % 2 == 0) { pEven = i; break; } } for (int i = pEven+1; i < n; i++) { //發現奇數,從第一個偶數開始,後移 if (array[i] % 2 == 1) { int temp = array[i]; for (int j = i; j >= pEven; j--) { array[j] = array[j - 1]; } array[pEven] = temp; pEven++; } } } };
14.輸入一個鏈表,輸出該鏈表中倒數第k個結點。spa
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public: ListNode* FindKthToTail(ListNode*pListHead,unsigned int k){ ListNode* p = pListHead; ListNode* q = p; while(k--){ if(p){ p = p->next; }else{ return nullptr; } } while(p){ if(p){ p = p->next; q = q->next; } } return q; } };
15.輸入一個鏈表,反轉鏈表後,輸出鏈表的全部元素。code
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public: ListNode* ReverseList(ListNode* pHead) { if(pHead == nullptr) return pHead; ListNode* fakeHead = new ListNode(-1); ListNode* p = pHead; while (p) { ListNode* fakeHead_next = fakeHead->next; ListNode* p_next = p->next; fakeHead->next = p; p->next = fakeHead_next; p = p_next; } pHead = fakeHead->next; return pHead; } };
16.輸入兩個單調遞增的鏈表,輸出兩個鏈表合成後的鏈表,固然咱們須要合成後的鏈表知足單調不減規則。blog
class Solution {
public: ListNode* Merge(ListNode* pHead1, ListNode* pHead2){ ListNode* fakeHead = new ListNode(-1); ListNode* p = fakeHead; ListNode* p1 = pHead1; ListNode* p2 = pHead2; while(p1 && p2){ if(p1->val <= p2->val){ p->next = p1; p1 = p1->next; }else{ p->next = p2; p2= p2->next; } p = p->next; } if(p1) p->next = p1; if(p2) p->next =p2; return fakeHead->next; } };
17.輸入兩棵二叉樹A,B,判斷B是否是A的子結構。(ps:咱們約定空樹不是任意一個樹的子結構)排序
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
private: bool doHasSubtree(TreeNode* pRoot1, TreeNode* pRoot2) { if (pRoot2 == nullptr) return true; if (pRoot1 == nullptr) return false; return ((pRoot1->val == pRoot2->val) && doHasSubtree(pRoot1->left, pRoot2->left) && doHasSubtree(pRoot1->right, pRoot2->right)) || doHasSubtree(pRoot1->left, pRoot2) || doHasSubtree(pRoot1->right, pRoot2); } public: bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2) { if (pRoot2 == nullptr) //空樹不是任意一個子樹的子結構 return false; return doHasSubtree(pRoot1,pRoot2); } };
18.操做給定的二叉樹,將其變換爲源二叉樹的鏡像。ci
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public: void Mirror(TreeNode* pRoot) { if(pRoot == nullptr) return; TreeNode* temp = pRoot->left; pRoot->left = pRoot->right; pRoot->right = temp; Mirror(pRoot->left); Mirror(pRoot->right); } };
19.輸入一個矩陣,按照從外向裏以順時針的順序依次打印出每個數字,例如,若是輸入以下矩陣: 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.get
class Solution {
private: void printMatrix(vector<vector<int>>& matrix, vector<int>& res, int circle) { int endRow = matrix.size() - 1 - circle; int endCol = matrix[0].size() - 1 - circle; //從左到右 for (int i = circle; i <= endCol; i++) { res.push_back(matrix[circle][i]); } //從上往下 for (int i = circle + 1; i <= endRow; i++) { res.push_back(matrix[i][endCol]); } //從右往左 if (circle < endCol && circle < endRow) { for (int i = endCol - 1; i >= circle; i--) { res.push_back(matrix[endRow][i]); } } //從下往上 if (circle < endCol && circle < endRow) { for (int i = endRow - 1; i > circle; i--) { res.push_back(matrix[i][circle]); } } } public: vector<int> printMatrix(vector<vector<int>>& matrix) { vector<int> res; if (matrix.empty()) return res; int rows = matrix.size(); int cols = matrix[0].size(); int circle = 0; while (circle * 2 < rows && circle * 2 < cols) { printMatrix(matrix, res, circle); circle++; } return res; } };
20.定義棧的數據結構,請在該類型中實現一個可以獲得棧最小元素的min函數。it
#include<cassert>
class Solution {
private: stack<int> m_data; stack<int> m_minVal; public: void push(int value) { m_data.push(value); if (m_minVal.empty() || value < m_minVal.top()) { m_minVal.push(value); } else { m_minVal.push(m_minVal.top()); } } void pop() { assert(m_data.size() > 0); m_data.pop(); m_minVal.pop(); } int top() { assert(m_data.size() > 0); return m_data.top(); } int min() { assert(m_minVal.size() > 0); return m_minVal.top(); } };