没有合适的资源?快使用搜索试试~ 我知道了~
LeetCode每日一题高频面试算法题目1
需积分: 0 1 下载量 201 浏览量
2022-08-03
17:23:53
上传
评论
收藏 4.3MB PDF 举报
温馨提示
试读
97页
LeetCode每日一题高频面试算法题目1
资源详情
资源评论
资源推荐
LeetCode每日一题高频面试算法题目
day01 (队列实现栈)
public static class QueueToStack{
private Queue<Integer> queue;
private Queue<Integer> help;
public QueueToStack(){
queue = new LinkedList<Integer>();
help = new LinkedList<Integer>();
}
public void push(int pushInt){
queue.add(pushInt);
}
/**
* @Description: 弹栈操作操作
* 弹栈时,queue队列所有数据迁移至 help 返回最后一个数 并交换指针
*/
public Integer pop(){
if (queue.isEmpty())
throw new RuntimeException("栈空!");
while (queue.size()>1){
help.add(queue.poll());
}
int temp = queue.poll();
swap();
return temp;
}
/**
* @Description: 栈的peek操作 只返回栈顶元素
* 原理同pop操作,但是在最后的一个元素要继续入队 help 因为peek只是返回栈顶 并非弹出
*/
public Integer peek(){
if (queue.isEmpty())
throw new RuntimeException("栈空");
while (queue.size()>1){
day02(反转一个单链表)
help.add(queue.poll());
}
int temp=queue.poll();
help.add(temp); //关键地方
swap();
return temp;
}
private void swap() {
Queue<Integer> temp = queue;
queue = help;
help = temp;
}
}
非递归
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
if(head == null || head.next==null)
return head;
ListNode curNode = head.next;
ListNode preNode = head;
preNode.next = null;
ListNode curNext;
while(curNode!=null){
curNext = curNode.next;
curNode.next = preNode;
preNode = curNode;
curNode = curNext;
}
return preNode;
}
}
day03(合并两个排序数组保证有序)
解析:
时间复杂度:O(n),假设 nn 是列表的长度,时间复杂度是 O(n)。
空间复杂度:O(1)。
//1->2->3->4->5:递归执行完向下走的时候,第一次的p指向5,head指向4,head.next是5,当执行
head.next.next=head时,p.next指向4,当执行head.next=null时,断开head的4到5的节点完成一
次反转,以此类推
public ListNode reverseList(ListNode head){
if(head==null || head.next==null){
return head;
}
ListNode p = reverseList(head.next);
head.next.next = head;
head.next = null;
return p;
}
时间复杂度:O(n),假设 nn 是列表的长度,那么时间复杂度为 O(n)。
空间复杂度:O(n),由于使用递归,将会使用隐式栈空间。递归深度可能会达到 n层。
day04(腐烂的橘子)
class Solution {
public void merge(int[] A, int m, int[] B, int n) {
int pa = 0;
int pb = 0;
int[] sorted = new int[m+n];
int curVal = 0;
while(pa<m || pb<n){
if(pa==m){ //注意判断其中一个是否到达尾部
curVal = B[pb++];
}else if(pb==n){
curVal = A[pa++];
}else if(A[pa]<B[pb]){
curVal = A[pa++];
}else{
curVal = B[pb++];
}
sorted[pa+pb-1] = curVal;
}
for(int i=0;i<m+n;i++){
A[i] = sorted[i];
}
}
}
1、先将所有腐烂橘子放入Queue(LinkedList)中,建立Map key=r*C+c value=此时此刻腐烂橘子所
经历的时间
2、当queue不为空 循环遍历,queue remove得到腐烂橘子队列中的位置,分析该腐烂橘子上下左右
使其腐烂,并把腐烂橘子(key=r*C+c, value=上层腐烂橘子对应时间+1)
3、遍历网格,如果有位置为1,说明有橘子未腐烂,return -1,否则返回map中的最大value
class Solution {
//对行和列进行移动,上,左,下,右
int[] dr = new int[]{-1,0,1,0};
int[] dc = new int[]{0,-1,0,1};
public int orangesRotting(int[][] grid) {
int R = grid.length;
int C = grid[0].length;
Queue<Integer> queue = new LinkedList();
Map<Integer,Integer> depth = new HashMap<>();
//先遍历寻找该开始就腐烂的橘子
for(int r=0;r<R;r++){
for(int c=0;c<C;c++){
if(grid[r][c]==2){
int code = r*C+c; //将表格中腐烂橘子的二维坐标转化为一个数字编码
queue.add(code);
depth.put(code,0); //key为二维坐标对应的数字编码,value为该编码对
应的橘子腐烂用时
}
}
}
剩余96页未读,继续阅读
BellWang
- 粉丝: 17
- 资源: 315
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0