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

多伦多在职每日刷题打卡也欢迎组队

🔗
 楼主| zhangrz2 2019-5-29 05:16:56 | 只看该作者
全局:
359. Logger Rate Limiter

  1. class Logger {
  2.     Map<String, Integer> records;
  3.     /** Initialize your data structure here. */
  4.     public Logger() {
  5.         records = new HashMap<>();
  6.     }
  7.    
  8.     /** Returns true if the message should be printed in the given timestamp, otherwise returns false.
  9.         If this method returns false, the message will not be printed.
  10.         The timestamp is in seconds granularity. */
  11.     public boolean shouldPrintMessage(int timestamp, String message) {
  12.         if (timestamp < records.getOrDefault(message, 0)) {
  13.             return false;
  14.         }
  15.         records.put(message, timestamp + 10);
  16.         return true;
  17.     }
  18. }

  19. /**
  20. * Your Logger object will be instantiated and called as such:
  21. * Logger obj = new Logger();
  22. * boolean param_1 = obj.shouldPrintMessage(timestamp,message);
  23. */
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-5-30 09:43:51 | 只看该作者
全局:
381. Insert Delete GetRandom O(1) - Duplicates allowed

  1. class RandomizedCollection {
  2.     List<Integer> vals;
  3.     Map<Integer, List<Integer>> map;
  4.     java.util.Random rand = new java.util.Random();
  5.     /** Initialize your data structure here. */
  6.     public RandomizedCollection() {
  7.         vals = new ArrayList<Integer>();
  8.         map = new HashMap<>();
  9.     }
  10.    
  11.     /** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
  12.     public boolean insert(int val) {
  13.         boolean contains = map.containsKey(val);
  14.         int index = vals.size();
  15.         List<Integer> indexes = map.getOrDefault(val, new ArrayList<Integer>());
  16.         indexes.add(index);
  17.         map.put(val, indexes);
  18.         vals.add(val);
  19.         return !contains;
  20.     }
  21.    
  22.     /** Removes a value from the collection. Returns true if the collection contained the specified element. */
  23.     public boolean remove(int val) {
  24.         if (vals.size() == 0 || !map.containsKey(val)) {
  25.             return false;
  26.         }
  27.         List<Integer> indexes = map.get(val);
  28.         int index = indexes.get(indexes.size() - 1);
  29.         indexes.remove(indexes.size() - 1);
  30.         int last = vals.get(vals.size() - 1);
  31.         if (val != last) {
  32.             vals.set(index, last);
  33.             List<Integer> newIndexes = map.get(last);
  34.             newIndexes.remove(newIndexes.size() - 1);
  35.             newIndexes.add(index);
  36.             Cllections.sort(newIndexes);
  37.             map.put(last, newIndexes);
  38.         }
  39.         vals.remove(vals.size() - 1);
  40.         if (indexes.size() == 0) {
  41.             map.remove(val);
  42.         }else {
  43.             map.put(val, indexes);
  44.         }
  45.         return true;
  46.         
  47.     }
  48.    
  49.     /** Get a random element from the collection. */
  50.     public int getRandom() {
  51.         if (vals.size() == 0) {
  52.             return -1;
  53.         }
  54.         int index = rand.nextInt(vals.size());
  55.         return vals.get(index);
  56.     }
  57. }

  58. /**
  59. * Your RandomizedCollection object will be instantiated and called as such:
  60. * RandomizedCollection obj = new RandomizedCollection();
  61. * boolean param_1 = obj.insert(val);
  62. * boolean param_2 = obj.remove(val);
  63. * int param_3 = obj.getRandom();
  64. */
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-5-30 09:46:21 | 只看该作者
全局:
[quote]zhangrz2 发表于 2019-5-30 09:43
381. Insert Delete GetRandom O(1) - Duplicates allowed

  1. class RandomizedCol ...[/quote]
  2. Other people solution -- creating own class to store value and index(bravo)

  3. [code]class NumberAndIndex {
  4.     public int number, index;
  5.     public NumberAndIndex(int number, int index) {
  6.         this.number = number;
  7.         this.index = index;
  8.     }
  9. }

  10. public class RandomizedCollection {
  11.     // pair.number is the number, pair.index is the index in map value
  12.     private List<NumberAndIndex> nums;
  13.     // key is the number, value if the indices list in nums;
  14.     private Map<Integer, List<Integer>> map;
  15.     private Random rand;

  16.     /** Initialize your data structure here. */
  17.     public RandomizedCollection() {
  18.         map = new HashMap<>();
  19.         nums = new ArrayList<>();
  20.         rand = new Random();
  21.     }
  22.    
  23.     /** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */
  24.     public boolean insert(int val) {
  25.         boolean existed = map.containsKey(val);
  26.         
  27.         if (!existed) {
  28.             map.put(val, new ArrayList<Integer>());
  29.         }
  30.         List<Integer> indices = map.get(val);
  31.         indices.add(nums.size());
  32.         nums.add(new NumberAndIndex(val, indices.size() - 1));

  33.         return !existed;
  34.     }
  35.    
  36.     /** Removes a value from the collection. Returns true if the collection contained the specified element. */
  37.     public boolean remove(int val) {
  38.         if (!map.containsKey(val)) {
  39.             return false;
  40.         }
  41.    
  42.         List<Integer> indices = map.get(val);
  43.         int index = indices.get(indices.size() - 1);
  44.         
  45.         NumberAndIndex numIndex = nums.get(nums.size() - 1);
  46.         nums.set(index, numIndex);
  47.         nums.remove(nums.size() - 1);
  48.         map.get(numIndex.number).set(numIndex.index, index);
  49.         
  50.         indices.remove(indices.size() - 1);
  51.         if (indices.size() == 0) {
  52.             map.remove(val);
  53.         }
  54.         
  55.         return true;
  56.     }
  57.    
  58.     /** Get a random element from the collection. */
  59.     public int getRandom() {
  60.         int index = rand.nextInt(nums.size());
  61.         return nums.get(index).number;
  62.     }
  63. }
