
leetcode
Faldict
Archer
展开
-
leetcode#1 Two Sum
水题一个,直接用冒泡做了,但是很奇怪的是用vector<int> iterator和size_t的运行时间差了很多。前者直接就超时了,后者AC。class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { size_t n = nums.size(); vector<i原创 2016-10-12 12:40:39 · 393 阅读 · 0 评论 -
leetcode#783. Minimum Distance Between BST Nodes
leetcode#783. Minimum Distance Between BST NodesProblem Description Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the values of any two differen...原创 2018-02-12 22:12:47 · 594 阅读 · 0 评论 -
leetcode#780. Reaching Points
780. Reaching Points [hard]Problem Description: A move consists of taking a point (x, y) and transforming it to either (x, x+y) or (x+y, y). Given a starting point (sx, sy) and a target p...原创 2018-02-12 23:09:24 · 523 阅读 · 0 评论 -
leetcode#792. Number of Matching Subsequences
792. Number of Matching SubsequencesProblem Description Given string S and a dictionary of words words, find the number of words[i] that is a subsequence of S.Analysis and Solution基本思路是对wor...原创 2018-03-07 09:14:26 · 665 阅读 · 0 评论 -
leetcode#779 K-th Symbol in Grammar
EZ. 观察变换规律可知, 一个数字变换成的两个数字第一个还是他自己,另一个是他的反。所以把这n次操作看成完全二叉树,自底向上搜索即可。class Solution {public: int kthGrammar(int N, int K) { int flag = 0; while (N > 1) { flag = K % ...原创 2018-02-08 00:29:53 · 372 阅读 · 0 评论 -
leetcode#790. Domino and Tromino Tiling
790. Domino and Tromino TilingProblem Description We have two types of tiles: a 2x1 domino shape, and an “L” tromino shape. These shapes may be rotated. XX <- domino XX ...原创 2018-02-27 20:37:43 · 472 阅读 · 0 评论 -
leetcode#795. Number of Subarrays with Bounded Maximum
795. Number of Subarrays with Bounded MaximumProblem Description We are given an array A of positive integers, and two positive integers L and R (L <= R). Return the number of (contiguou...原创 2018-03-08 10:32:38 · 622 阅读 · 0 评论 -
leetcode#789. Escape The Ghosts
789. Escape The GhostsProblem Description You are playing a simplified Pacman game. You start at the point (0, 0), and your destination is (target[0], target[1]). There are several ghosts on the...原创 2018-02-28 15:35:47 · 300 阅读 · 0 评论 -
leetcode#688. Knight Probability in Chessboard
688. Knight Probability in Chessboard题目描述 在NxN棋盘上,骑士从第r行和第c列开始,并尝试进行K次移动。 行和列都是0索引的,所以左上角的正方形是(0,0),右下角的正方形是(N-1,N-1)。 象棋骑士有8种可能的动作,如下图所示。 每个移动都是基本方向上的两个方格,然后是正交方向上的一个方格。 骑士每移动一次...原创 2018-03-01 09:26:32 · 507 阅读 · 0 评论 -
leetcode#791. Custom Sort String
791. Custom Sort StringProblem Description S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sorted in some custom order previously. We want to...原创 2018-03-09 08:40:52 · 404 阅读 · 0 评论 -
leetcode#788. Rotated Digits
788. Rotated DigitsProblem Description X is a good number if after rotating each digit individually by 180 degrees, we get a valid number that is different from X. Each digit must be rotated ...原创 2018-03-09 13:37:59 · 669 阅读 · 0 评论 -
leetcode#689. Maximum Sum of 3 Non-Overlapping Subarrays
689. Maximum Sum of 3 Non-Overlapping SubarraysProblem Description In a given array nums of positive integers, find three non-overlapping subarrays with maximum sum. Each subarray will be o...原创 2018-03-02 10:18:56 · 330 阅读 · 0 评论 -
leetcode#786. K-th Smallest Prime Fraction
786. K-th Smallest Prime FractionProblem Description A sorted list A contains 1, plus some number of primes. Then, for every p < q in the list, we consider the fraction p/q. What is the...原创 2018-02-25 14:14:33 · 630 阅读 · 0 评论 -
leetcode#793. Preimage Size of Factorial Zeroes Function
793. Preimage Size of Factorial Zeroes FunctionProblem Description Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * … * x, and by convention, 0! = 1.) For ...原创 2018-03-05 21:58:01 · 850 阅读 · 1 评论 -
leetcode# 669. Trim a Binary Search Tree
Problem Description Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the roo...原创 2018-02-11 21:24:47 · 274 阅读 · 0 评论 -
leetcode#773. Sliding Puzzle
leetcode#773. Sliding PuzzleProblem Description On a 2x3 board, there are 5 tiles represented by the integers 1 through 5, and an empty square represented by 0. A move consists of choosin...原创 2018-02-10 19:22:17 · 612 阅读 · 0 评论 -
leetcode#321 Create Maximum Numbers
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the >maximum number of length k <= m + n from digits of the two. The relative order of the digits >from the same arra原创 2016-10-13 19:14:52 · 447 阅读 · 0 评论 -
leetcode#77 Combinations
题目描述 Given two integers n and k, return all possible combinations of k numbers out of 1 … n. For example, If n = 4 and k = 2, a solution is: [ [2,4], [3,4],原创 2016-10-15 08:58:43 · 397 阅读 · 0 评论 -
leetcode#94
Given a binary tree, return the inorder traversal of its nodes’ values. 给定一棵二叉树,返回它的中序遍历直接递归解决就可以了,没什么好说的。需要注意的是检查节点值是否为空。/** * Definition for a binary tree node. * struct TreeNode { * int v原创 2017-03-19 21:38:51 · 340 阅读 · 0 评论 -
leetcode#105 Construct Binary Tree from Preorder and Inorder Traversal
Desciption Given preorder and inorder traversal of a tree, construct the binary tree.这道题很简单,先序的第一个元素就是根元素,然后在中序中找到对应的位置,则左边的就是左子树,右边的即为右子树。值得注意的是,C++切片操作很难用,所以我就用Go写的解答。最后大部分debug时间都在解决语法问题T_TSoluti原创 2017-03-28 18:54:29 · 312 阅读 · 0 评论 -
leetcode#224 Basic Calculator
题目描述: Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ), the plus + or >minus sign -, non-negative integers原创 2017-03-23 15:35:31 · 411 阅读 · 0 评论 -
leetcode#101 Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree [1,2,2,3,4,4,3] is symmetric But the following [1,2,2,null,3,null,3] i原创 2017-03-14 19:04:10 · 339 阅读 · 0 评论 -
leetcode#99 Recover Binary Search Tree
Description: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure.Solution:这道题如果不考虑空间占用的话只要中序遍历找到乱序的两个数就可以了。但是题目中有说最好使用一个空间为O(1)的算法原创 2017-04-17 20:40:04 · 352 阅读 · 0 评论 -
leetcode#437 Path Sum III
Description: You are given a binary tree in which each node contains an integer value. Find the number of paths that sum to a given value. The path does not need to start or end at the root原创 2017-04-16 19:06:52 · 257 阅读 · 0 评论 -
leetcode#563 Binary Tree Tilt
Description: Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the absolute difference between the sum of all >left subtree node values and the sum of原创 2017-04-25 14:39:01 · 532 阅读 · 0 评论 -
leetcode#561 Array Partition I
Description: Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), …, (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as l原创 2017-04-25 14:59:38 · 1263 阅读 · 0 评论 -
leetcode#463 Island Perimeter
Description: You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid原创 2017-04-25 15:14:35 · 362 阅读 · 0 评论 -
leetcode#777. Swap Adjacent in LR String
leetcode#777. Swap Adjacent in LR StringProblem Description: In a string composed of ‘L’, ‘R’, and ‘X’ characters, like “RXXLRXRXL”, a move consists of either replacing one occurrence of “XL” wi...原创 2018-02-08 20:13:04 · 709 阅读 · 0 评论 -
leetcode#794. Valid Tic-Tac-Toe State
794. Valid Tic-Tac-Toe StateProblem Description A Tic-Tac-Toe board is given as a string array board. Return True if and only if it is possible to reach this board position during the course of ...原创 2018-03-06 10:46:16 · 576 阅读 · 0 评论