
leetcode
hrz希望可以多多交流
这个作者很懒,什么都没留下…
展开
-
Leetcode 429. N叉树的层序遍历
注意queue不断改变,先用n存下queue的大小。第二点是要对root进行判断是否为空。/*// Definition for a Node.class Node {public: int val = NULL; vector<Node*> children; Node() {} Node(int _val, vector<N...原创 2018-07-23 14:43:24 · 1062 阅读 · 0 评论 -
Leetcode 287. 寻找重复数
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, f...原创 2018-07-14 22:57:41 · 1055 阅读 · 2 评论 -
Convertion of grey code and binary 格雷码和二进制数之间的转换
摘自维基百科的转换代码:/* The purpose of this function is to convert an unsingned binary number to reflected binary Grey code.*/unsigned int binary2Grey(unsigned int num){ return (num>>1)^num...转载 2018-07-14 10:53:23 · 280 阅读 · 0 评论 -
Leetcode 442. 数组中重复的数据
题目:给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次。找到所有出现两次的元素。你可以不用到任何额外空间并在O(n)时间复杂度内解决这个问题吗?思路:这类问题的重要条件是1 ≤ a[i] ≤ n。class Solution {public: vector<int> findDuplicates(vector<...原创 2018-07-14 10:28:39 · 1310 阅读 · 0 评论 -
Leetcode 797. All Paths From Source to Target
class Solution {public: vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) { vector<vector<int>> res; vector<int&g原创 2018-07-13 23:30:35 · 150 阅读 · 0 评论 -
Leetcode 859.亲密字符串
class Solution {public: bool buddyStrings(string A, string B) { int n1=A.size(),n2=B.size(); if(n1!=n2) return false; int ind1,ind2,cnt=0; vector<int> v(26,0...原创 2018-07-13 23:17:45 · 632 阅读 · 0 评论 -
Leetcode 861.翻转矩阵后的得分
class Solution {public: int matrixScore(vector<vector<int>>& A) { int m=A.size(),n=A[0].size(); int res=0, k=1; for(int i=0;i<m;++i){ if(A[i...原创 2018-07-13 23:16:27 · 432 阅读 · 0 评论 -
Leetcode 700. 二叉搜索树中的搜索
给定二叉搜索树(BST)的根节点和一个值。 你需要在BST中找到节点值等于给定值的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 NULL。例如,给定二叉搜索树: 4 / \ 2 7 / \ 1 3和值: 2你应该返回如下子树: 2 / \ 1 ...原创 2018-07-19 20:20:00 · 1018 阅读 · 0 评论 -
Leetcode 515. 在每个树行中找最大值
思路:简单的二叉树层次遍历的推广。二叉树层次遍历是典型的广度优先搜索(BFS)的应用。代码如下:/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : v...原创 2018-07-14 23:05:33 · 289 阅读 · 0 评论