复制代码
回复

使用道具 举报

🔗
adayxiang 2019-5-30 10:40:15 | 只看该作者
全局:
楼主加个微信?ID:tll9514
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-5-30 10:42:57 | 只看该作者
全局:
77. Combinations

  1. class Solution {
  2.     public List<List<Integer>> combine(int n, int k) {
  3.         List<List<Integer>> ans = new ArrayList<List<Integer>>();
  4.         if (k <= 0) {
  5.             return ans;
  6.         }
  7.         buildList(ans, new ArrayList<Integer>(), 1, n, k);
  8.         return ans;
  9.     }
  10.    
  11.     private void buildList(List<List<Integer>> ans, List<Integer> list, int start, int n, int k) {
  12.         if (list.size() == k) {
  13.             ans.add(new ArrayList<>(list));
  14.             return;
  15.         }
  16.         for (int i = start; i <= n; i++) {
  17.             list.add(i);
  18.             buildList(ans, list, i + 1, n, k);
  19.             list.remove(list.size() - 1);
  20.         }
  21.         return;
  22.     }
  23. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-5-30 10:43:38 | 只看该作者
全局:
138. Copy List with Random Pointer

  1. /*
  2. // Definition for a Node.
  3. class Node {
  4.     public int val;
  5.     public Node next;
  6.     public Node random;

  7.     public Node() {}

  8.     public Node(int _val,Node _next,Node _random) {
  9.         val = _val;
  10.         next = _next;
  11.         random = _random;
  12.     }
  13. };
  14. */
  15. class Solution {
  16.     public Node copyRandomList(Node head) {
  17.         if (head == null) {
  18.             return null;
  19.         }
  20.         Node oriHead = head;
  21.         Node dump = new Node(-1);
  22.         dump.next = new Node(oriHead.val);
  23.         Map<Integer, Node> map = new HashMap<>();
  24.         map.put(oriHead.val, dump.next);
  25.         while (oriHead != null) {
  26.             int curVal = oriHead.val;
  27.             Node curNode = map.getOrDefault(curVal, new Node(curVal));
  28.             if (oriHead.next != null) {
  29.                 curNode.next = map.getOrDefault(oriHead.next.val, new Node(oriHead.next.val));
  30.                 map.put(curNode.next.val, curNode.next);
  31.             }
  32.             if(oriHead.random != null) {
  33.                  Node curRan = map.getOrDefault(oriHead.random.val, new Node(oriHead.random.val));
  34.                  curNode.random = curRan;
  35.                  map.put(curNode.random.val, curNode.random);
  36.             }
  37.             oriHead = oriHead.next;
  38.             curNode = curNode.next;
  39.         }
  40.         return dump.next;
  41.     }
  42. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-5-31 08:26:11 | 只看该作者
全局:
234. Palindrome Linked List -- two pointer

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. *     int val;
  5. *     ListNode next;
  6. *     ListNode(int x) { val = x; }
  7. * }
  8. */
  9. class Solution {
  10.     public boolean isPalindrome(ListNode head) {
  11.         if (head == null || head.next == null) {
  12.             return true;
  13.         }
  14.         ListNode slow = head;
  15.         ListNode fast = head;
  16.         while(fast != null && fast.next != null) {
  17.             fast = fast.next.next;
  18.             slow = slow.next;
  19.         }
  20.         if (fast != null) {// odd number of nodes;
  21.             slow = slow.next;
  22.         }
  23.         slow = reverse(slow);
  24.         fast = head;
  25.         while(fast != null && slow != null) {
  26.             if (fast.val != slow.val) {
  27.                 return false;
  28.             }
  29.             fast = fast.next;
  30.             slow = slow.next;
  31.         }
  32.         
  33.         return true;
  34.     }
  35.    
  36.     private ListNode reverse(ListNode head) {
  37.         ListNode prev = null;
  38.         while(head != null) {
  39.             ListNode next = head.next;
  40.             head.next = prev;
  41.             prev = head;
  42.             head = next;
  43.         }
  44.         return prev;
  45.     }
  46. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-5-31 09:21:51 | 只看该作者
全局:
11. Container With Most Water -- two pointer

  1. class Solution {
  2.     public int maxArea(int[] height) {
  3.         if (height == null || height.length < 2) {
  4.             return 0;
  5.         }
  6.         int start = 0;
  7.         int end = height.length - 1;
  8.         int ans = 0;
  9.         while(start < end) {
  10.             int left = height[start];
  11.             int right = height[end];
  12.             ans = Math.max(ans, Math.min(left, right) * (end - start));
  13.             if (left < right) {
  14.                 start++;
  15.             }else {
  16.                 end--;
  17.             }
  18.         }
  19.         return ans;
  20.     }
  21. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-5-31 09:59:05 | 只看该作者
全局:
328. Odd Even Linked List -- in place O(N)
  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. *     int val;
  5. *     ListNode next;
  6. *     ListNode(int x) { val = x; }
  7. * }
  8. */
  9. class Solution {
  10.     public ListNode oddEvenList(ListNode head) {
  11.         if (head == null) {
  12.             return null;
  13.         }
  14.         ListNode dump = new ListNode(-1);
  15.         dump.next = head;
  16.         ListNode odd = new ListNode(-1);
  17.         ListNode even = new ListNode(-1);
  18.         ListNode newOdd = odd;
  19.         ListNode newEven = even;
  20.         while(head != null && head.next != null) {
  21.             ListNode next = head.next.next;
  22.             odd.next = head;
  23.             even.next = head.next;
  24.             head.next.next = null;
  25.             head.next = null;
  26.             head = next;
  27.             odd = odd.next;
  28.             even = even.next;
  29.         }
  30.         if (head != null) {
  31.             odd.next = head;
  32.             odd = odd.next;
  33.         }
  34.         odd.next = newEven.next;
  35.         return dump.next;
  36.     }
  37. }
复制代码

回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-1 09:00:49 | 只看该作者
全局:
238. Product of Array Except Self -- math(optimize from brute force)

  1. class Solution {
  2.     public int[] productExceptSelf(int[] nums) {
  3.         int[] ans = new int[nums.length];
  4.         int start = 1;
  5.         for (int i = 0; i < nums.length; i++) {
  6.             if (i == 0) {
  7.                 ans[i] = 1;
  8.             }else {
  9.                 ans[i] = start;
  10.             }
  11.             start = start * nums[i];
  12.         }
  13.         start = nums[nums.length - 1];
  14.         for (int i = nums.length - 2; i >= 0; i--) {
  15.             ans[i] = start * ans[i];
  16.             start = start * nums[i];
  17.         }
  18.         return ans;
  19.     }
  20. }
复制代码
回复

使用道具 举报

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

本版积分规则

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