代码改变世界

随笔档案-2018年07月

[LeetCode] 42. Trapping Rain Water_hard tag: Two Pointers

2018-07-28 01:26 by Johnson_强生仔仔, 239 阅读, 收藏, 编辑
摘要: Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. 阅读全文

[LintCode] 395. Coins in a Line 2_Medium tag: Dynamic Programming, 博弈

2018-07-26 00:34 by Johnson_强生仔仔, 326 阅读, 收藏, 编辑
摘要: Description There are n coins with different value in a line. Two players take turns to take one or two coins from left side until there are no more c 阅读全文

[LintCode] 394. Coins in a Line_ Medium tag:Dynamic Programming_博弈

2018-07-25 23:16 by Johnson_强生仔仔, 320 阅读, 收藏, 编辑
摘要: Description There are n coins in a line. Two players take turns to take one or two coins from right side until there are no more coins left. The playe 阅读全文

[LeetCode] 329. Longest Increasing Path in a Matrix_Hard tag: Dynamic Programming, DFS, Memoization

2018-07-25 06:25 by Johnson_强生仔仔, 265 阅读, 收藏, 编辑
摘要: Given an integer matrix, find the length of the longest increasing path. From each cell, you can either move to four directions: left, right, up or do 阅读全文

[LeetCode] 331. Verify Preorder Serialization of a Binary Tree_Medium tag: stack

2018-07-25 05:19 by Johnson_强生仔仔, 297 阅读, 收藏, 编辑
摘要: One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, 阅读全文

[LeetCode] 255. Verify Preorder Sequence in Binary Search Tree_Medium tag: Preorder Traversal, tree

2018-07-24 06:06 by Johnson_强生仔仔, 275 阅读, 收藏, 编辑
摘要: Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary search tree. You may assume each number in the seq 阅读全文

[LeetCode] 106. Construct Binary Tree from Postorder and Inorder Traversal_Medium tag: Tree Traversal

2018-07-24 04:23 by Johnson_强生仔仔, 229 阅读, 收藏, 编辑
摘要: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. For example, 阅读全文

[LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal_Medium tag: Tree Traversal

2018-07-24 04:11 by Johnson_强生仔仔, 263 阅读, 收藏, 编辑
摘要: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. For example, 阅读全文

[LeetCode] 285. Inorder Successor in BST_Medium tag: Inorder Traversal

2018-07-24 01:09 by Johnson_强生仔仔, 231 阅读, 收藏, 编辑
摘要: Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Note: If the given node has no in-order successor in 阅读全文

[LeetCode] 230. Kth Smallest Element in a BST_Medium tag: Inorder Traversal

2018-07-24 00:51 by Johnson_强生仔仔, 295 阅读, 收藏, 编辑
摘要: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's 阅读全文

[LeetCode] 98. Validate Binary Search Tree_Medium

2018-07-22 09:50 by Johnson_强生仔仔, 250 阅读, 收藏, 编辑
摘要: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only 阅读全文

[LeetCode] 590. N-ary Tree Postorder Traversal_Easy

2018-07-22 09:09 by Johnson_强生仔仔, 499 阅读, 收藏, 编辑
摘要: Given an n-ary tree, return the postorder traversal of its nodes' values. For example, given a 3-ary tree: Return its postorder traversal as: [5,6,3,2 阅读全文

[LeetCode] 429. N-ary Tree Level Order Traversal_ Easy

2018-07-22 04:09 by Johnson_强生仔仔, 565 阅读, 收藏, 编辑
摘要: Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example, given a 3-ary tree: 阅读全文

[LeetCode] 589. N-ary Tree Preorder Traversal_Easy

2018-07-22 03:55 by Johnson_强生仔仔, 463 阅读, 收藏, 编辑
摘要: Given an n-ary tree, return the preorder traversal of its nodes' values. For example, given a 3-ary tree: Return its preorder traversal as: [1,3,5,6,2 阅读全文

[LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal

2018-07-22 00:03 by Johnson_强生仔仔, 450 阅读, 收藏, 编辑
摘要: Pre: node 先, Inorder: node in, Postorder: node 最后 PreOrder Inorder PostOrder node-> left -> right left -> node ->right left -> right ->node Recursive 阅读全文

[LeetCode]94, 144, 145 Binary Tree InOrder, PreOrder, PostOrder Traversal_Medium

2018-07-22 00:01 by Johnson_强生仔仔, 159 阅读, 收藏, 编辑
摘要: Given a binary tree, return the inorder, preorder, postorder traversal of its nodes' values. Example: 这三个题目的思路及总结都在LeetCode questions conlusion_InOrde 阅读全文

[LeetCode] 161. One Edit Distance_Medium

2018-07-20 03:45 by Johnson_强生仔仔, 196 阅读, 收藏, 编辑
摘要: Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 possiblities to satisify one edit distance apart: Exa 阅读全文

[LeetCode] 72. Edit Distance_hard tag: Dynamic Programming

2018-07-19 23:59 by Johnson_强生仔仔, 263 阅读, 收藏, 编辑
摘要: Given two words word1 and word2, find the minimum number of operations required to convert word1to word2. You have the following 3 operations permitte 阅读全文

[LeetCode] 64. Minimum Path Sum_Medium tag: Dynamic Programming

2018-07-19 06:52 by Johnson_强生仔仔, 272 阅读, 收藏, 编辑
摘要: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. 阅读全文

[LeetCode] 62. Unique Paths_ Medium tag: Dynamic Programming

2018-07-19 05:56 by Johnson_强生仔仔, 250 阅读, 收藏, 编辑
摘要: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any p 阅读全文

[LeetCode] 489. Robot Room Cleaner_Hard tag: DFS, backtracking

2018-07-19 04:59 by Johnson_强生仔仔, 81 阅读, 收藏, 编辑
摘要: You are controlling a robot that is located somewhere in a room. The room is modeled as an m x n binary grid where 0 represents a wall and 1 represent 阅读全文

[LeetCode] 394. Decode String_Medium tag: stack 666

2018-07-19 04:42 by Johnson_强生仔仔, 183 阅读, 收藏, 编辑
摘要: Given an encoded string, return it's decoded string. The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is b 阅读全文

[LeetCode] 721. Accounts Merge_Medium tag: DFS recursive

2018-07-19 04:03 by Johnson_强生仔仔, 300 阅读, 收藏, 编辑
摘要: Given a list accounts, each element accounts[i] is a list of strings, where the first element accounts[i][0] is a name, and the rest of the elements a 阅读全文

[LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive

2018-07-18 09:07 by Johnson_强生仔仔, 293 阅读, 收藏, 编辑
摘要: Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especially, this path can be either increasing or decreas 阅读全文

[LeetCode] 298. Binary Tree Longest Consecutive Sequence_Medium tag: DFS recursive

2018-07-18 06:44 by Johnson_强生仔仔, 215 阅读, 收藏, 编辑
摘要: Given a binary tree, find the length of the longest consecutive sequence path. The path refers to any sequence of nodes from some starting node to any 阅读全文

[LeetCode] 687. Longest Univalue Path_Medium tag: DFS recursive

2018-07-18 06:25 by Johnson_强生仔仔, 212 阅读, 收藏, 编辑
摘要: Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the roo 阅读全文

[LeetCode] 124. Binary Tree Maximum Path Sum_ Hard tag: DFS recursive, Divide and conquer

2018-07-18 05:47 by Johnson_强生仔仔, 259 阅读, 收藏, 编辑
摘要: Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any 阅读全文

[LeetCode] 437. Path Sum III_ Medium tag: DFS

2018-07-18 00:38 by Johnson_强生仔仔, 281 阅读, 收藏, 编辑
摘要: 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 t 阅读全文

[LeetCode] questions conclustion_Path in Tree

2018-07-18 00:29 by Johnson_强生仔仔, 353 阅读, 收藏, 编辑
摘要: Path in Tree: [LeetCode] 112. Path Sum_Easy tag: DFS input: root, target, return True if exists sum(root-> leaf) == target else False 1 [LeetCode] 257 阅读全文

[LeetCode] 221. Maximal Square _ Medium Tag: Dynamic Programming

2018-07-17 06:44 by Johnson_强生仔仔, 262 阅读, 收藏, 编辑
摘要: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area. Example: 3.2) skip left and up, jus 阅读全文

[LeetCode] 198. House Robber _Easy tag: Dynamic Programming

2018-07-17 05:10 by Johnson_强生仔仔, 270 阅读, 收藏, 编辑
摘要: You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping yo 阅读全文

[LeetCode] questions conclustion_BFS, DFS

2018-07-17 01:37 by Johnson_强生仔仔, 538 阅读, 收藏, 编辑
摘要: BFS, DFS 的题目总结. Directed graph: Directed Graph Loop detection and if not have, path to print all path. Morris Traversal_ O(1) space to change binary t 阅读全文

[LeetCode] 113. Path Sum II

2018-07-14 00:49 by Johnson_强生仔仔, 295 阅读, 收藏, 编辑
摘要: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. Note: A leaf is a node with no children. Exampl 阅读全文

[LeetCode] 257. Binary Tree Paths_ Easy tag: DFS

2018-07-13 23:42 by Johnson_强生仔仔, 268 阅读, 收藏, 编辑
摘要: Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example: 3.2) Recursive 4.. Test cases 1) empty 2) 1 3) 阅读全文

