36 -20 = 16

16 - 10 = 6

6 - 5 = 1

1 - 1 = 0

## C++指针两例

#include <iostream>

using namespace std;

void foo(const void* ptr)

{

std::cout << "In foo(void*)" << std::endl;

}

void foo(bool b)

{

std::cout << "In foo(bool)" << std::endl;

}

......

## [LeetCode C++实现]236. Lowest Common Ancestor of a Binary Tree

236. Lowest Common Ancestor of a Binary Tree

/**

* Definition for a binary tree node.

* struct TreeNode {

* int val;

* TreeNode *left;

* TreeNode *right;

* TreeNode(int x) : val(x), left(NULL), right(NULL) {}

* };

*/

class Soluti......

## [LeetCode C++实现]235. Lowest Common Ancestor of a Binary Search Tree

235. Lowest Common Ancestor of a Binary Search Tree

/**

* Definition for a binary tree node.

* struct TreeNode {

* int val;

* TreeNode *left;

* TreeNode *right;

* TreeNode(int x) : val(x), left(NULL......

## [LeetCode C++实现]64. Minimum Path Sum

64. Minimum Path Sum

class Solution {

public:

int minPathSum(vector<vector<int>>& grid) {

int m = grid.size();

int n = grid[0].size();

vector<vector<int>> sum(m,vector<int>(n,grid[0][0]));

for......

## [LeetCode C++实现]98. Validate Binary Search Tree

98. Validate Binary Search Tree

/**

* Definition for a binary tree node.

* struct TreeNode {

* int val;

* TreeNode *left;

* TreeNode *right;

* TreeNode() : val(0), left(nullptr), right(nullpt......

## [LeetCode C++实现]225. Implement Stack using Queues

225. Implement Stack using Queues这里使用队列实现stack，使用c++ stl中的deque.

class MyStack {

public:

/** Initialize your data structure here. */

MyStack() {

}

/** Push element x onto stack. */

void push(int x) {

stack.push_back(x);

}

/** Removes the element on top of the stack and returns that element. */......

## [LeetCode C++实现]704. Binary Search

class Solution {

public:

int search(vector<int>& nums, int target) {

int start = 0,end = nums.size() - 1;

while(start <= end)

{

int mid = (start + end) / 2;

if(nums[mid] == target)

return mid;

else if(nums[mid] < target)

start = mid + 1;

else

en......

## [LeetCode C++实现]233. Number of Digit One

233. Number of Digit One

class Solution {

public:

int countDigitOne(int n) {

int res = 0;

for(int i = 1;i <= n;i++)

{

res += cal_digit(i);

}

return res;

}

private:

int cal_digit(int n){

int cnt = 0;

while(n)

{

if(n % 10 == 1)

cnt++;

n /= 10......

## [LeetCode C++实现]69. Sqrt(x)

69. Sqrt(x)

class Solution {

public:

int mySqrt(int x) {

int ans = 0;

for(int i = 1;i <= x;i++)

{

//avoid overflow

if(i > x / i)

return ans;

ans = i;

}

return ans;

}

};

Runtime: 68 ms, faster than 5.95% of C++ online submissions for Sqrt(x).

Memory Usage: 6......