Plantre Plantre
首页
  • 算法

    • 查找
    • 排序
  • 力扣

    • 排序
技术
硬件
逆向
  • 前端文章

    • HTML
    • CSS
    • JavaScript
  • 技术

    • 技术文档
    • GitHub技巧
    • Nodejs
    • 博客搭建
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

plantre

一个后端开发者
首页
  • 算法

    • 查找
    • 排序
  • 力扣

    • 排序
技术
硬件
逆向
  • 前端文章

    • HTML
    • CSS
    • JavaScript
  • 技术

    • 技术文档
    • GitHub技巧
    • Nodejs
    • 博客搭建
  • 学习笔记

    • 《JavaScript教程》
    • 《JavaScript高级程序设计》
    • 《ES6 教程》
    • 《Vue》
    • 《React》
    • 《TypeScript 从零实现 axios》
    • 《Git》
    • TypeScript
    • JS设计模式总结
  • 学习
  • 面试
  • 心情杂货
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 计算机组成原理

  • 操作系统

  • 计算机网络

  • 设计模式

  • Java

  • Spring

  • SpringCloud

  • MySQL

  • Redis

  • 分布式

  • Zookeeper

  • Dubbo

  • Kafka

  • 数据结构

  • 算法

  • OJ

    • 自定义
    • 排序
    • 位运算
    • 二分查找
    • 递归
    • 双指针(逆向,快慢)
    • 滑动窗口
    • 辅助栈
    • 贪心
    • 回溯
    • 动态规划
    • 二叉树
    • DFS
    • 拼接&拆分
    • 模拟
      • 工业实现原理
      • 数学
      • 字符串
    • 从道家哲学看计算机?
    • 后端
    • OJ
    plantre
    2025-07-07
    目录

    模拟

    # 8. 字符串转换整数 (atoi) - 力扣(LeetCode) (opens new window)

    class Solution {
        public int myAtoi(String s) {
            if (s == null || s.isEmpty()) return 0;
            int index = 0, sign = 1;
            long num = 0;
            // 1. 跳过前导空格
            while (index < s.length() && s.charAt(index) == ' ') index++;
            if (index == s.length()) return 0;
            // 2. 处理符号
            if (s.charAt(index) == '+' || s.charAt(index) == '-') {
                sign = (s.charAt(index) == '-') ? -1 : 1;
                index++;
            }
            // 3. 转换数字
            while (index < s.length() && Character.isDigit(s.charAt(index))) {
                int digit = s.charAt(index) - '0';
                num = num * 10 + digit;
                // 4. 溢出检查
                if (num * sign > Integer.MAX_VALUE) return Integer.MAX_VALUE;
                if (num * sign < Integer.MIN_VALUE) return Integer.MIN_VALUE;
                index++;
            }
            return (int)num * sign;
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25

    # 54. 螺旋矩阵 - 力扣(LeetCode) (opens new window)

    class Solution {
        public List<Integer> spiralOrder(int[][] matrix) {
            if (matrix.length == 0)
                return new ArrayList<Integer>();
            int top = 0;
            int bottom = matrix.length - 1;
            int left = 0;
            int right = matrix[0].length - 1;
            int x = 0;
    
            // 结果数组大小计算保持不变
            Integer[] res = new Integer[(right + 1) * (bottom + 1)];
    
            while (true) {
              // 1. 从左向右遍历顶行
               for (int i = left; i <= right; i++) {
                    res[x++] = matrix[top][i];
               }  
               if (++top > bottom) break;
               // 2. 从上向下遍历右列
               for(int i = top; i <= bottom; i++) {
                    res[x++] = matrix[i][right];
               }
               if (--right < left) break;
               // 3. 从右向左遍历底行
               for (int i = right; i >= left; i--) {
                    res[x++] = matrix[bottom][i];
               }
               if (--bottom < top) break;
               // 4. 从下向上遍历左列
               for (int i = bottom; i >= top; i--) {
                    res[x++] = matrix[i][left];
               }
               if (++left > right) break;
            }
            return Arrays.asList(res);
        }
    }
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    编辑 (opens new window)
    上次更新: 2025/07/16, 10:21:39
    拼接&拆分
    工业实现原理

    ← 拼接&拆分 工业实现原理→

    最近更新
    01
    加油鸭
    07-30
    02
    要点总结
    07-28
    03
    字符串
    07-15
    更多文章>
    Theme by Vdoing | Copyright © 2025-2025 plantre | MIT License
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式