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

滑动窗口438.找到字符串中所有的字母异位词|链表|206.反转链表92.反转链表II

438.找到字符串中所有的字母异位词

思路:固定窗口

点击查看代码
class Solution {public List<Integer> findAnagrams(String s, String p) {List<Integer> result = new ArrayList<>();if (s.length() < p.length()) {return result;}// 统计pMap<Character, Integer> needMatch = new HashMap<>();// 统计windowMap<Character, Integer> window = new HashMap<>();// 初始化needMatchfor (char c : p.toCharArray()) {needMatch.put(c, needMatch.getOrDefault(c, 0) + 1);}int left = 0;int match = 0;for(int right = 0; right < s.length(); right++){char c = s.charAt(right);char l = s.charAt(left);//2hashif(needMatch.containsKey(c)){window.put(c, window.getOrDefault(c,0) + 1);if(needMatch.get(c).intValue() == window.get(c).intValue()){match++;}}if(right-left + 1 == p.length()){if(match == needMatch.size()){result.add(left);}//2hashif(needMatch.containsKey(l)){if(needMatch.get(l).intValue() == window.get(l).intValue()){match--;}window.put(l, window.get(l) - 1);}left++;}}return result;}
}

反转链表

思路:pre指针,cur指针,记录next。更改当前的,移动pre和cur。

点击查看代码
/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode() {}*     ListNode(int val) { this.val = val; }*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {public ListNode reverseList(ListNode head) {ListNode pre = null;ListNode cur = head;while(cur != null){ListNode next = cur.next;cur.next = pre;pre = cur;cur = next;}return pre;}
}

92.反转链表II

点击查看代码
/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode() {}*     ListNode(int val) { this.val = val; }*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
class Solution {public ListNode reverseBetween(ListNode head, int left, int right) {ListNode dummy = new ListNode(0, head);ListNode p0 = dummy;for(int i = 0; i < left - 1; i++){p0 = p0.next;}ListNode pre = null;ListNode cur = p0.next;for(int i = 0; i < right - left + 1; i++){ListNode next = cur.next;cur.next = pre;pre = cur;cur = next;}p0.next.next = cur;p0.next = pre;return dummy.next;}
}
http://www.gsyq.cn/news/148340.html

相关文章:

  • 能检测老人摔倒的养老机器人:守护银发安全的智能助手 - 资讯焦点
  • 方法调用时,Java 的及早求值(eager evaluation)策略
  • 2025年AI搜索优化服务市场观察:技术演进与企业适配路径 - 品牌测评鉴赏家
  • 解析 `std::deque` 的‘地图-缓冲区’内存结构:为什么它是实现高性能栈(Stack)的首选底座?
  • 2025 MBA必看!10个降AI率工具测评榜单
  • 软件测试工程师的35岁危机:是确有其事,还是伪命题?
  • 五、定积分
  • 什么是 ‘ABI Breaking’?为什么给 `std::list` 增加一个成员变量会引发整个操作系统的崩溃风险?
  • 第六次团队作业-事后分析部分
  • 孩子想学人工智能?2025年高口碑机构选课攻略来了 - 品牌测评鉴赏家
  • 解析 C++20 `std::ranges` 管道符:如何利用延迟求值(Lazy Evaluation)处理无限序列?
  • 解析 ‘Flyweight Pattern’:在高频交易系统中如何共享千万级订单数据的元信息?
  • 第六次团队作业——事后分析部分
  • 2025年市面上专业的换热器公司哪家靠谱,工业型暖风机/侧吹风幕机/空气换热器/工业风幕机,换热器品牌排行 - 品牌推荐师
  • 互联网大厂Java面试:从Spring Boot到微服务架构的场景应用
  • 完整教程:openGauss在AI时代的向量数据库应用实践与技术演进深度解析
  • 基于链动2+1模式、AI智能名片与S2B2C商城小程序的粉丝社群策划与实施研究
  • AI时代流量密码:揭秘顶尖搜索排名优化公司 - 品牌测评鉴赏家
  • 河南保护膜标杆企业!众思创PE_铝天花保护膜赋能建材产业 - 朴素的承诺
  • 12312
  • 2025权威发布!新加坡留学中介服务实力榜单揭晓 - 留学品牌推荐官
  • 工业嵌入式系统串行接口:技术演进、核心优势与选型指南
  • 第六次团队作业-复审部分
  • 哈希-03-字母异位词分组
  • 教育博主实测:2025年高性价比AI智能体开发服务推荐指南 - 品牌测评鉴赏家
  • PyTorch MNIST全连接分类器完整流程
  • 深入解析:基于Spring Boot 3 + Spring Security6 + JWT + Redis实现登录、token身份认证
  • 一些Android平台的早期J2ME实现方案的情况
  • 揭秘沃尔玛购物卡回收猫腻,教你安全避坑 - 京顺回收
  • 2025最新!10个AI论文平台测评:研究生写论文必备神器