随笔分类 -  graph(dfs)

289. Game of Life
摘要:According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970." Given a board with m by n... 阅读全文

posted @ 2018-11-09 07:36 猪猪🐷

834. Sum of Distances in Tree
摘要:An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0] and edges[i][1] together. 阅读全文

posted @ 2018-11-09 06:55 猪猪🐷

489. Robot Room Cleaner
摘要:Given a robot cleaner in a room modeled as a grid. Each cell in the grid can be empty or blocked. The robot cleaner with 4 given APIs can move forward, turn left or turn right. Each turn it made is... 阅读全文

posted @ 2018-11-09 06:48 猪猪🐷

419. Battleships in a Board
摘要:Given an 2D board, count how many battleships are in it. The battleships are represented with 'X's, empty slots are represented with '.'s. You may assume the following rules: * You receive a valid bo... 阅读全文

posted @ 2018-11-08 16:38 猪猪🐷

529. Minesweeper
摘要: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 阅读全文

posted @ 2018-11-08 16:35 猪猪🐷

695. Max Area of Island
摘要: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.) You may assume all four edges of the grid are surro... 阅读全文

posted @ 2018-11-08 16:29 猪猪🐷

465. Optimal Account Balancing
摘要:A group of friends went on holiday and sometimes lent each other money. For example, Alice paid for Bill's lunch for $10. Then later Chris gave Alice $5 for a taxi ride. We can model each transactio... 阅读全文

posted @ 2018-11-06 09:43 猪猪🐷

694. Number of Distinct Islands
摘要: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.) You may assume all four edges of the grid are surro... 阅读全文

posted @ 2018-11-06 09:42 猪猪🐷

802. Find Eventual Safe States
摘要:Approach #2: Depth-First Search [Accepted] Intuition As in Approach #1, the crux of the problem is whether you reach a cycle or not. Let us perform a 阅读全文

posted @ 2018-11-06 09:41 猪猪🐷

547. Friend Circles
摘要:There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a direct friend of B, and B is a direct friend of C, then... 阅读全文

posted @ 2018-11-06 09:37 猪猪🐷

200. Number of Islands
摘要: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 adjacent lands horizontally or vertically. You may assum... 阅读全文

posted @ 2018-11-06 09:36 猪猪🐷

679. 24 Game
摘要:https://leetcode.com/problems/24-game/solution/ You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated through *, /, +, -, (, ) to get the value of 24. E... 阅读全文

posted @ 2018-11-06 09:33 猪猪🐷

529. Minesweeper
摘要:Example 2: 阅读全文

posted @ 2018-09-25 04:18 猪猪🐷

737. Sentence Similarity II
摘要:737. Sentence Similarity II Given two sentences words1, words2 (each represented as an array of strings), and a list of similar word pairs pairs, determine if two sentences are similar. For example... 阅读全文

posted @ 2018-09-20 18:26 猪猪🐷

399. Evaluate Division
摘要:399. Evaluate Division Equations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number). Given some queries, return th... 阅读全文

posted @ 2018-08-28 21:19 猪猪🐷

684. Redundant Connection
摘要:In this problem, a tree is an undirected graph that is connected and has no cycles. The given input is a graph that started as a tree with N nodes (wi 阅读全文

posted @ 2018-08-28 20:08 猪猪🐷

323. Number of Connected Components in an Undirected Graph
摘要: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 find the number of connected 阅读全文

posted @ 2018-08-09 17:52 猪猪🐷

261. Graph Valid Tree
摘要:valid tree: no cycle, one connected component Given n nodes labeled from 0 to n-1 and a list of undirected edges (each edge is a pair of nodes), write 阅读全文

posted @ 2018-08-09 17:51 猪猪🐷

导航