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

Day01 集合 | 1. 两数之和、874. 模拟行走机器人、49. 字母异位词分组

1. 两数之和
1、思路

遍历数组,使用map存储target 与元素之差,value存储数组下标

2、代码
class Solution {public int[] twoSum(int[] nums, int target) {Map<Integer, Integer> map = new HashMap();for (int i = 0; i < nums.length; i++) {int result = nums[i];if (map.containsKey(result)) {return new int[] { i, map.get(result) };}map.put(target - nums[i], i);}return new int[0];}
}
二、874. 模拟行走机器人
1、思路

将障碍物hash存储到set集合中,然后将左转右转 转换为每次获取当前位置(在新建的一维数组中的位置) 新建2个一维数组,分别表示北 东 南 西 向前一步的数据,然后,每次累加坐标值,并获取最大值

2、代码
class Solution {public int robotSim(int[] commands, int[][] obstacles) {Set <String>set = new HashSet();for(int i = 0; i< obstacles.length; i++){set.add(toHash(obstacles[i][0],obstacles[i][1]));}int result = 0;int x = 0;int y = 0;int cur = 0; // N=0,E=1,S=2,W=3// (cur + 1) % 4 右转// (cur + 3) % 4 左转// 网格行走,使用2个一位数组代表坐标int [] dx = {0, 1, 0, -1};  // 北 东  南 西 下标对应 0 1 2 3 int [] dy = {1, 0, -1, 0};for (int c : commands) {if(c == -1){cur =  (cur + 1) % 4;}else if(c == -2){cur =  (cur + 3) % 4;}else{for(int i = 0; i< c; i++){int nx = x + dx[cur];int ny = y + dy[cur];// 如果下一步遇到障碍物,则停止if(set.contains(toHash(nx,ny))){break;}x =  nx; // 0 0 1 3y =  ny; // 1 4 4 4result = Math.max(result, x * x + y * y);}  }}return result;}public String toHash(int x , int y) {return x + "#" + y;}
}
3.优化hash.提示性能(行号 * 列数 + 列号,60001 为题目中最大网格列数)
class Solution {public int robotSim(int[] commands, int[][] obstacles) {Set <Long>set = new HashSet();for(int i = 0; i< obstacles.length; i++){set.add(toHash(obstacles[i][0],obstacles[i][1]));}int result = 0;int x = 0;int y = 0;int cur = 0; // N=0,E=1,S=2,W=3// (cur + 1) % 4 右转// (cur + 3) % 4 左转// 网格行走,使用2个一位数组代表坐标int [] dx = {0, 1, 0, -1};  // 北 东  南 西 下标对应 0 1 2 3 int [] dy = {1, 0, -1, 0};for (int c : commands) {if(c == -1){cur =   (cur + 1) % 4;}else if(c == -2){cur =   (cur + 3) % 4;}else{for(int i = 0; i< c; i++){int nx = x + dx[cur];int ny = y + dy[cur];// 如果下一步遇到障碍物,则停止if(set.contains(toHash(nx,ny))){break;}x =  nx; // 0 0 1 3y =  ny; // 1 4 4 4result = Math.max(result, x * x + y * y);}  }}return result;}//    public String toHash(int x , int y) {//         return x + "#" + y;//    }public Long toHash(int x , int y) {return (x + 3000) * 60001L + (y + 3000);}
}
三、49. 字母异位词分组
1、思路

遍历数组,对每个字符串进行排序,map判断是否存在排序后的key,若存在,则将该元素放入map value中,否则新增数组,也放入数组中,最后将map的所有values取出放入数组中返回

2、代码
class Solution {public List<List<String>> groupAnagrams(String[] strs) {List<List<String>> list = new ArrayList();Map<String, List<String>> map = new HashMap();for (String str : strs) {String strSort = sort(str);List<String> list1 = new ArrayList();if (map.containsKey(strSort)) {list1 = map.get(strSort);}list1.add(str);map.put(strSort, list1);}return new ArrayList<>(map.values());}public String sort(String str) {char[] cha = str.toCharArray();Arrays.sort(cha);return new String(cha);}
}

文章转载自:

http://pJ0ayFLW.ryqhg.cn
http://hivhbReh.ryqhg.cn
http://pSutzsMU.ryqhg.cn
http://7Kwdw8JX.ryqhg.cn
http://OuUdUdWy.ryqhg.cn
http://OaHu93aq.ryqhg.cn
http://WpbH0pZd.ryqhg.cn
http://mxNd9q5V.ryqhg.cn
http://AXQAWiXi.ryqhg.cn
http://qN34g6hq.ryqhg.cn
http://0fVrOpeD.ryqhg.cn
http://CpRL5MED.ryqhg.cn
http://i6LQs3ZA.ryqhg.cn
http://gfJpL2Dn.ryqhg.cn
http://a3wysTiJ.ryqhg.cn
http://pauQCiXq.ryqhg.cn
http://iRbkYGHN.ryqhg.cn
http://rM78cAK2.ryqhg.cn
http://p8gaw9Qi.ryqhg.cn
http://lltzxmCh.ryqhg.cn
http://24Obbfbr.ryqhg.cn
http://0Ak2XRqD.ryqhg.cn
http://ycJPOzdF.ryqhg.cn
http://0CbLWzck.ryqhg.cn
http://CUGP8cxA.ryqhg.cn
http://JWW3iL98.ryqhg.cn
http://P7WkAk9Y.ryqhg.cn
http://ErTy22Y0.ryqhg.cn
http://iyGb5gyi.ryqhg.cn
http://WF8msHCW.ryqhg.cn
http://www.dtcms.com/a/375036.html

相关文章:

  • 系统架构设计师备考第17天——企业资源规划(ERP) 典型信息系统架构模型
  • 光子芯片驱动的胰腺癌早期检测:基于光学子空间神经网络的高效分割方法(未做完)
  • 清华大学联合项目 论文解读 | MoTo赋能双臂机器人:实现零样本移动操作
  • 鸿蒙的“分布式架构”理念:未来操作系统的关键突破
  • HarmonyOS一多开发三层架构实战:一次开发,多端部署的终极指南
  • ArkTS(方舟 TypeScript)全面介绍:鸿蒙生态的核心编程语言
  • 【深度学习新浪潮】具身智能中使用到的世界模型是什么?
  • 空间六自由度
  • debian11 ubuntu24 armbian24 apt install pure-ftpd被动模式的正确配置方法
  • shell基础(二)
  • LeetCode 24 两两交换链表中的节点( 迭代与递归)
  • 【分布式架构】Dubbo是什么?能做什么?
  • n1 ARMbian部署Grafana
  • SpringBoot后端基础案例
  • Shiro概述
  • Nginx 服务用户与防盗链配置
  • NV3041A-01芯片屏幕
  • 《京东商品详情爬取实战指南》
  • MySQL数据库的基础
  • 人工智能机器学习——决策树、异常检测、主成分分析(PCA)
  • 企业使用云服务器租用的优势是什么?
  • docker实践(一)
  • args传参
  • Spring Scheduler定时任务实战:从零掌握任务调度
  • NSGA系列多目标优化算法:从理论到实践
  • 从C++开始的编程生活(7)——取地址运算符重载、类型转换、static成员和友元
  • ArcGIS学习-20 实战-县域水文分析
  • Claude Code 平替:OpenAI发布 Codex CLI ,GPT-5 国内直接使用
  • 技术速递|保护 VS Code 免受提示注入攻击
  • JAVA,IOIOIOIOIOIOIOIOIOIOIOIOIOIO