楼主: adbase
跳转到指定楼层
上一主题 下一主题
收起左侧

[每天两道题]坚持找到工作为止

   
🔗
 楼主| adbase 2022-6-18 15:02:41 | 只看该作者
全局:
207. Course Schedule 210. Course Schedule II
拓扑排序,拓扑排序是非常重要的图论的写法,之前就没有太理解,现在更加深刻了一些。
dfs写法可能对我比较好理解。

我认为拓扑排序主要难度在于编程技巧……
每一个节点一共有三个状态,状态1,未读取,状态2,正在读取,状态3, 已经读取。
默认都是未读取的状态。
我们从入度为0的节点开始,
先把它标记为状态2, 然后遍历它的邻居。
若是它没有邻居,就把它标记为已读取。再继续找下一个入读为零的节点重新开始。。
若是它有邻居,就递归进去。
当所有邻居都遍历完成,我们就把当前节点标记未状态3已读取。

递归的返回条件有两个,一个是遇到状态3,那么我们直接返回。另一个是遇到状态2, 此时说明有环。

这就是总体的逻辑,但是问题在于如何编程。
我看了看最简单的写法是定义一个int[] visit = new int[numCourses];
visit[i] = 0 状态 1 未读取
visit[i] = -1 状态2 在读取
visit[i] = 1 状态3 已读取

每次进入递归先判断visit的值,然后根据值返回或继续。若是visit[i] = 0,我们就先让它为 -1,再遍历邻居,再设置为1。

所以代码就是
  1. class Solution {
  2.     List<Integer> rs = new ArrayList<>();
  3.     public int[] findOrder(int numCourses, int[][] prerequisites) {
  4.         if( prerequisites.length == 0){
  5.             int[] res = new int[numCourses];
  6.             for(int i = 0; i < numCourses; i++) {
  7.                res[i] = i;
  8.             }
  9.             return res;
  10.         }
  11.         Map<Integer, List<Integer>> map = new HashMap<>();
  12.         
  13.         for(int[] a : prerequisites) {
  14.             List<Integer> l = map.getOrDefault(a[0], new ArrayList<>());
  15.             l.add(a[1]);
  16.             map.put(a[0], l);
  17.         }
  18.         int[] visit = new int[numCourses];
  19.         for(int i = 0; i < numCourses; i++) {
  20.             if(!helper(i, visit, map)) return new int[0];
  21.         }
  22.         
  23.         int[] res = new int[numCourses];
  24.         for(int i = 0; i < rs.size(); i++) {
  25.             res[i] = rs.get(i);
  26.         }
  27.         return res;
  28.     }
  29.    
  30.     private boolean helper(int node, int[] visit, Map<Integer, List<Integer>> map) {
  31.         if(visit[node] > 0) {
  32.             return true;
  33.         }
  34.         if(visit[node] < 0){
  35.             return false;
  36.         }
  37.         
  38.         visit[node] = -1;
  39.         
  40.         if(map.containsKey(node)) {
  41.             List<Integer> nei = map.get(node);
  42.             for(int next : nei) {
  43.                 if(!helper(next, visit, map)) return false;
  44.             }
  45.         }
  46.         
  47.         visit[node] = 1;
  48.         rs.add(node);
  49.         return true;
  50.     }
  51. }
复制代码
回复

使用道具 举报

🔗
 楼主| adbase 2022-6-19 15:24:21 | 只看该作者
全局:
209. Minimum Size Subarray Sum
  1. class Solution {
  2.     public int minSubArrayLen(int target, int[] nums) {
  3.         int rs = Integer.MAX_VALUE;
  4.         int sum = 0;
  5.         int l = 0;
  6.         for(int i = 0; i < nums.length; i++) {
  7.             sum += nums[i];
  8.             while(sum >= target ) {
  9.                 rs = Math.min(rs, i - l + 1);
  10.                 sum -= nums[l];
  11.                 l++;
  12.             }
  13.         }
  14.         return rs == Integer.MAX_VALUE ? 0 : rs;
  15.     }
  16. }
复制代码
回复

使用道具 举报

🔗
 楼主| adbase 2022-6-19 15:25:04 | 只看该作者
全局:
208. Implement Trie (Prefix Tree)
  1. class Trie {

  2.     Node root;
  3.     public Trie() {
  4.         root = new Node();
  5.     }
  6.    
  7.     public void insert(String word) {
  8.         Node curr = root;
  9.         for(char c : word.toCharArray()) {
  10.             curr.add(c);
  11.             curr = curr.find(c);
  12.         }
  13.         curr.isWord = true;
  14.     }
  15.    
  16.     public boolean search(String word) {
  17.         Node curr = root;
  18.         for(char c : word.toCharArray()) {
  19.             curr = curr.find(c);
  20.             if(curr == null) return false;
  21.         }
  22.         return curr.isWord;
  23.     }
  24.    
  25.     public boolean startsWith(String prefix) {
  26.         Node curr = root;
  27.         for(char c : prefix.toCharArray()) {
  28.             curr = curr.find(c);
  29.             if(curr == null) return false;
  30.         }
  31.         return true;
  32.     }
  33. }

  34. class Node{
  35.     char c;
  36.     Map<Character, Node> next;
  37.     boolean isWord;
  38.    
  39.     public Node() {
  40.         next = new HashMap<>();
  41.         isWord = false;
  42.     }
  43.    
  44.     public Node(char c) {
  45.         this.c = c;
  46.         next = new HashMap<>();
  47.         isWord = false;
  48.     }
  49.    
  50.     public void add(char c) {
  51.         if(find(c) == null){
  52.           next.put(c, new Node(c));  
  53.         }
  54.     }
  55.    
  56.     public Node find(char c) {
  57.         if(next.containsKey(c)) return next.get(c);
  58.         return null;
  59.     }
  60. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表