[LeetCode] 116&117. Populating Next Right Pointers in Each Node I&II_Medium tag: BFS(Dont know why leetcode tag it as DFS...)

2018-07-13 07:33 by Johnson_强生仔仔, 220 阅读, 收藏, 编辑
摘要: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to point to its ne 阅读全文

[LeetCode] 114. Flatten Binary Tree to Linked List_Medium tag: DFS

2018-07-13 07:09 by Johnson_强生仔仔, 201 阅读, 收藏, 编辑
摘要: Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look 阅读全文

[LeetCode] 112. Path Sum_Easy tag: DFS

2018-07-13 06:16 by Johnson_强生仔仔, 234 阅读, 收藏, 编辑
摘要: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. N 阅读全文

[LeetCode] 851. Loud and Rich_ Medium tag: DFS

2018-07-13 04:55 by Johnson_强生仔仔, 398 阅读, 收藏, 编辑
摘要: In a group of N people (labelled 0, 1, 2, ..., N-1), each person has different amounts of money, and different levels of quietness. For convenience, w 阅读全文

[LeetCode] 711. Number of Distinct Islands II_hard tag: DFS

2018-07-12 05:23 by Johnson_强生仔仔, 995 阅读, 收藏, 编辑
摘要: Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) Yo 阅读全文

[LeetCode] 694. Number of Distinct Islands

2018-07-11 06:17 by Johnson_强生仔仔, 583 阅读, 收藏, 编辑
摘要: Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) Yo 阅读全文

[LeetCode] 827. Making A Large Island

