Algorithm Flashcards
- Pyramid Transition Matrix
We are stacking blocks to form a pyramid. Each block has a color which is a one letter string, like 'Z'
.
For every block of color C
we place not in the bottom row, we are placing it on top of a left block of color A
and right block of color B
. We are allowed to place the block there only if (A, B, C)
is an allowed triple.
We start with a bottom row of bottom, represented as a single string. We also start with a list of allowed triples allowed. Each allowed triple is represented as a string of length 3.
Return true if we can build the pyramid all the way to the top, otherwise false.
2 coupled DFS
- Ternary Expression Parser
Given a string representing arbitrarily nested ternary expressions, calculate the result of the expression. You can always assume that the given expression is valid and only consists of digits 0-9, ?, :, T and F (T and F represent True and False respectively).
- Stack
- 有时候有一大部分是不需要判断的,用character array
- 用计数器来,有就加,没有就减
- Keys and Rooms
There are N rooms and you start in room 0. Each room has a distinct number in 0, 1, 2, …, N-1, and each room may have some keys to access the next room.
Formally, each room i has a list of keys rooms[i], and each key rooms[i][j] is an integer in [0, 1, …, N-1] where N = rooms.length. A key rooms[i][j] = v opens the room with number v.
Initially, all the rooms start locked (except for room 0).
You can walk back and forth between rooms freely.
Return true if and only if you can enter every room.
just dfs
- 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 components in an undirected graph.
just dfs
- The Maze
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling up, down, left or right, but it won’t stop rolling until hitting a wall. When the ball stops, it could choose the next direction.
Given the ball’s start position, the destination and the maze, determine whether the ball could stop at the destination.
The maze is represented by a binary 2D array. 1 means the wall and 0 means the empty space. You may assume that the borders of the maze are all walls. The start and destination coordinates are represented by row and column indexes.
还是dfs,只是说,怎么解决不是一次走一格,而是一次走一列或者一行
- 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 surrounded by water.
Count the number of distinct islands. An island is considered to be the same as another if and only if one island can be translated (and not rotated or reflected) to equal the other.
0,1 islands 这种题的做法,切记通过把1变成0,来表示已经visited了,用visited[][]这种做法,容易出bug,因为你要有两个条件,一是grid[][] !=0 or visited[][]
还有一个要注意的点,每层结束之后,加一个b,来表示每层的结束
- Matchsticks to Square
Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match girl has, please find out a way you can make one square by using up all those matchsticks. You should not break any stick, but you can link them up, and each matchstick must be used exactly one time.
Your input will be several matchsticks the girl has, represented with their stick length. Your output will either be true or false, to represent whether you could make one square using all the matchsticks the little match girl has.
这题还是挺有意思的,首先,如果你想组成几个框,可以用一个有限的array来组成,分别向里面填,其实这个思路还是挺straightforward的
- Flatten a Multilevel Doubly Linked List
You are given a doubly linked list which in addition to the next and previous pointers, it could have a child pointer, which may or may not point to a separate doubly linked list. These child lists may have one or more children of their own, and so on, to produce a multilevel data structure, as shown in the example below.
Flatten the list so that all the nodes appear in a single-level, doubly linked list. You are given the head of the first level of the list.
其实更像一个linked list的题,有corner case要注意,比如当最后一个有children的是最后一个时,这时,你要注意你预留的next其实是null,这时就没有null.pre这个操作了
- Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array.
- sort and binary search(注意两个情况都不满足时)
2. 用公式求和然后再减去
- Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
用dummy node,用另外一位单独来记录余数
我的写法有冗余并不好,可以结合一起写
while(l1 != null || l2 != null || rem != 0)
l1 == null ? 0 : l1.val
- Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.
Linkedlist,你要删除一个点,一定是记录他前面一个值
- Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/description/
- Reverse Linked List
public ListNode reverseList(ListNode head) { ListNode pre = null; while (head != null) { ListNode temp = head.next; head.next = pre; pre = head; head = temp; }
return pre; }
//recursion
public ListNode reverseList(ListNode head) { /* recursive solution */ return reverseListInt(head, null); }
private ListNode reverseListInt(ListNode head, ListNode newHead) { if (head == null) return newHead; ListNode next = head.next; head.next = newHead; return reverseListInt(next, head); }
- Reverse Linked List II
Reverse a linked list from position m to n. Do it in one-pass.
Note: 1 ≤ m ≤ n ≤ length of list.
这题有意思,要再熟悉熟悉,先把点记录下来
- Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
You should preserve the original relative order of the nodes in each of the two partitions.
开两个list,一个保持小于的,一个保持大于的,分开进行操作,
注意尾巴要给null, 变化来的才要操作
- Sort List
Sort a linked list in O(n log n) time using constant space complexity.
三大O(nlogn)时间复杂度:
merge sort, quick sort, heap sort
list - > merge sort
array -> quick sort
分治思想,注意有一步操作是mid.next == null
- Middle of the Linked List
fast slow pointer
private ListNode findMid(ListNode head) {
if (head == null) {
return null;
}
ListNode slow = head; ListNode fast = head.next; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } return slow; }
- Reorder List
Given a singly linked list L: L0→L1→…→Ln-1→Ln,
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
You may not modify the values in the list’s nodes, only nodes itself may be changed.
对基本功的考察
- Linked List Cycle
Given a linked list, determine if it has a cycle in it.
快慢指针 遇到相等,就返回true
- Linked List Cycle II
快慢指针,遇到,将快指针挪到开头,直到碰到
- Rotate List
Given a linked list, rotate the list to the right by k places, where k is non-negative.
先得到len,然后对k进行取模,得到真正要的长度
用两个指针,确定要挪动的开头位置和结尾位置
有一个容易出bug的地方,先把尾巴挪过来,在确定头部,否则万一直接是null就尴尬了
- Merge k Sorted Lists
三种方法;pq,merge方法
注意corner case, 如果list 中node为null时怎么办
- Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Return a deep copy of the list.
method 1 hashmap
method 2 加到list里,分三步走,将复制的点插入list,复制random pointer, split出来
- Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
不太好总结,孰能生巧把
有点level-order traversal的意思