[Mock] Facebook 6
6495 ワード
463. Island Perimeter
You are given row x col grid representing a map where grid[i][j] = 1 represents land and grid[i][j] = 0 represents water.
Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).
The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.
My Answer 1: Accepted (Runtime: 628 ms - 37.97% / Memory Usage: 17.9 MB - 14.51%)
class Solution:
def islandPerimeter(self, grid: List[List[int]]) -> int:
def func(grid, i, j):
if i < 0 or i > len(grid)-1 or j < 0 or j > len(grid[0])-1:
return
grid[i][j] = -1
a = 4
if i > 0 and grid[i-1][j] == 1:
a -= 1
func(grid, i-1, j)
elif i > 0 and grid[i-1][j] == -1:
a -= 1
if i < len(grid)-1 and grid[i+1][j] == 1:
a -= 1
func(grid, i+1, j)
elif i < len(grid)-1 and grid[i+1][j] == -1:
a -= 1
if j > 0 and grid[i][j-1] == 1:
a -= 1
func(grid, i, j-1)
elif j > 0 and grid[i][j-1] == -1:
a -= 1
if j < len(grid[0])-1 and grid[i][j+1] == 1:
a -= 1
func(grid, i, j+1)
elif j < len(grid[0])-1 and grid[i][j+1] == -1:
a -= 1
self.ans += a
self.ans = 0
for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
func(grid, i, j)
return self.ans
1
で再び島に戻ります経由地を
-1
に変更正方形なので
a = 4
から周囲に
1
があるところが接続されているので、a - 1
に電話して、回ってから帰ります.四方八方に
-1
があるので、つながっているところです.みんなでもう一度やってみると~
143. Reorder List
You are given the head of a singly linked-list. The list can be represented as:
a - 1
Reorder the list to be on the following form:L0 → L1 → … → Ln - 1 → Ln
You may not modify the values in the list's nodes. Only nodes themselves may be changed.My Answer 1: Time Limit Exceeded (10 / 12 test cases passed.)
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reorderList(self, head: ListNode) -> None:
"""
Do not return anything, modify head in-place instead.
"""
while head.next:
last = head
while last.next and last.next.next:
last = last.next
if last == head:
break
tmp = head.next
head.next = last.next
last.next = None
head = head.next
head.next = tmp
head = head.next
L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → …
:最後のノードを指す前のノード最後のノードは
last
ですなくてもいいです.
last.next
とhead
の間に挿入head.next
は既に着席しているので、last.next
でNone
前に2つのグリッド個数を再計算するのは難しい...
Solution 1: Accepted (Runtime: 88 ms - 86.45% / Memory Usage: 23.3 MB - 77.22%)
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reorderList(self, head: ListNode) -> None:
"""
Do not return anything, modify head in-place instead.
"""
#step 1: find middle
if not head: return []
slow, fast = head, head
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
#step 2: reverse second half
prev, curr = None, slow.next
while curr:
nextt = curr.next
curr.next = prev
prev = curr
curr = nextt
slow.next = None
#step 3: merge lists
head1, head2 = head, prev
while head2:
nextt = head1.next
head1.next = head2
head1 = head2
head2 = nextt
中間値の検索
=>
head
それぞれ1マス、slow
それぞれ2マス=>最終
fast
が中間値右半分を逆さにする
=>
slow
、prev
およびcurr
=>nextt
は反右半prev
とhead
の合併133. Clone Graph
Given a reference of a node in a connected undirected graph.
Return a deep copy (clone) of the graph.
Each node in the graph contains a value (int) and a list (List[Node]) of its neighbors.
class Node {
public int val;
public List<Node> neighbors;
}
Solution 1: Accepted (Runtime: 40 ms - 55.53% / Memory Usage: 14.7 MB - 55.68%)
"""
# Definition for a Node.
class Node:
def __init__(self, val = 0, neighbors = None):
self.val = val
self.neighbors = neighbors if neighbors is not None else []
"""
class Solution:
def cloneGraph(self, node: 'Node') -> 'Node':
if not node:
return node
m = {node: Node(node.val)}
stack = [node]
while stack:
n = stack.pop()
for neigh in n.neighbors:
if neigh not in m:
stack.append(neigh)
m[neigh] = Node(neigh.val)
m[n].neighbors.append(m[neigh])
return m[node]
DFS iterativelyprev
を使用してノードを個別に表示します.stack
で隣人に加わる...?Reference
この問題について([Mock] Facebook 6), 我々は、より多くの情報をここで見つけました https://velog.io/@jsh5408/Mock-Facebook-6テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol