[知识总结] 【CodeTop x LeetCode】Page 2 : 21-40面试高频算法题题解
作者:CC下载站 日期:2021-10-29 09:01:00 浏览:86 分类:编程开发
更多答案也可以关注我的GitHub算法仓库——Algorithm
(大家可以根据我每道题打的星来衡量这道题的难易,星越少越难)
所有代码都是在力扣上提交通过的,保证代码的正确无误
基础数据结构:
class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
92.反转链表II
思路是记录反转部分的前一个节点和后一个节点,将其反转后再连接,思路是这个,但是我在实现上可能麻烦一些:
class Solution {
public ListNode reverseBetween(ListNode head, int left, int right) {
ListNode curr = head;
ListNode prev = null;
ListNode next = curr;
int flag = 1;
while (flag < right) {
if (flag == left - 1) {
prev = curr;
}
curr = curr.next;
flag++;
}
next = curr.next;
if (prev == null) {
head = reverse(head, left, right);
prev = head;
}else {
prev.next = reverse(prev.next, left, right);
}
while ( prev.next != null && left <= right) {
prev = prev.next;
}
if (next != null) {
if (prev.next != null) {
prev.next.next = next;
}else {
prev.next = next;
}
}
return head;
}
public ListNode reverse(ListNode head, int left, int right) {
if (left == right) {
return head;
}
ListNode prev = null;
ListNode curr = head;
while (left <= right) {
ListNode nextTemp = curr.next;
curr.next = prev;
prev = curr;
curr = nextTemp;
left++;
}
return prev;
}
}
200.岛屿数量
广搜、深搜都可以,这里是深搜+剪枝,不用回溯:
class Solution {
public int numIslands(char[][] grid) {
int res = 0;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == '1') {
res++;
dfs(grid, i, j);
}
}
}
return res;
}
public void dfs(char[][] grid, int x, int y) {
grid[x][y] = '0';
if (x < grid.length - 1 && grid[x+1][y] == '1') {
dfs(grid, x+1, y);
}
if (x > 0 && grid[x-1][y] == '1') {
dfs(grid, x-1, y);
}
if (y < grid[0].length - 1 && grid[x][y+1] == '1') {
dfs(grid, x, y+1);
}
if (y > 0 && grid[x][y-1] == '1') {
dfs(grid, x, y-1);
}
}
}
33.搜索旋转排序数组
二分(我当时是懒省事直接O(n)遍历了)
class Solution {
public int search(int[] nums, int target) {
int n = nums.length;
if (n == 0) {
return -1;
}
if (n == 1) {
return nums[0] == target ? 0 : -1;
}
int l = 0, r = n - 1;
while (l <= r) {
int mid = (l + r) / 2;
if (nums[mid] == target) {
return mid;
}
if (nums[0] <= nums[mid]) {
if (nums[0] <= target && target < nums[mid]) {
r = mid - 1;
} else {
l = mid + 1;
}
} else {
if (nums[mid] < target && target <= nums[n - 1]) {
l = mid + 1;
} else {
r = mid - 1;
}
}
}
return -1;
}
}
46.全排列
经典算法,使用dfs+交换+回溯,可以当模板套用:
class Solution {
private List<List<Integer>> list = new ArrayList<>();
public List<List<Integer>> permute(int[] nums) {
dfs(nums, 0, nums.length-1);
return list;
}
public void dfs(int[] array,int start,int end) {
if(start==end) {
List<Integer> li = new ArrayList<>();
for (int i = 0; i < array.length; i++) {
li.add(array[i]);
}
list.add(li);
} else {
for (int i = start; i <= end; i++) {
swap(array,start,i);
dfs(array,start+1,end);
swap(array,start,i);
}
}
}
public void swap(int[] array,int i,int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
142.环形链表 II
当快慢指针相遇之后,使用第三个指针从头开始和慢指针一起向后移动(步伐都是一步),当第三个指针与慢指针相遇时,即为环的入口:
public ListNode detectCycle(ListNode head) {
ListNode fast = head;
ListNode slow = head;
ListNode res = head;
while (fast != null && slow != null) {
fast = fast.next;
if (fast == null) {
break;
}
fast = fast.next;
slow = slow.next;
if (slow == fast) {
while (slow != res) {
slow = slow.next;
res = res.next;
}
return res;
}
}
return null;
}
23.合并K个排序链表
结合前面那道合并两个链表,我们只需循环调用该方法即可:
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if (lists.length == 0) {
return null;
}
if (lists.length == 1) {
return lists[0];
}
ListNode res = lists[0];
for (int i = 0; i < lists.length-1; i++) {
res = mergeTwoLists(res, lists[i+1]);
}
return res;
}
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) {
return l2;
}
if (l2 == null) {
return l1;
}
if (l1.val < l2.val) {
l1.next = mergeTwoLists(l1.next, l2);
return l1;
}else {
l2.next = mergeTwoLists(l2.next, l1);
return l2;
}
}
}
54.螺旋矩阵
模拟打印顺序:
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> list = new ArrayList<>();
int left = 0 ,up = 0;
int right = matrix[0].length - 1;
int down = matrix.length - 1;
int size = matrix.length * matrix[0].length;
while (list.size() < size) {
for (int i = left; i <= right; i++) {
if (list.size() < size) list.add(matrix[up][i]);
}
up++;
for (int i = up; i <= down; i++) {
if (list.size() < size) list.add(matrix[i][right]);
}
right--;
for (int i = right; i >= left; i--) {
if (list.size() < size) list.add(matrix[down][i]);
}
down--;
for (int i = down; i >= up; i--) {
if (list.size() < size) list.add(matrix[i][left]);
}
left++;
}
return list;
}
704.二分查找
如题,就是考二分
public int search(int[] nums, int target) {
int l = 0, r = nums.length-1, m;
while (l <= r) {
m = (r + l)/2;
if (target == nums[m]) {
return m;
}
if (target > nums[m]) {
l = m + 1;
}else {
r = m - 1;
}
}
return -1;
}
232.用栈实现队列
两个栈实现,需要弹出时,将栈s1全部倒入s2,再从s2弹
class MyQueue {
private Stack<Integer> s1 = new Stack<>();
private Stack<Integer> s2 = new Stack<>();
private int front;
public MyQueue() {}
public void push(int x) {
if (s1.empty()) {
front = x;
}
s1.push(x);
}
public int pop() {
if (s2.empty()) {
while(!s1.empty()) {
s2.push(s1.pop());
}
}
return s2.pop();
}
public int peek() {
if (s2.empty()) {
return front;
}else {
return s2.peek();
}
}
public boolean empty() {
return s1.empty() && s2.empty();
}
}
42.接雨水
动规:从左向右,记录最大高度;再从右向左记录最大高度;最后每格的积水为两个数组中较小的那个再减去这个位置的高度:
public int trap(int[] height) {
int res = 0;
int len = height.length;
int[] left = new int[len];
int[] right = new int[len];
left[0] = height[0];
for (int i = 1; i < len; i++) {
left[i] = Math.max(left[i-1], height[i]);
}
right[len-1] = height[len-1];
for (int i = len - 2; i >= 0; i--) {
right[i] = Math.max(height[i], right[i+1]);
}
for (int i = 0; i < len; i++) {
res += Math.min(left[i], right[i]) - height[i];
}
return res;
}
94.二叉树的中序遍历
递归
class Solution {
List<Integer> list = new ArrayList<>();
public List<Integer> inorderTraversal(TreeNode root) {
dfs(root);
return list;
}
public void dfs (TreeNode root) {
if (root != null) {
dfs(root.left);
list.add(root.val);
dfs(root.right);
}
}
}
300.最长上升子序列
经典动规:(现在看看差不多又忘记怎么做了)
public int lengthOfLIS(int[] nums) {
if (nums.length == 0) {
return 0;
}
int[] dp = new int[nums.length];
dp[0] = 1;
int res = 1;
for (int i = 1; i < nums.length; i++) {
dp[i] = 1;
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
dp[i] = Math.max(dp[i], dp[j] + 1);
}
}
res = Math.max(res, dp[i]);
}
return res;
}
143.重排链表
分三步:找中点——>翻转链表——>合并链表
class Solution {
public void reorderList(ListNode head) {
if (head == null) {
return;
}
ListNode mid = middleList(head);
ListNode l2 = reverseList(mid);
mergeList(head, l2);
}
public ListNode middleList(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while (fast.next != null && fast.next.next != null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
public ListNode reverseList(ListNode head) {
ListNode prev = null, next;
while (head != null) {
next = head.next;
head.next = prev;
prev = head;
head = next;
}
return prev;
}
public void mergeList(ListNode l1, ListNode l2) {
ListNode l1_t, l2_t;
while (l1 != null && l2 != null) {
l1_t = l1.next;
l1.next = l2;
l1 = l1_t;
l2_t = l2.next;
l2.next = l1;
l2 = l2_t;
}
}
}
199.二叉树的右视图
向右深搜,技巧点:树的深度==集合大小,所以记录每层的深度,当depth == res.size()
时为第一当这一层的最右节点:
class Solution {
List<Integer> res = new ArrayList<>();
public List<Integer> rightSideView(TreeNode root) {
dfs(root, 0);
return res;
}
public void dfs (TreeNode root, int depth) {
if (root != null) {
if (depth == res.size()) {
res.add(root.val);
}
dfs(root.right, depth+1);
dfs(root.left, depth+1);
}
}
}
70.爬楼梯
就是斐波那契数列,滚动数组解决:
public int climbStairs(int n) {
if (n == 1) {
return 1;
}
int[] dp = new int[3];
dp[0] = 1;
dp[1] = 1;
for (int i = 2; i <= n; i++) {
dp[2] = dp[0] + dp[1];
dp[0] = dp[1];
dp[1] = dp[2];
}
return dp[2];
}
22.链表中倒数第k个节点
两个指针,一个从起点,另一个从正数第k个移动:
public ListNode getKthFromEnd(ListNode head, int k) {
ListNode fast = head;
ListNode slow = head;
while (k-- > 0) {
fast = fast.next;
}
while (fast != null) {
fast = fast.next;
slow = slow.next;
}
return slow;
}
124.二叉树中的最大路径和
思路是递归,递归去找左右最大的路径和,个人觉得比较难,需重复做:
class Solution {
int res = Integer.MIN_VALUE;
public int maxPathSum(TreeNode root) {
dfs(root);
return res;
}
public int dfs(TreeNode node) {
if (node == null) {
return 0;
}
int left = Math.max(dfs(node.left), 0);
int right = Math.max(dfs(node.right), 0);
res = Math.max(res, node.val + left + right);
return node.val + Math.max(left, right);
}
}
69.x 的平方根
二分:
public int mySqrt(int x) {
int res = -1, l = 0, r = x,m;
while (l <= r) {
m = l + (r - l)/2;
if ((long)m*m <= x) {
l = m + 1;
res = m;
}else {
r = m - 1;
}
}
return res;
}
56.合并区间
根据左端点排列后开始有序合并:
public int[][] merge(int[][] intervals) {
List<int[]> res = new ArrayList<>();
if (intervals == null || intervals.length == 0) {
return res.toArray(new int[0][]);
}
if (intervals.length == 1) {
return intervals;
}
Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
for (int i = 0; i < intervals.length-1;i++) {
int left = intervals[i][0];
int right = intervals[i][1];
while (right >= intervals[i+1][1] || right >= intervals[i+1][0]) {
if (right < intervals[i+1][1]) {
right = intervals[i+1][1];
}
i++;
if (i == intervals.length - 1) {
break;
}
}
int[] temp = new int[]{left, right};
res.add(temp);
}
if (res.get(res.size() - 1)[1] < intervals[intervals.length -1][1]) {
res.add(intervals[intervals.length -1]);
}
return res.toArray(new int[0][]);
}
19.删除链表的倒数第 N 个结点
我的写法可能麻烦些,主要是一些边界值的判断,思路和前面寻找倒数第N个节点思路一样,只不过是找N+1个,再做删除操作即可:
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode prev = findprev(head, n+1);
if (prev == null) {
if (head.next != null) {
return head.next;
}
return null;
}
if (prev.next != null) {
prev.next = prev.next.next;
}
return head;
}
public ListNode findprev (ListNode head, int k) {
ListNode fast = head;
ListNode slow = head;
while (k-- > 0) {
if (fast == null) {
return null;
}
fast = fast.next;
}
while (fast != null) {
fast = fast.next;
slow = slow.next;
}
return slow;
}
}
猜你还喜欢
- 03-29 [编程相关] Winform窗体圆角以及描边完美解决方案
- 03-29 [前端问题] has been blocked by CORS policy跨域问题解决
- 03-29 [编程相关] GitHub Actions 入门教程
- 03-29 [编程探讨] CSS Grid 网格布局教程
- 10-12 [编程相关] python实现文件夹所有文件编码从GBK转为UTF8
- 10-11 [编程算法] opencv之霍夫变换:圆
- 10-11 [编程算法] OpenCV Camshift算法+目标跟踪源码
- 10-11 [Python] python 创建 Telnet 客户端
- 10-11 [编程相关] Python 基于 Yolov8 + CPU 实现物体检测
- 03-15 [脚本工具] 使用go语言开发自动化脚本 - 一键定场、抢购、预约、捡漏
- 01-08 [编程技术] 秒杀面试官系列 - Redis zset底层是怎么实现的
- 01-05 [编程技术] 《Redis设计与实现》pdf
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[资料] 24秋初中改版教材全集(全版本)[PDF]
[电影] 高分国剧《康熙王朝》(2001)4K 2160P 国语中字 全46集 78.2G
[动画] 迪士尼系列动画139部 国英双语音轨 【蓝光珍藏版440GB】
[电影] 莫妮卡贝鲁奇为艺术献身电影大合集 1080P超清 双语字幕
[电影] DC电影宇宙系列合集18部 4K 高码率 内嵌中英字幕 273G
[音乐] 【坤曲/4坤时】鸡你太美全网最全,385首小黑子战歌,黄昏见证虔诚的信徒,巅峰诞生虚伪的拥护!
[音乐] 用餐背景音乐大合集 [MP3/flac]
[书籍] 彭子益医书合集 [PDF/DOC]
[电影] 《环太平洋两部合集》 4K REMUX原盘 [杜比视界] 国英双语音轨 [内封特效字幕] [133.8G]
[电影] 异人之下 The Traveller 2024✨【影版】【4K正式版/HQ超高码/DDP5.1】✚【1080高码】无水印/无压缩
[书籍] 彭子益医书合集 [PDF/DOC]
[游戏] 《黑神话悟空》免安装学习版【全dlc整合完整版】+Steam游戏解锁+游戏修改工具!
[动画] 《名侦探柯南》名侦探柯南百万美元的五菱星 [TC] [MP4]
[电视剧集] [BT下载][黑暗城市- 清扫魔 Dark City: The Cleaner 第一季][全06集][英语无字][MKV][720P/1080P][WEB-RAW]
[涨点姿势] 男性性技宝典:14招实战驭女术——爱抚、按摩、催情、姿势、高潮全攻略
[动画] 2002《火影忍者》720集全【4K典藏版】+11部剧场版+OVA+漫画 内嵌简日字幕
[剧集] 《斯巴达克斯》1-4季合集 无删减版 1080P 内嵌简英特效字幕
[CG剧情] 《黑神话:悟空》158分钟CG完整剧情合集 4K120帧最高画质
[短剧] 被下架·禁播的羞羞短剧·午夜短剧合集
[游戏] 黑神话悟空离线完整版+修改器
[影视] 美国内战 4K蓝光原盘下载+高清MKV版/内战/帝国浩劫:美国内战(台)/美帝崩裂(港) 2024 Civil War 63.86G
[影视] 一命 3D 蓝光高清MKV版/切腹 / 切腹:武士之死 / Hara-Kiri: Death of a Samurai / Ichimei 2011 一命 13.6G
[影视] 爱情我你他 蓝光原盘下载+高清MKV版/你、我、他她他 2005 Me and You and Everyone We Know 23.2G
[影视] 穿越美国 蓝光原盘下载+高清MKV版/窈窕老爸 / 寻找他妈…的故事 2005 Transamerica 20.8G
[电影] 《黄飞鸿》全系列合集
[Android] 开罗游戏 ▎像素风格的模拟经营的游戏厂商安卓游戏大合集
[游戏合集] 要战便战 v0.9.107 免安装绿色中文版
[书籍] 彭子益医书合集 [PDF/DOC]
[资源] 精整2023年知识星球付费文合集136篇【PDF格式】
[系统]【黑果小兵】macOS Big Sur 11.0.1 20B50 正式版 with Clover 5126 黑苹果系统镜像下载
- 最新评论
-
怎么没有后续闲仙麟 评论于:11-03 怎么没后续闲仙麟 评论于:11-03 有靳东!嘻嘻奥古斯都.凯撒 评论于:10-28 流星花园是F4处女作也是4人集体搭配的唯一一部!奥古斯都.凯撒 评论于:10-28 找了好久的资源,终于在这里找到了。感谢本站的资源和分享。谢谢AAAAA 评论于:10-26 找了好久的资源,终于在这里找到了。感谢本站的资源和分享。谢谢password63 评论于:10-26 找了好久的资源,终于在这里找齐了!!!!blog001 评论于:10-21 找了好久的资源,终于在这里找齐了!!!!blog001 评论于:10-21 找了好久的资源,终于在这里找到了。感谢本站的资源和分享。谢谢WillKwok 评论于:10-09 感谢分享1234123 评论于:10-07
- 热门tag