当前位置: 首页 > article >正文

面试冲刺 - 算法题 1

59.螺旋矩阵II

对于循环的圈数 loop = (n / 2):

意思是:如果转一圈,左右都会少一条边,整个正方形的宽度减2,一共宽度是n,每次减2,就是循环n/2次。

class Solution {
    public int[][] generateMatrix(int n) {
        int loop = 0;  // 控制循环次数
        int[][] res = new int[n][n];
        int start = 0;  // 每次循环的开始点(start, start)
        int count = 1;  // 定义填充数字
        int i, j;

        while (loop++ < n / 2) { // 判断边界后,loop从1开始
            // 模拟上侧从左到右
            for (j = start; j < n - loop; j++) {
                res[start][j] = count++;
            }

            // 模拟右侧从上到下
            for (i = start; i < n - loop; i++) {
                res[i][j] = count++;
            }

            // 模拟下侧从右到左
            for (; j >= loop; j--) {
                res[i][j] = count++;
            }

            // 模拟左侧从下到上
            for (; i >= loop; i--) {
                res[i][j] = count++;
            }
            start++;
        }

        if (n % 2 == 1) {
            res[start][start] = count;
        }

        return res;
    }
}

206.反转链表

需要注意的是递归法:

在使用递归法的时候,每次递归传入的是下一次循环的当前结点和前一结点:

class Solution {
    public ListNode reverseList(ListNode head) {
        //ListNode dum = new ListNode(-1,head);//设置虚拟头节点  
        return reverse(head,null);
    }
    public ListNode reverse(ListNode cur,ListNode pre){
        if(cur == null){
            return pre;
        }
        ListNode temp = cur.next;
        cur.next = pre;
        return reverse(temp,cur);
    }
}

142.环形链表II

需要注意别陷入死循环,当快慢指针相遇的时候是一定有环的。

public class Solution {
    public ListNode detectCycle(ListNode head) {
        //定义一个快慢指针
        ListNode slow = head;
        ListNode fast = head;
        while(fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
            if(slow == fast){//有环
                slow = head;
                while(slow != fast){
                    fast = fast.next;
                    slow = slow.next;
                }
                return slow;
            }
        }
        return null;

    }
}


199.二叉树的右视图

注意层序遍历的有两层循环

class Solution {
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> res = new LinkedList();
        if(root == null){
            return res;
        }
        Queue<TreeNode> queue = new LinkedList();
        queue.add(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            while(size-- > 0){
                TreeNode cur =  queue.peek();
                queue.poll();
                if(size == 0){
                    res.add(cur.val);
                }
                if(cur.left != null){
                    queue.add(cur.left);
                }
                if(cur.right != null){
                    queue.add(cur.right);
                }
            }
        }
        return res;
    }
}

http://www.kler.cn/news/160356.html

相关文章:

  • 大数据生态架构:探索未来科技的无限可能。
  • Word文件设置了只读模式,为什么还能编辑?
  • 开发重要网站
  • 同旺科技 USB TO RS-485 定制款适配器--- 拆解(四)
  • 要求CHATGPT高质量回答的艺术:提示工程技术的完整指南—第 4 章:控制温度和 Top-p 采样
  • k8s 安装 Longhorn
  • 【数据结构】动态规划(Dynamic Programming)
  • qt 5.15.2 网络文件下载功能
  • Pair<T, U>
  • Ubuntu22.04 安装nvida-docker2和改路径
  • 分布式数据库HBase
  • 使用Go快速开发TCP公共服务
  • 深信服技术认证“SCSA-S”划重点:XSS漏洞
  • APP测试的测试内容有哪些,常见的Bug分类介绍!
  • 网络和Linux网络_11(数据链路层)以太网(MAC帧)协议+局域网转发+ARP协议
  • jvs智能bi新增:数据集添加sql自定义节点、添加websocket任务进度动态展示等等
  • springboot引入swagger2
  • 10-tornado项目部署
  • 18、XSS——cookie安全
  • RPG项目01_脚本代码
  • Apache Ofbiz XML-RPC RCE漏洞复现(CVE-2023-49070)
  • 识别低效io引起的free buffer waits
  • 日志框架梳理(Log4j,Reload4j,JUL,JCL,SLF4J,Logback,Log4j2)
  • wsl2 ubuntu下配置go执行make init 错误 /bin/bash line 1 go command not found
  • DevOps搭建(三)-Docker环境安装细步骤
  • 多个项目复用node_modules
  • mac电池最大充电限制工具 AlDente Pro中文 for Mac
  • 深入理解Sentinel系列-1.初识Sentinel
  • 【WPF】扫描的方式发现局域网中的Android设备
  • 利用阿里云 DDoS、WAF、CDN 和云防火墙为在线业务赋能