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

leetcode做题

简单题开场

290. 单词规律

class Solution { public boolean wordPattern(String pattern, String s) { String[] words = s.split(" "); Map<Character, String> pToS = new HashMap<>(); Map<String, Character> sToP = new HashMap<>(); if(words.length != pattern.length()) return false; for(int i = 0; i < pattern.length(); i++){ char c = pattern.charAt(i); if((pToS.containsKey(c) && (!pToS.get(c).equals(words[i]))) || (sToP.containsKey(words[i]) && sToP.get(words[i]) != c) ){ return false; } pToS.put(c, words[i]); sToP.put(words[i], c); } return true; } }

36. 有效的数独

自己做的时候是行,列,九宫分开写的,看题解才知道可以三合一的写

class Solution { public boolean isValidSudoku(char[][] board) { //三合一 开出三个数组 int[][] rowHas = new int[9][9]; //rowhas[i][j]表示第i行有数字j int[][] colHas = new int[9][9]; //colhas[i][j]表示第i列有数字j int[][][] subBoxHas = new int[3][3][9]; //subBoxHas[i][j][x] 表示 第i,j个九宫格有数字x for(int i = 0; i < 9; i++){ for(int j = 0; j < 9; j++){ char c = board[i][j]; if(c == '.') continue; //将数字1~9 转为 0 ~ 8 int temp = c - '0' - 1; rowHas[i][temp]++; colHas[j][temp]++; subBoxHas[i / 3][j / 3][temp]++; if(rowHas[i][temp] > 1 || colHas[j][temp] > 1 || subBoxHas[i / 3][j / 3][temp] > 1){ return false; } } } return true; } }

54. 螺旋矩阵

以前在洛谷上做过,没想到还是不会

class Solution { public List<Integer> spiralOrder(int[][] matrix) { List<Integer> ans = new ArrayList<>(); int n = matrix.length, m = matrix[0].length; int left = 0, right = m - 1, top = 0, bottom = n - 1; while(ans.size() < m * n){ for(int j = left; j <= right && ans.size() < m * n; j++){ ans.add( matrix[top][j] ); } top++; for(int i = top; i <= bottom && ans.size() < m * n; i++){ ans.add( matrix[i][right] ); } right--; for(int j = right; j >= left && ans.size() < m * n; j--){ ans.add( matrix[bottom][j] ); } bottom--; for(int i = bottom; i >= top && ans.size() < m * n; i--){ ans.add( matrix[i][left] ); } left++; } return ans; } }
http://www.jsqmd.com/news/763636/

相关文章:

  • AI命令行工具进程监控与通知系统:提升开发效率的智能外挂
  • 麦克斯韦方程组:电磁场理论的基石与工程应用
  • 终极FF14国际服汉化指南:3分钟实现全中文界面体验
  • 二进制报警器 学习笔记
  • 新手必看:TMS320F280049最小系统板DIY,从选型到电源设计的保姆级避坑指南
  • 2026 年 5 月国内外在线浊度仪十大品牌排名 - 仪表人小余
  • AI建站工具全流程指南:零基础如何从0到1搭建个人品牌网站
  • 用PyTorch手把手教你实现LoRA:从Linear到ConvLoRA的完整代码解析
  • 数学建模小白避坑指南:线性规划建模常见5大误区及Matlab的linprog函数正确打开方式
  • 为内部知识库问答系统集成Taotoken提供的多模型能力
  • 基于GPT的终端AI助手开发:从原理到工程实践
  • free-fs BOPLA VULNs Report
  • 从Matlab仿真到嵌入式C代码:雷达CFAR加速核的实战配置与参数调优指南
  • 【边缘AI场景Docker调优白皮书】:基于Raspberry Pi 5/JeVois-Bin/NVIDIA Jetson实测数据的12项关键参数配置清单
  • 音频重采样(Audio Resampling)实现指南
  • 别再一个个部署模型了!用Xinference在AutoDL上一次性搞定Embedding、Rerank和Qwen(附完整命令清单)
  • AI 英语伴学 APP的开发
  • 量子网络模拟中的张量网络技术与应用
  • 新手猫粮创业者的避坑指南与成功攻略
  • 【前端(十三)】JavaScript 数组与字符串笔记
  • Mac mini 从零开始:新建隔离用户 + 完整安装 Hermes Agent
  • 别再只会用等号了!C++ vector赋值,swap和assign到底哪个更快?
  • 程序化噪声在游戏开发中的应用:从Perlin到Shader实战
  • Barlow字体超级家族:如何用一个开源字体解决你的多平台设计统一难题
  • 效率提升:用快马ai一键生成winutil多模块工具箱代码框架
  • Golden UPF Flow实战解析:如何用一份UPF搞定RTL到门级的低功耗验证
  • LIDA:基于大语言模型的自然语言数据可视化代码生成工具
  • 5个常见游戏控制器兼容性难题:XOutput如何让旧手柄在现代游戏中重获新生
  • Obsidian BMO Chatbot:在笔记软件中集成AI助手的配置与实战指南
  • 为Alexa注入ChatGPT灵魂:智能语音助手开发实战指南