楼主: creazy-lobster
跳转到指定楼层
上一主题 下一主题
收起左侧

刷题打卡贴

🔗
lalxyy 2019-3-16 22:29:14 | 只看该作者
全局:
leetcode 28

热身题,遍历一遍,截取子串对比就行


不需要写出KMP algorithm?直接brute force就可以了嘛

补充内容 (2019-3-16 22:29):
虽然很多语言的实现确实是brute force,比如Java
回复

使用道具 举报

🔗
 楼主| creazy-lobster 2019-3-20 13:42:40 | 只看该作者
全局:

这题拿出来一般不是考你KMP, 我记得某培训老师说过。但是遇到我也能写所以会总比不会强。
回复

使用道具 举报

🔗
 楼主| creazy-lobster 2019-3-20 13:48:25 | 只看该作者
全局:
第19天

17
String
Backtracking
Enumeration
FMAGU
高频
Given a string containing digits from 2-9 inclusive,
return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below.
Note that 1 does not map to any letters.
Input:"23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
backtracking枚举所有组合,首先需要建立一个数字到字母的映射。
这次要求的组合比之前的组合题多了一个嵌套,所以我们只需要在
拿到数字后在做一次枚举所有的字母。
如下图我们也可以看出由于只有3层用BFS的方法也可做
                       ’23’
                /                    \
              ’2’                 ’3’
           /  | \                       /  |     \
    ‘a’ 'b’‘c’   ‘d’ ‘e’ ‘f’
