
LeetCode刷题
楼上小宇
more AC, more happy!!!
展开
-
C++详解Leetcode:101. Symmetric Tree
原题思路主要就是判断一个二叉树是否左右对称,可以通过一个递归函数判断,将当前二叉树设为p,与之相同的另外一个二叉树设为q,通过递归比较p->left和q->right,p->right和q->left,最后得出二叉树是否左右对称code/** * Definition for a binary tree node. * struct TreeNode { * int val; *原创 2017-07-29 22:50:26 · 567 阅读 · 0 评论 -
C++详解Leetcode:103. Binary Tree Zigzag Level Order Traversal
原题思路与102题层次遍历最大的区别就是,此题要求先在开始层从左到右遍历,下一层再从右到左,再从左到右。。。依次类推,所以只要在层次遍历的代码中,加上一个计数变量level记录当前层,初始值为1,当下一个数是下一层时,判断level%2是否等于0,如果level为奇数则从左到右,如果为偶数,则将存储在vector中的这一层数进行逆转,然后存储到最后的结果向量中。codeclass Solution原创 2017-07-30 17:22:04 · 733 阅读 · 0 评论 -
C++详解Leetcode:104. Maximum Depth of Binary Tree
原题思路此题就是计算二叉树的最大深度,通过递归可以很轻松的进行处理code/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(原创 2017-07-30 17:25:04 · 502 阅读 · 0 评论 -
C++详解Leetcode:105. Construct Binary Tree from Preorder and Inorder Traversal
原题思路通过二叉树的前序遍历和中序遍历来构建二叉树,通过递归可以很容易的解决这个问题,在遇到二叉树的问题,应该习惯先画图再来解决code/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; *原创 2017-07-30 17:34:05 · 1405 阅读 · 0 评论 -
C++详解Leetcode:106. Construct Binary Tree from Inorder and Postorder Traversal
原题思路通过二叉树的中序遍历和后序遍历来构建二叉树,通过递归可以很简单的解决code/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x),原创 2017-07-30 17:39:46 · 681 阅读 · 1 评论 -
C++详解Leetcode:102. Binary Tree Level Order Traversal
原题思想这一题主要考查二叉树的层次遍历,最常用的方法就是用队列去做,但是需要将二叉树的节点值存到一个二维数组中去,然后返回,这需要在队列存储时候做一些操作,通过NULL来标记二叉树的层次代码和详解struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right;};class Solution原创 2017-07-22 17:44:50 · 950 阅读 · 0 评论 -
求从1到n整数中1出现的次数:O(logn)算法
剑指offer上的一题,但是感觉这位兄弟的解法更好#include<iostream>using namespace std;#define N 2000int cnt(int n){ if (n < 1) { return 0; } int count = 0; int base = 1; int round = n;原创 2017-09-29 09:27:10 · 386 阅读 · 0 评论 -
求1+2+...+n的前n项和,但不用乘除法、for、while、if、else、switch、case等关键字及条件判断语句
问题求1+2+…+n的前n项和,但不用乘除法、for、while、if、else、switch、case等关键字及条件判断语句解答#include<iostream>using namespace std;int sum(int n){ n && (n += sum(n - 1)); return n;}int main(){ int n; cin >> n;原创 2017-09-20 22:13:40 · 792 阅读 · 0 评论 -
简单小游戏-剪刀石头布的c语言实现
#include <stdio.h>#include <stdlib.h>#include <time.h>int main(void){ char gesture[3][10] = { "scissor", "stone", "cloth" }; int man, computer, result, ret; srand(time(NULL)); while (原创 2017-10-22 20:29:16 · 7275 阅读 · 0 评论