2018-07-10 05:33 by Johnson_强生仔仔, 532 阅读, 收藏, 编辑
摘要: In a 2D grid of 0s and 1s, we change at most one 0 to a 1. After, what is the size of the largest island? (An island is a 4-directionally connected gr 阅读全文

[LeetCode] 104. Maximum Depth of Binary Tree_Easy tag: DFS

2018-07-09 23:31 by Johnson_强生仔仔, 180 阅读, 收藏, 编辑
摘要: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest l 阅读全文

[LeetCode] 301. Remove Invalid Parentheses_Hard tag:BFS

2018-07-09 10:40 by Johnson_强生仔仔, 403 阅读, 收藏, 编辑
摘要: Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. Note: The input string may cont 阅读全文

[LeetCode] 210. Course Schedule II

2018-07-08 10:46 by Johnson_强生仔仔, 295 阅读, 收藏, 编辑
摘要: There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prerequisites, for example to take course 0 you have to 阅读全文

Directed Graph Loop detection and if not have, path to print all path.

2018-07-08 10:46 by Johnson_强生仔仔, 446 阅读, 收藏, 编辑
摘要: 这里总结针对一个并不一定所有点都连通的general directed graph, 去判断graph里面是否有loop存在, 收到启发是因为做了[LeetCode] 207 Course Schedule_Medium tag: BFS, DFS, 这个题实际上就是监测directed graph 阅读全文

[LeetCode] 785. Is Graph Bipartite?_Medium tag: DFS, BFS

2018-07-06 09:25 by Johnson_强生仔仔, 418 阅读, 收藏, 编辑
摘要: Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipartite if we can split it's set of nodes into two ind 阅读全文

[LeetCode] 286. Walls and Gates_Medium tag: BFS

2018-07-06 06:19 by Johnson_强生仔仔, 245 阅读, 收藏, 编辑
摘要: You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstacle. 0 - A gate. INF - Infinity means an empty room 阅读全文

[LeetCode] 261. Graph Valid Tree _ Medium tag: BFS

2018-07-06 04:30 by Johnson_强生仔仔, 316 阅读, 收藏, 编辑
摘要: Given n nodes labeled from 0 to n-1 and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges make 阅读全文

[LeetCode] 513. Find Bottom Left Tree Value_ Medium tag: BFS

2018-07-06 01:00 by Johnson_强生仔仔, 245 阅读, 收藏, 编辑
摘要: Given a binary tree, find the leftmost value in the last row of the tree. Example 1: Input: 2 / \ 1 3 Output: 1 Example 2: Input: 1 / \ 2 3 / / \ 4 5 阅读全文

[LeetCode] 101. Symmetric Tree_ Easy tag: BFS

2018-07-06 00:31 by Johnson_强生仔仔, 251 阅读, 收藏, 编辑
摘要: 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 symmet 阅读全文

[LeetCode] 103. Binary Tree Zigzag Level Order Traversal _ Medium tag: BFS

2018-07-05 06:27 by Johnson_强生仔仔, 124 阅读, 收藏, 编辑
摘要: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and 阅读全文

[LeetCode] 207 Course Schedule_Medium tag: BFS, DFS

2018-07-05 06:08 by Johnson_强生仔仔, 265 阅读, 收藏, 编辑
摘要: There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prerequisites, for example to take course 0 you have to 阅读全文

[LeetCode] 133. Clone Graph_ Medium tag: BFS, DFS

2018-07-05 05:06 by Johnson_强生仔仔, 157 阅读, 收藏, 编辑
摘要: Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's undirected graph serialization: Nodes are labeled 阅读全文

[LeetCode] 675. Cut Off Trees for Golf Event_Hard tag: BFS

2018-07-04 23:13 by Johnson_强生仔仔, 518 阅读, 收藏, 编辑
摘要: You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-negative 2D map, in this map: 0 represents the obstacl 阅读全文

[LeetCode] 127. Word Ladder _Medium tag: BFS

2018-07-04 10:16 by Johnson_强生仔仔, 256 阅读, 收藏, 编辑
摘要: Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, 阅读全文

[LeetCode] 102. Binary Tree Level Order Traversal_Medium tag: BFS

2018-07-04 08:20 by Johnson_强生仔仔, 233 阅读, 收藏, 编辑
摘要: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree [3 阅读全文

[LeetCode] 200. Number of Islands_ Medium tag: BFS

2018-07-04 04:40 by Johnson_强生仔仔, 371 阅读, 收藏, 编辑
摘要: Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacen 阅读全文

[LeetCode] 199. Binary Tree Right Side View_ Medium tag: BFS, Amazon

2018-07-04 04:04 by Johnson_强生仔仔, 194 阅读, 收藏, 编辑
摘要: Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. Example 阅读全文

[Leetcode] 863. All Nodes Distance K in Binary Tree_ Medium tag: BFS, Amazon

2018-07-03 09:35 by Johnson_强生仔仔, 774 阅读, 收藏, 编辑
摘要: We are given a binary tree (with root node root), a target node, and an integer value `K`. Return a list of the values of all nodes that have a distan 阅读全文

[LeetCode] 529. Minesweeper_ Medium_ tag: BFS

2018-07-03 06:04 by Johnson_强生仔仔, 292 阅读, 收藏, 编辑
摘要: Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix representing the game board. 'M' represents an unrevealed min 阅读全文

[LeetCode] 690. Employee Importance_Easy tag: BFS

2018-07-02 23:08 by Johnson_强生仔仔, 201 阅读, 收藏, 编辑
摘要: You are given a data structure of employee information, which includes the employee's unique id, his importance value and his directsubordinates' id. 阅读全文

[LeetCode] 733. Flood Fill_Easy tag: BFS

2018-07-02 11:05 by Johnson_强生仔仔, 330 阅读, 收藏, 编辑
摘要: An image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535). Given a coordinate (sr, 阅读全文
点击右上角即可分享
微信分享提示