这是我参与11月更文挑战的第20天,活动详情查看:2021最后一次更文挑战
LeetCode习题集 有些题可能直接略过了,整理一下之前刷leetcode
141. 环形链表
给定一个链表,判断链表中是否有环。
为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。
示例 1:
输入:head = [3,2,0,-4], pos = 1
输出:true
解释:链表中有一个环,其尾部连接到第二个节点。
示例 2:
输入:head = [1,2], pos = 0
输出:true
解释:链表中有一个环,其尾部连接到第一个节点。
示例 3:
输入:head = [1], pos = -1 输出:false 解释:链表中没有环。
进阶:
你能用 O(1)(即,常量)内存解决此问题吗?
快慢指针
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
//快慢指针,当能指向同一个结点的时候,证明里面存在环
public class Solution {
public boolean hasCycle(ListNode head) {
if(head == null || head.next == null) return false;
ListNode fast = head.next;
ListNode slow = head;
while(fast != slow){
if(fast.next == null || fast.next.next == null) return false;
fast = fast.next.next;
slow = slow.next;
}
return true;
}
}
142. 环形链表 II
给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。
说明:不允许修改给定的链表。
示例 1:
输入:head = [3,2,0,-4], pos = 1
输出:tail connects to node index 1
解释:链表中有一个环,其尾部连接到第二个节点。
示例 2:
输入:head = [1,2], pos = 0
输出:tail connects to node index 0
解释:链表中有一个环,其尾部连接到第一个节点。
示例 3:
输入:head = [1], pos = -1 输出:no cycle 解释:链表中没有环。
进阶: 你是否可以不用额外空间解决此题?## 143. 重排链表
给定一个单链表 L:L0→L1→…→Ln-1→Ln , 将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→…
你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
示例 1:
给定链表 1->2->3->4, 重新排列为 1->4->2->3. 示例 2:
给定链表 1->2->3->4->5, 重新排列为 1->5->2->4->3.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public void reorderList(ListNode head) {
if (head == null) {
return;
}
ListNode fast = head, slow = head;
//慢指针指向了靠中间的结点
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
ListNode cur = slow.next, pre = null, next = null;
slow.next = null;
while (cur != null) {
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
ListNode p1 = head, p2 = pre;
while (p1 != null && p2 != null) {
next = p2.next;
p2.next = p1.next;
p1.next = p2;
p1 = p2.next;
p2 = next;
}
}
}
144. 二叉树的前序遍历
给定一个二叉树,返回它的 前序 遍历。
示例:
输入: [1,null,2,3]
1
\
2
/
3
输出: [1,2,3]
前序遍历:先输出根结点,在输出左结点,在输出右结点
使用栈,栈后进先出,从根结点向下添加,先把下面的输出,然后在一步一步向上走
先把根结点添加,
然后输出结点的值,结点压栈,找此结点的左子树,重复这个操作一直找到左子树为空的时候
然后从栈里弹出结点,此时弹出的结点为上面最后一个左子树为空的结点(栈后进先 出)
使用弹出结点的右结点循环上面的操作,
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
if (root == null) {
return res;
}
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
res.add(Integer.valueOf(node.val));
if (node.right != null) {
stack.push(node.right);
}
if (node.left != null) {
stack.push(node.left);
}
}
return res;
}
}
145. 二叉树的后序遍历
给定一个二叉树,返回它的 后序 遍历。
示例:
输入: [1,null,2,3]
1
\
2
/
3
输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗?
后序遍历:先输出左结点,在输出右结点,在输出根结点
因为后序遍历是最后输出根结点,我们按照左结点压栈,左结点可以到底,但是还要先访问右结点,才能输出根结点,
所以要找一个变量存一下右结点是否被访问(换句话说,存一下上次访问的结点),
如果右结点被访问了,此时可以输出根结点
使用栈,栈后进先出,从根结点向下添加,先把下面的输出,然后在一步一步向上走
先把根结点添加,
步骤:
结点压栈,找此结点的左子树,重复这个操作一直找到左子树为空的时候
然后从栈里得到栈顶的结点(栈顶未出栈),此时拿到的结点为上面最后一个左子树为空的结点(栈后进先出),
判断一下右结点是否为空或者 上次被访问的是否为右结点(被访问)
如果右结点为空或者被访问:左右结点都被访问了,可以输出根结点,把上次访问的结点改为当前根结点,当前根结点被访问了
如果右结点不为空并且没有被访问:那么把当前结点改为右结点,
使用弹出结点的右结点循环上面的操作,
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {//非递归写法
List<Integer> res = new ArrayList<Integer>();
if(root == null)
return res;
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode pre = null;
stack.push(root);
while(!stack.isEmpty()){
TreeNode curr = stack.peek();
if((curr.left == null && curr.right == null) ||
(pre != null && (pre == curr.left || pre == curr.right))){
//如果当前结点左右子节点为空或上一个访问的结点为当前结点的子节点时,当前结点出栈
res.add(curr.val);
pre = curr;
stack.pop();
}else{
if(curr.right != null) stack.push(curr.right); //先将右结点压栈
if(curr.left != null) stack.push(curr.left); //再将左结点入栈
}
}
return res;
}
}
146. LRU缓存机制
运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制。它应该支持以下操作: 获取数据 get 和 写入数据 put 。
获取数据 get(key) - 如果密钥 (key) 存在于缓存中,则获取密钥的值(总是正数),否则返回 -1。 写入数据 put(key, value) - 如果密钥不存在,则写入其数据值。当缓存容量达到上限时,它应该在写入新数据之前删除最近最少使用的数据值,从而为新的数据值留出空间。
进阶:
你是否可以在 O(1) 时间复杂度内完成这两种操作?
示例:
LRUCache cache = new LRUCache( 2 /* 缓存容量 */ );
cache.put(1, 1); cache.put(2, 2); cache.get(1); // 返回 1 cache.put(3, 3); // 该操作会使得密钥 2 作废 cache.get(2); // 返回 -1 (未找到) cache.put(4, 4); // 该操作会使得密钥 1 作废 cache.get(1); // 返回 -1 (未找到) cache.get(3); // 返回 3 cache.get(4); // 返回 4
Java好,Java妙,Java的ADC真奇妙(ADC=LinkedHashMap)
class LRUCache {
int capacity;
LinkedHashMap<Integer, Integer> cache;
public LRUCache(int capacity) {
this.capacity = capacity;
cache = new LinkedHashMap<Integer, Integer>(capacity, 0.75f, true) {
@Override
protected boolean removeEldestEntry(Map.Entry eldest) {
return cache.size() > capacity;
}
};
}
public int get(int key) {
return cache.getOrDefault(key, -1);
}
public void put(int key, int value) {
cache.put(key, value);
}
}
/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache obj = new LRUCache(capacity);
* int param_1 = obj.get(key);
* obj.put(key,value);
*/
147. 对链表进行插入排序
对链表进行插入排序。
插入排序的动画演示如上。从第一个元素开始,该链表可以被认为已经部分排序(用黑色表示)。
每次迭代时,从输入数据中移除一个元素(用红色表示),并原地将其插入到已排好序的链表中。
插入排序算法:
插入排序是迭代的,每次只移动一个元素,直到所有元素可以形成一个有序的输出列表。 每次迭代中,插入排序只从输入数据中移除一个待排序的元素,找到它在序列中适当的位置,并将其插入。 重复直到所有输入数据插入完为止。
示例 1:
输入: 4->2->1->3 输出: 1->2->3->4 示例 2:
输入: -1->5->3->4->0 输出: -1->0->3->4->5
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode insertionSortList(ListNode head) {
ListNode dummy = new ListNode(0), pre;
dummy.next = head;
while(head != null && head.next != null) {
//找到第一个值大于下一个结点的结点
if(head.val <= head.next.val) {
head = head.next;
continue;
}
pre = dummy;
//找到大于结点的下一个的值,也就是找到插入排序的目标位置
while (pre.next.val < head.next.val) pre = pre.next;
//交换两个值
ListNode curr = head.next;
head.next = curr.next;
curr.next = pre.next;
pre.next = curr;
}
return dummy.next;
}
}
148. 排序链表
在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序。
示例 1:
输入: 4->2->1->3 输出: 1->2->3->4 示例 2:
输入: -1->5->3->4->0 输出: -1->0->3->4->5
归并排序法:在动手之前一直觉得空间复杂度为常量不太可能,因为原来使用归并时,都是 O(N)的,
需要复制出相等的空间来进行赋值归并。对于链表,实际上是可以实现常数空间占用的(链表的归并
排序不需要额外的空间)。利用归并的思想,递归地将当前链表分为两段,然后merge,分两段的方
法是使用 fast-slow 法,用两个指针,一个每次走两步,一个走一步,知道快的走到了末尾,然后
慢的所在位置就是中间位置,这样就分成了两段。merge时,把两段头部节点值比较,用一个 p 指向
较小的,且记录第一个节点,然后 两段的头一步一步向后走,p也一直向后走,总是指向较小节点,
直至其中一个头为NULL,处理剩下的元素。最后返回记录的头即可。
主要考察3个知识点,
知识点1:归并排序的整体思想
知识点2:找到一个链表的中间节点的方法
知识点3:合并两个已排好序的链表为一个新的有序链表
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode sortList(ListNode head) {
return head == null ? null : mergeSort(head);
}
private ListNode mergeSort(ListNode head) {
if (head.next == null) {
return head;
}
ListNode p = head, q = head, pre = null;
while (q != null && q.next != null) {
pre = p;
p = p.next;
q = q.next.next;
}
pre.next = null;
ListNode l = mergeSort(head);
ListNode r = mergeSort(p);
return merge(l, r);
}
ListNode merge(ListNode l, ListNode r) {
ListNode dummyHead = new ListNode(0);
ListNode cur = dummyHead;
while (l != null && r != null) {
if (l.val <= r.val) {
cur.next = l;
cur = cur.next;
l = l.next;
} else {
cur.next = r;
cur = cur.next;
r = r.next;
}
}
if (l != null) {
cur.next = l;
}
if (r != null) {
cur.next = r;
}
return dummyHead.next;
}
}
149. 直线上最多的点数
给定一个二维平面,平面上有 n 个点,求最多有多少个点在同一条直线上。
示例 1:
输入: [[1,1],[2,2],[3,3]] 输出: 3 解释:
^
|
| o
| o
| o
+------------->
0 1 2 3 4
示例 2:
输入: [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]] 输出: 4 解释:
^
|
| o
| o o
| o
| o o
+------------------->
0 1 2 3 4 5 6
求在不在一条直线除了斜率还有什么???
求斜率.用两种方法
用最大约数方法(gcd), 把他化成最简形式, 3/6 == 2/4 == 1/2
除数(不太精确, 速度快!)
class Solution {
public int maxPoints(int[][] points) {
int n = points.length;
if (n == 0) return 0;
if (n == 1) return 1;
int res = 0;
for (int i = 0; i < n - 1; i++) {
Map<String, Integer> slope = new HashMap<>();
int repeat = 0;
int tmp_max = 0;
for (int j = i + 1; j < n; j++) {
//找到两个点的xy,来找到他们的斜率
int dy = points[i][1] - points[j][1];
int dx = points[i][0] - points[j][0];
if (dy == 0 && dx == 0) {
repeat++;
continue;
}
//找到最大公约数
int g = gcd(dy, dx);
if (g != 0) {
dy /= g;
dx /= g;
}
//保存斜率
String tmp = String.valueOf(dy) + "/" + String.valueOf(dx);
//放到map中保存,如果存在就数量+1,如果不存在就默认为
slope.put(tmp, slope.getOrDefault(tmp, 0) + 1);
tmp_max = Math.max(tmp_max, slope.get(tmp));
}
//保存当前中在一条直线上的最大的数量
res = Math.max(res, repeat + tmp_max + 1);
}
return res;
}
//找到最大公约
private int gcd(int dy, int dx) {
if (dx == 0) return dy;
else return gcd(dx, dy % dx);
}
}
150. 逆波兰表达式求值
根据逆波兰表示法,求表达式的值。
有效的运算符包括 +, -, *, / 。每个运算对象可以是整数,也可以是另一个逆波兰表达式。
说明:
整数除法只保留整数部分。 给定逆波兰表达式总是有效的。换句话说,表达式总会得出有效数值且不存在除数为 0 的情况。 示例 1:
输入: ["2", "1", "+", "3", "*"]
输出: 9
解释: ((2 + 1) * 3) = 9
示例 2:
输入: ["4", "13", "5", "/", "+"]
输出: 6
解释: (4 + (13 / 5)) = 6
示例 3:
输入: ["10", "6", "9", "3", "+", "-11", "", "/", "", "17", "+", "5", "+"] 输出: 22 解释:
((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22
PS: 既然是逆波兰表达式,那为何不逆波兰写??? (从后往前写)
加减乘除分贝分别做对应的操作
class Solution {
public int evalRPN(String[] tokens) {
index = tokens.length - 1;
return getPrefix(tokens);
}
int index;
public int getPrefix(String[] tokens) {
String token = tokens[index--];
if (token.equals("+")) {
int prefix1 = getPrefix(tokens);
int prefix0 = getPrefix(tokens);
return prefix0 + prefix1;
} else if (token.equals("-")) {
int prefix1 = getPrefix(tokens);
int prefix0 = getPrefix(tokens);
return prefix0 - prefix1;
} else if (token.equals("*")) {
int prefix1 = getPrefix(tokens);
int prefix0 = getPrefix(tokens);
return prefix0 * prefix1;
} else if (token.equals("/")) {
int prefix1 = getPrefix(tokens);
int prefix0 = getPrefix(tokens);
return prefix0 / prefix1;
} else {
return Integer.parseInt(token);
}
}
}