320
Backtracking
Enumeration
G低频
Write a function to generate the generalized abbreviations of a word.
Note: The order of the output does not matter.
Example:
Input: "word"
Output:
["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d",
"1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]
也是backtracking枚举所有组合,input的每一个字符两种选择:
可以忽略,也可以缩写,因为缩写的字符都是连续的,
所以我们用一个count变量来记录当前连续的缩写字符长度。
当前字符缩写的时候就把count拼接在字符中间并且把count重置为0,
忽略就count++。当到最后一位的时候把当前字符加入结果。
93
Backtracking
Enumeration
AMF低频
Given a string containing only digits, restore it by
returning all possible valid IP address combinations.
Example:
Input: "25525511135"
Output: ["255.255.11.135", "255.255.111.35"]
求一个字符串能组成合法ip地址的组合。遇到这个题一定先搞清楚
ip的格式,ip地址分4段,每段的数字从0-255。
也是backtracking解,我们每次可以选取1,2,3个数三种选择。
比如例子中有2 25 255三种截取方法,然后我们把剩下字符继续做
搜索。还有就是在选数字的时候第二个第三个数开头不能为0,
第三个数的大小不能大于255.
282
Backtracking
Enumeration
FGL中频
Given a string that contains only digits 0-9 and a target value,
return all possibilities to add binary operators (not unary) +, -, or *
between the digits so they evaluate to the target value.
Example 1:
Input: num = "123", target = 6
Output:["1+2+3", "1*2*3"]
给定一个数字字符串,要求在数字之间加上”+-*“符号进行运算,
使得结果等于target。有两点需要注意:
1. 数字可以取1个也可以多个,比如’1234’可以12*3, 123+4
  • 进行乘法运算有优先级:比如’1+2*3”结果是7而不是9

  • 起始为0的数字直接跳过

所以这题就需要枚举所有数字组合和所有运算符的组合,
还要保存当前运算结果,如果最后等于target加入结果数组。
这题思路看起来和其他枚举backtracking差不多,但是有很多陷阱。
时间复杂度O(n*3^n)。
还有一点就是需要维护一个prev变量来保存上一个值。
当做乘法运算时候就得把当前运算结果减去上一个运算结果,
在加上上一个运算结果乘以当前值,有点绕
例子:expr=’1+2’, cur = 3, prev = 2, 当前为4,需要做1+2*4
当前表达式expr为’1+2’,当前运算结果3,上一个运算结果2,
当我们要在当前值做乘法运算时候,需要cur-prev+ prev*n,
n为当前数字,比如当前n为4,就是3-2+2*4= 9,
140
Backtracking
Enumeration
GAMUPS中频
iven a non-empty string s and a dictionary wordDict containing a list of non-empty words,
add spaces in s to construct a sentence where each word is a valid dictionary word.
Return all such possible sentences.
Note:
  • The same word in the dictionary may be reused multiple times in the segmentation.
  • You may assume the dictionary does not contain duplicate words.
Input:
s = "catsanddog"
wordDict = ["cat", "cats", "and", "sand", "dog"]
Output:
[
  "cats and dog",
  "cat sand dog"
]
直接用dfs枚举所有字符串来找word会超时。
这次我们从字典入手,每次从字典里面取word,看是否match字符串开始
如果在,我们就继续检测剩下的字符串,直到终点我们把当前字符串加入结果
我们看例子:
第一个词cat,然后s.startswith(‘cat’)发现有匹配,我们就截取字符,
’sanddog‘继续递归查找。
第二个单词cats,也发现单词是s开头的字符串,截取剩下的
’anddog‘继续递归查找。
这题还需要用一个memory来存当前字符和字符串的映射,可以
减少重复运算。




回复

使用道具 举报

🔗
 楼主| creazy-lobster 2019-3-21 14:57:10 | 只看该作者
全局:
第20天,数组里面的排序相关类型题目,

88
Array
Sort
FM高频
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.
Note:
  • The number of elements initialized in nums1 and nums2 are m and n respectively.

  • You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.

Example:
Input:
nums1 = [1,2,3,0,0,0], m = 3
nums2 = [2,5,6],       n = 3

Output: [1,2,2,3,5,6]
合并排序,两个排序数组,把第二个数组数合并到第一个去,
炒鸡简单,就从两个数组最后往前比较,大的放到最后面去,
最后要处理下比完了之后第二个数组还有剩下的情况,
就把剩下的拿去覆盖掉第一个数组前面的数。
75
Array
Sort
FMU中频
Given an array with n objects colored red, white or blue, sort them in-place
so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Here, we will use the integers 0, 1, and 2 to represent the color red, white,
and blue respectively.
Note: You are not suppose to use the library's sort function for this problem.
Example:
Input: [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]
最简单就是计数排序,数一下分别多少个0,1,2,但要求one pass和constant space。
那就得在遍历得时候置换了,我们需要两个指针left, left,分别指向数组两端,
然后分三种情况
  • 当前值是2,交换当前值和left,然后left--

  • 当前值是0,交换当前值和left,然后left++,i++

  • 当前值是1,i++,left不动,所以left会指向最左边的1

283
Array
Sort
FG高频
Given an array nums, write a function to move all 0's to the end of it
while maintaining the relative order of the non-zero elements.
Example:
Input: [0,1,0,3,12]
Output: [1,3,12,0,0]
Note:
  • You must do this in-place without making a copy of the array.

  • Minimize the total number of operations.

要求原地把数组里的0移动到数组里非零值后面,
解法:遍历两遍数组,第一遍把所有非0数字全部往前移,如果有0直接覆盖,
比如[0, 1, 0, 3, 12]  填完后是→  [1, 3, 12, 3, 12]
然后第二遍遍历把数组后面的值设置成0
280
Array
Greedy
Sort
AG家
Given an unsorted array nums, reorder it in-place such that
nums[0] <= nums[1] >= nums[2] <= nums[3]....
从1开始,
奇数位:如果小于前1位,交换
偶数位:如果大于前1位,交换
奇数位大,偶数位小, 这样保证了每个顶点都大于左右
324
Array
Sort
Airbnb中频
Input: nums = [1, 5, 1, 1, 6, 4]
Output: One possible answer is [1, 4, 1, 5, 1, 6].
这题有重复数字了。最开始想到是排序,然后从两头取数字放。
时间复杂度O(nlogn), 空间复杂度O(n),但是题目followup,要求线性时间,O(1)空间。
那就肯定有什么trick了,这个思路不看答案面试时候真想不出来。
思路是找到中位数,把数分为大小两拨,奇数位把大拨的从大到小放,
偶数位把小拨的从小到大放。
根据公式(1 + 2 x index) % n, 我们把当前的index乘以2加1保证它为奇数,
然后取n余数让他不超出n的范围,这个公式计算出大拨里数index放的奇数位置。
如下
index => 运算后 Index
0 => (1 + 2 x 0) % 6 = 1 % 6 = 1
1 => (1 + 2 x 1) % 6 = 3 % 6 = 3
2 => (1 + 2 x 2) % 6 = 5 % 6 = 5
如果我们继续算小拨的index,会发现重复,我们需要放偶数位置
所以需要做下处理。
3 => (1 + 2 x 3) % 6 = 7 % 6 = 1
4 => (1 + 2 x 4) % 6 = 9 % 6 = 3
5 => (1 + 2 x 5) % 6 = 11 % 6 = 5
对于大于n/2的index,n|1会得到偶数index,|是逻辑或运算
if n = 6,二进制为 110 | 1 = 111 = 7
if n = 7,二进制为 111 | 1 = 111 = 7
然后我们用新公式(1 + 2*index) % (n | 1)就可以算出偶数index了
3 => (1 + 2 x 3) % 7 = 7 % 7 = 0
4 => (1 + 2 x 4) % 7 = 9 % 7 = 2
5 => (1 + 2 x 5) % 7 = 11 % 7 = 4


回复

使用道具 举报

🔗
 楼主| creazy-lobster 2019-3-22 14:51:44 | 只看该作者
全局:
第21天, 主要做了linkedlist的一些题,这个类型还是很多公司会考的题型,多练练画画图遇到很容易过, 估计明天后天会有2天都是这类型。

#
名称
Tag& 频率
题目
解题

206
LinkedList
多公司高频
Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
反转链表,可以用迭代和递归两种方法做,这类题需要设置一个prev变量,
用来记录前一个值,每次操作把当前next指向prev,然后继续跳到下一个操作。
141
LinkedList
多公司中频
Given a linked list, determine if it has a cycle in it.
To represent a cycle in the given linked list,
we use an integer pos which represents the position (0-indexed)
in the linked list where tail connects to. If pos is -1,
then there is no cycle in the linked list.

Example 1:
Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list,
where tail connects to the second node.
检测链表是否存在环,用快慢指针法,快指针每次走2步,慢指针每次走1步。
如果有环他们肯定会在某一点相遇,返回true。
若没有环程序自动结束返回false
24
LinkedList
多公司中频
Given a linked list, swap every two adjacent nodes and return its head.
You may not modify the values in the list's nodes, only nodes itself may be changed.
Example:
Given 1->2->3->4, you should return the list as 2->1->4->3.
要求将链表中的节点两两交换,由于链表结构变化,所以我们这里需要用
到一个链表里面常用到技巧,dummy node,就是创建一个node,
将它指向head,当我们做完操作后返回dummy指向到新head。
接下来就是交换nodes了,我们把2个nodes看作一个整体,
每次循环我们交换一对nodes,并把prev指向新的left node。
做完交换后我们还需要把指针移动到下一个pair,这里需要检测一下是否为空
328
LinkedList
多公司低频
Amazon
Given a singly linked list, group all odd nodes together followed by the even nodes.
Please note here we are talking about the node number and not the value in the nodes.
Example 2:
Input: 2->1->3->5->6->4->7->NULL
Output: 2->3->6->7->1->5->4->NULL
用了两个dummy node来分别作为奇数偶数位链表开头,
然后把计数偶数位分别放到两个链表上,最后连接2个链表
92
LinkedList
FAGM中低频
Reverse a linked list from position m to n. Do it in one-pass.
Note: 1 ≤ m ≤ n ≤ length of list.
Example:
Input: 1->2->3->4->5->NULL, m = 2, n = 4
Output: 1->4->3->2->5->NULL
这次反转链表要求反转m到n一段位置,解法是先算出offset=n-m,
然后用这个距离定义两个变量表示要求反转的头和尾。
现在我们只需要做像1里面的反转就好了,这题主要还是一些case
考虑的问题,很难做到15分钟bug free。
237
LinkedList
低频
Input: head = [4,5,1,9], node = 5
Output: [4,1,9]
Explanation: You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function.
居然有这种题,删除一个节点连接前后节点,done。
19
LinkedList
GMAU中低频
Given a linked list, remove the n-th node from the end of list and return its head.
Example:
Given linked list: 1->2->3->4->5, and n = 2.
After removing the second node from the end,
the linked list becomes 1->2->3->5.
这题要求one pass,和上面92很像,我们设置left,left两个指针,
初始化距离为n,然后一直移动直到left到达最后一个元素。
然后我们跳过left的下一个元素就好。
83
LinkedList
Salesforce中频
GM低频
Given a sorted linked list, delete all duplicates such that each element appear only once.
Example 1:
Input: 1->1->2
Output: 1->2
给一个排序的linkedlist, 移除重复数字。
很简单,当cur值等于cur.next的值就把cur指针指向cur.next.next,
不等于的之后移动cur = cur.next到下一个数字。
注意过程中要检测cur和cur.next不为null


回复

使用道具 举报

🔗
 楼主| creazy-lobster 2019-3-23 15:17:39 | 只看该作者
全局:

第22天
203Remove Linked List ElementsLinkedList
AGPY低频
Remove all elements from a linked list of integers that have value val.
Example:
Input: 1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5
移除链表里面给定的val,这题也需要dummy node,
因为可能第一个就是要移除对象。然后设置pre,cur一前一后,
每次移动cur如果cur.val == val就把pre的next指向下一个,else移动pre。
82Remove Duplicates from Sorted List IILinkedList
salesforce
Google低频
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
Example 1:
Input: 1->2->3->3->4->4->5
Output: 1->2->5
要求删除所有重复的数字,只留下没有重复的数字。
同样的套路,dummy node,当发现cur和下一个元素重复的话,
我们就一直寻找最后一位,然后把pre的next指向下一个元素。
369Plus One Linked ListLinkedList
Google,Amazon低频
Given a non-negative integer represented as non-empty
a singly linked list of digits, plus one to the integer.
You may assume the integer do not contain any leading zero,
except the number 0 itself.
The digits are stored such that the most significant digit is
at the head of the list.
Example :
Input: [1,2,3]
Output: [1,2,4]
这题如果不做限制用stack,要求O(1)space的用以下思路:
分2部:第1部用left,right两个变量做标记,right指向head,遍历到最后,
当right不为9的时候,就把left=right,
这样保证了left是需要进位的最前面那位,
比如1→9→9→9, 最后i指向1,j指向最后。
第二步因为right指向最后位,我们看right是否为9,
不为9直接加1不会进位。
是9的话我们就把i+1然后把i后面的数全部设为0就好。ex: 2→0→0→0
还有1种case需要注意就是9→9这种,这种情况需要返回dummy node
2Add Two NumbersLinkedList
Amazon
Google
Facebook
Microsoft
Apple
Yahoo
其他多公司低频
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.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
这题要求两个链表代表的数字加起来的和的一个新链表,表的数字的倒序的
和其他链表题一样我们需要个dummy node来创建新表,
首先我们需要一个carry变量来存进位,
然后遍历l1, l2,分别把他们的值和carry加入sum变量,
然后我们用除法取模的方法,比如sum=4+6=10, carry = sum//10
newNode.val = sum%10,这样就跟新了carry,创建了当前节点。
然后接着做下一个节点,注意l1, l2可以为空,carry可以为0,
所以当l1, l2其一不为空或carry为1时候都要创建新node。
445Add Two Numbers IILinkedList
AGFM低频
You are given two non-empty linked lists representing two
non-negative integers.
The most significant digit comes first 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.
Follow up:
What if you cannot modify the input lists?
In other words, reversing the lists is not allowed.
Example:
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7
上一题变形题,现在数字是正序的
用了2个stack可以从后往前拿数,其他的和上一题步骤差不多。
160Intersection of Two Linked ListsLinkedList
AGUFM低频
Write a program to find the node at which the intersection of
two singly linked lists begins.
https://assets.leetcode.com/uploads/2018/12/13/160_statement.png
要求两个有相交的linkedlist的交点,例子如下就是找到8。
4(a)→1\
8→4→5
5(b)→0→1/
思路很巧妙,循环2遍,用a,b指针分别指向两个链表head,
然后遍历,直到最后然后把指针指向另一个链表头部,
直到他们相遇。
比如例子a=4,b=5, 当a=null时候,a就指向5。
下一步当b=null当时候,b指向4,a指向0,
你会发现他们在移动2步就会相遇。
4(b)→1\
8→4→5
5→0(a)→1/
当两个链表没有交点的时候,两次循环后他们会同时停在null位置上
从而推出循环
21Merge Two Sorted ListsLinkedList
Amazon高频
多公司低频
Merge two sorted linked lists and return it as a new list.
The new list should be made by splicing together the nodes of
the first two lists.
Example:
Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
合并两个链表,比较基础,dummy node创建新链表,
然后当两个链表都不为空都时候往里面加小的,
最后当剩下一个链表当时候把他加到后面就好了。
[/table][table]
203Remove Linked List ElementsLinkedList
AGPY低频
Remove all elements from a linked list of integers that have value val.
Example:
Input: 1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5
移除链表里面给定的val,这题也需要dummy node,
因为可能第一个就是要移除对象。然后设置pre,cur一前一后,
每次移动cur如果cur.val == val就把pre的next指向下一个,else移动pre。
82Remove Duplicates from Sorted List IILinkedList
salesforce
Google低频
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
Example 1:
Input: 1->2->3->3->4->4->5
Output: 1->2->5
要求删除所有重复的数字,只留下没有重复的数字。
同样的套路,dummy node,当发现cur和下一个元素重复的话,
我们就一直寻找最后一位,然后把pre的next指向下一个元素。
369Plus One Linked ListLinkedList
Google,Amazon低频
Given a non-negative integer represented as non-empty
a singly linked list of digits, plus one to the integer.
You may assume the integer do not contain any leading zero,
except the number 0 itself.
The digits are stored such that the most significant digit is
at the head of the list.
Example :
Input: [1,2,3]
Output: [1,2,4]
这题如果不做限制用stack,要求O(1)space的用以下思路:
分2部:第1部用left,right两个变量做标记,right指向head,遍历到最后,
当right不为9的时候,就把left=right,
这样保证了left是需要进位的最前面那位,
比如1→9→9→9, 最后i指向1,j指向最后。
第二步因为right指向最后位,我们看right是否为9,
不为9直接加1不会进位。
是9的话我们就把i+1然后把i后面的数全部设为0就好。ex: 2→0→0→0
还有1种case需要注意就是9→9这种,这种情况需要返回dummy node
2Add Two NumbersLinkedList
Amazon
Google
Facebook
Microsoft
Apple
Yahoo
其他多公司低频
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.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
这题要求两个链表代表的数字加起来的和的一个新链表,表的数字的倒序的
和其他链表题一样我们需要个dummy node来创建新表,
首先我们需要一个carry变量来存进位,
然后遍历l1, l2,分别把他们的值和carry加入sum变量,
然后我们用除法取模的方法,比如sum=4+6=10, carry = sum//10
newNode.val = sum%10,这样就跟新了carry,创建了当前节点。
然后接着做下一个节点,注意l1, l2可以为空,carry可以为0,
所以当l1, l2其一不为空或carry为1时候都要创建新node。
445Add Two Numbers IILinkedList
AGFM低频
You are given two non-empty linked lists representing two
non-negative integers.
The most significant digit comes first 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.
Follow up:
What if you cannot modify the input lists?
In other words, reversing the lists is not allowed.
Example:
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7
上一题变形题,现在数字是正序的
用了2个stack可以从后往前拿数,其他的和上一题步骤差不多。
160Intersection of Two Linked ListsLinkedList
AGUFM低频
Write a program to find the node at which the intersection of
two singly linked lists begins.
https://assets.leetcode.com/uploads/2018/12/13/160_statement.png
要求两个有相交的linkedlist的交点,例子如下就是找到8。
4(a)→1\
8→4→5
5(b)→0→1/
思路很巧妙,循环2遍,用a,b指针分别指向两个链表head,
然后遍历,直到最后然后把指针指向另一个链表头部,
直到他们相遇。
比如例子a=4,b=5, 当a=null时候,a就指向5。
下一步当b=null当时候,b指向4,a指向0,
你会发现他们在移动2步就会相遇。
4(b)→1\
8→4→5
5→0(a)→1/
当两个链表没有交点的时候,两次循环后他们会同时停在null位置上
从而推出循环
21Merge Two Sorted ListsLinkedList
Amazon高频
多公司低频
Merge two sorted linked lists and return it as a new list.
The new list should be made by splicing together the nodes of
the first two lists.
Example:
Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
合并两个链表,比较基础,dummy node创建新链表,
然后当两个链表都不为空都时候往里面加小的,
最后当剩下一个链表当时候把他加到后面就好了。
回复

使用道具 举报

🔗
 楼主| creazy-lobster 2019-3-24 14:28:59 | 只看该作者
全局:
第23天,linkedlist最后一部分


234Palindrome Linked ListLinkedList
MAGF低频
Given a singly linked list, determine if it is a palindrome.
Example 1:
Input: 1->2                Input: 1->2->2->1
Output: false             Output: true
Follow up:
Could you do it in O(n) time and O(1) space?
分三步做:

1. 快慢指针找中点
2. 反转前半段
3. 如果之前fast指针不为空的话,说明链表长度为奇数,

slow需要向前移动一位,然后从头开始比较两边的值
143Reorder ListLinkedList
FB中频
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 tde values in tde list's nodes,
only nodes itself may be changed.
Example 1:
Given 1->2->3->4, reorder it to 1->4->2->3.
Example 2:
Given 1->2->3->4->5, reorder it to 1->5->2->4->3.
和上一题如出一辙,我们需要反转后半段,但是有trick,也是分三步

1. 快慢指针找中点,但是这题要多做个操作,
  用个变量pre指向慢指针前一位,找到中点后把pre.next设为null。
  防止后面我们建立新链表的时候前半段包含多余的值
2. 反转后半段
3. 建立dummy node,每次往里面分别加入前半段后半段的node,
  注意奇数位后半段会多一个node。
142Linked List Cycle IILinkedList
MLGA低频
Given a linked list, return tde node where tde cycle begins.
If tdere is no cycle, return null.
找链表环开始的node。和第一题一样,首先要判断是否有环,
快慢指针法,如果他们相遇了就有环。
这题其实是道数学题,
                                    D
                          -----------
              L        /               \
head -----------C                  |
                       \               /
                          ---------X
如上图 L为head到环起始位置距离, D为环起点c到相遇点x的距离,
假设环长度M,  我们知道当他们相遇的时候, fast走了2倍距离
slow = L + D
fast = 2slow = 2L + 2D
然后fast又等于fast = L + M + D
所以有 2L + 2D = L + M + D  =>  L + D = M => L = M - D
所以可以看出环长度M减去D也就是相遇点x到起点距离等于L
所以从head和X出发,当两点相遇就是环起始点
148Sort ListLinkedList
FMG低频
Sort a linked list in O(n log n) time using constant space complexity.
Example 1:
Input: 4->2->1->3
Output: 1->2->3->4
要求不用额外空间排序链表,这里我们用merge sort,用快慢指针把
链表分为两部分,然后递归divide,最后把2个部分合并排序起来。
25Reverse Nodes in k-GroupLinkedList
FAM中频
Given a linked list, reverse tde nodes of a linked list k
at a time and return its modified list.
k is a positive integer and is less tdan or equal to
tde lengtd of tde linked list.
If tde number of nodes is not a multiple of k tden left-out nodes
  in tde end should remain as it is.
Example:
Given tdis linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
Note:

- Only constant extra memory is allowed.
- You may not alter tde values in tde list's nodes,
  only nodes itself may be changed.
这题让我们对链表每个k长度进行反转,思路为一段一段对反转,
这里用了递归写的,首先检测一下当前链表长度是否大于k,
如果小于直接返回。大于k我们反转k位,然后把当前head
的next指向剩下的list
61Rotate ListLinkedList
超低频
Given a linked list, rotate tde list to tde right by k places,
where k is non-negative.
Example 1:
Input: 1->2->3->4->5->NULL, k = 2
Output: 4->5->1->2->3->NULL
Explanation:
rotate 1 steps to tde right: 5->1->2->3->4->NULL
rotate 2 steps to tde right: 4->5->1->2->3->NULL
这题要求把链表后面的数移动k次到前面,由于k可能会超出链表长度,
所以我们首先要量出链表长度l,然后用k%l取出实际需要移动的步数。
然后用快慢指针,把距离设为k,同时移动快慢指针直到fast在最后一位,
最后把快指针next指向head,慢指针next指向null就好了。
86Partition ListLinkedList
Amazon低频
Given a linked list and a value x, partition it such tdat all nodes
less tdan x come before nodes greater tdan or equal to x.
You should preserve tde original relative order of tde nodes
in each of tde two partitions.
Example:
Input: head = 1->4->3->2->5->2, x = 3
Output: 1->2->2->4->3->5
要我们把链表小于x的node放在大于x node的后面,要保持原有相对顺序。
很简单,我们根据x大小把他们拆成2个list,然后把他们连接起来。
记得清空2个list最后一个node的next,不然会造成死循环。
23Merge k Sorted ListsLinkedList
FAG高频
多公司
Merge k sorted linked lists and return it as one sorted list.
Analyze and describe its complexity.
Example:
Input:
[
  1->4->5,
  1->3->4,
  2->6
]
Output: 1->1->2->3->4->4->5->6
合并k个链表,主要考察堆的使用,我们初始化先把每个list的第一个node
放进堆里面,用value排序。然后每次取出最小的node加入新链表,
然后看如果当前node还有next,就把next放进堆里面。
147Insertion Sort ListLinkedList
低频
Sort a linked list using insertion sort.要求在链表上做插入排序,思路一样,把大于head的node插入前面。

回复

使用道具 举报

🔗
 楼主| creazy-lobster 2019-3-26 13:58:36 | 只看该作者
全局:
24天打卡,写了二分查找的一些题。 二分法思路简单,是常考的类型题目,一般数组排序或部分排序,或者要求比O(n)时间快的搜索问题都是二分。做这类题思路清晰简单但是有很多变体,查找条件,判断条件和左右边界的更新方式配合不好容易造成死循环或遗漏区域。



[td]
#名称Tag& 频率题目解题
278First Bad VersionBinarySearch
FB高频
Given n = 5, and version = 4 is the first bad version.
call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true
Then 4 is the first bad version.
二分查找,每次找中间点,如果返回false说明bad version
在右边,true说明在左边。不断缩小范围直到找到第一个。
35Search Insert PositionBinarySearch
GA低频
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.
Example 1:
Input: [1,3,5,6], 5
Output: 2
这题需要初始化right=n,因为可能插入最后一位后面的index。
33Search in Rotated Sorted ArrayBinarySearch
FB,Amazon高频
多公司
Suppose an array sorted in ascending order is rotated
at some pivot unknown to you beforehand.
Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4
一个在某个点调转过的排序数组,要求找到给定target的index
二分法具体实现以下三步:

1. 如果mid等于target,返回mid
2. 如果mid>=start, 我们就知道左边是排序的,然后我们看target是否在[start, mid)之间,在的话right=mid,
不再left = mid + 1
3. 如果第2步不成立证明右边是排序的,我们就检测target是否
在(mid, end]之间,在就走右边,不再去左边。

上面第2步,取=号是因为当搜索范围缩小到2个数的时候,
我的mid总会落到左边那个数,这样二分就会往右走。

还要处理一个conor case就是当input只有1个数的时候,
由于我取right = n-1,且循环条件为start<right,
所以只有一个数的时候不会走二分做检查,需要单独写。</right,

153Find Minimum in Rotated Sorted ArrayBinarySearch
FLAGMU低频
Suppose an array sorted in ascending order is rotated
at some pivot unknown to you beforehand.
Find the minimum element.
Input: [4,5,6,7,0,1,2]
Output: 0
和33题一样,但是找最小的简单些,
我们只需要拿mid和right比较,如果右边升序,
那最小值肯定在左边end=mid;
不然的话那最小值就在左边right=mid-1
最后left所在index就是最小值。
162Find Peak ElementBinarySearch
FMAG中频
A peak element is an element that is greater than its neighbors.
Input: nums = [1,2,1,3,5,6,4]
Output: 1 or 5
Explanation: Your function can return either index number 1 where
the peak element is 2, or index number 5 where the peak element is 6.
查找数组中的peak,可能有多个peak。
因为我们总是拿到中间两个数左边那个,
所以找到mid后我们将他和mid+1比,
如果mid<mid+1,把left=mid+1,不然right=mid
374Guess Number Higher or LowerBinarySearch
Google低频
We are playing the Guess Game. The game is as follows:
I pick a number from 1 to n. You have to guess which number I picked.
You call a pre-defined API guess(int num) which returns 3 possible results (-1, 1, or 0):
-1 : My number is lower
1 : My number is higher
0 : Congrats! You got it!
Example :
Input: n = 10, pick = 6
Output: 6
从1到n的二分,其他没什么说的有一点就是查找条件加上
等于,可以覆盖只有1个input的情况。
34Find First and last position of element in sorted arrayBinarySearch
FLAGU中频
Given an array of integers nums sorted in ascending order,
find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1].
Example 1:
Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]
Example 2:
Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]
二分法搜索范围的类型题,有多种解法,我们先来看第一种:
最直觉的做法二分找到target然后往两边阔,最坏情况O(n)。
这样显然不符合要求,我们来思考下来看第二种:
做2次二分搜索,一次确定左边界,一次确定右边界,
然后返回结果。二次二分法需要单独写,唯一区别就是
在比较mid和target的时候,当等于的时候不断push另一边:
左边界是当mid>=target,移动right=mid-1
右边界是当mid<=target,移动left=mid+1
第三种做法在面试的时候不容易想到,只需写一个二分法,
我们先找target的左边界,然后找target+1的左边界,
因为target下一个数左边界前一位就是target的右边界。
349Intersection of Two ArraysBinarySearch
FB, Amazon中频
lyft, uber多公司低频
Given two arrays, write a function to compute their intersection.
Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]
最直接暴力解法把两个数组放hashset,然后取出来看在
不再另一个set里,时间复杂度O(n).
第二种解法就是排序数组2然后每次从数组1取一个数
用二分搜索数组2,时间复杂度O(nlogn).
第三者解法排序2个数组然后用two-pointer,也是O(nlogn).
350Intersection of Two Arrays IIBinarySearch
FB中频
GAU低频
Given two arrays, write a function to compute their intersection.
Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]
follow up不光返回intersection,还要返回重复次数。
我们可以用一个hashmap,key是数字,value是出现次数。
先扫描数组1,把数字和出现次数记录一下,
然后扫描数组2, 如果当前数字在hashmap中且出现次数不为0
则把数加入结果数组,并把相应数字count--



回复

使用道具 举报

🔗
zjccpmh 2019-3-27 13:17:32 | 只看该作者
全局:
老铁 这么刻苦 给跪了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表