[LintCode] 618 Search Graph Nodes 解题报告

Description
Given a undirected graph, a node and a target, return the nearest node to given node which value of it is target, return NULL if you can‘t find.

There is a mapping store the nodes‘ values in the given parameters.

Notice
It‘s guaranteed there is only one available solution

Example
2------3 - 5
 \        |     |
   \      |     |
     \    |     |
       \  |     |
         1 -- 4
Give a node 1, target is 50

there a hash named values which is [3,4,10,50,50], represent:
Value of node 1 is 3
Value of node 2 is 4
Value of node 3 is 10
Value of node 4 is 50
Value of node 5 is 50

Return node 4

5/13/2017

算法班,未经验证

不是很明白label和values的关系

 1 /**
 2  * Definition for graph node.
 3  * class UndirectedGraphNode {
 4  *     int label;
 5  *     ArrayList<UndirectedGraphNode> neighbors;
 6  *     UndirectedGraphNode(int x) {
 7  *         label = x; neighbors = new ArrayList<UndirectedGraphNode>();
 8  *     }
 9  * };
10  */
11 public class Solution {
12     /**
13      * @param graph a list of Undirected graph node
14      * @param values a hash mapping, <UndirectedGraphNode, (int)value>
15      * @param node an Undirected graph node
16      * @param target an integer
17      * @return the a node
18      */
19     public UndirectedGraphNode searchNode(ArrayList<UndirectedGraphNode> graph,
20                                           Map<UndirectedGraphNode, Integer> values,
21                                           UndirectedGraphNode node,
22                                           int target) {
23         // Write your code here
24         if (graph == null) return ret;
25         UndirectedGraphNode ret = null;
26         Set<UndirectedGraphNode> set = new HashSet<UndirectedGraphNode>();
27
28         Queue<UndirectedGraphNode> queue = new LinkedList<UndirectedGraphNode>();
29         queue.offer(node);
30         while (!queue.isEmpty()) {
31             UndirectedGraphNode t = queue.poll();
32             if (values.get(t) == target) {
33                 return t;
34             }
35             for (UndirectedGraphNode n: t.neighbors) {
36                 if (!set.contains(n)) {
37                     set.add(n);
38                     queue.offer(n);
39                 }
40             }
41         }
42         return null;
43     }
44 }
时间: 2024-10-12 16:08:09

[LintCode] 618 Search Graph Nodes 解题报告的相关文章

【原创】leetCodeOj --- Binary Search Tree Iterator 解题报告

时间挤挤总是有的 太久不做题,脑子都生锈了.来道水题练练手 题目地址: https://leetcode.com/problems/binary-search-tree-iterator/ 题目内容: Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the

LeetCode: Search Insert Position 解题报告

Search Insert Position Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples.[1,3,5,

LeetCode: Binary Search Tree Iterator 解题报告

Binary Search Tree Iterator Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in

Lintcode: Implement Queue by Stacks 解题报告

Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As the title described, you should only use two stacks to implement a queue's actions. The queue should support push(element), pop() and top() where pop is

LeetCode: Unique Binary Search Trees II 解题报告

Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example, Given n = 3, your program should return all 5 unique BST's shown below. 1         3     3      2      1 \      

[LintCode] 598 Zombie in Matrix 解题报告

DescriptionGiven a 2D grid, each cell is either a wall 2, a zombie 1 or people 0 (the number zero, one, two).Zombies can turn the nearest people(up/down/left/right) into zombies every day, but can not through wall. How long will it take to turn all p

LeetCode: Search in Rotated Sorted Array II 解题报告

Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the arr

【LeetCode】Word Search II 解题报告

[题目] Given a 2D board and a list of words from the dictionary, find all words in the board. Each word must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The sa

leetCode解题报告5道题(九)

题目一: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], [2,3], [1,2], [1,3], [1,4], ] 分析: 题意给我们一个数字n, 和一个数字k,让我们求出从 1~~n中取出k个数所能得到的组合数 所