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

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

🔗
 楼主| zhangrz2 2019-6-1 09:24:57 | 只看该作者
全局:
19. Remove Nth Node From End of List -- two pointers and fake head helper

  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 removeNthFromEnd(ListNode head, int n) {
  11.         if (n <= 0) {
  12.             return head;
  13.         }
  14.         ListNode dump = new ListNode(-1);
  15.         dump.next = head;
  16.         ListNode last = dump;
  17.         ListNode slow = dump.next;
  18.         ListNode fast = dump.next;
  19.         int count = 0;
  20.         while(fast != null) {
  21.             if (count == n) {
  22.                 last = last.next;
  23.                 slow = slow.next;
  24.             }else {
  25.                 count++;
  26.             }
  27.             fast = fast.next;
  28.         }
  29.         if (slow == head) {
  30.             return slow.next;
  31.         }else {
  32.             last.next = slow.next;
  33.             slow.next = null;
  34.         }
  35.         return head;
  36.     }
  37. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-1 10:18:13 | 只看该作者
全局:
148. Sort List -- merge sort
  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. //merge sort
  10. class Solution {
  11.     public ListNode sortList(ListNode head) {
  12.         if (head == null || head.next == null) {
  13.             return head;   
  14.         }
  15.         ListNode prev = null;
  16.         ListNode slow = head;
  17.         ListNode fast = head;
  18.         //divide
  19.         while(fast != null && fast.next != null) {
  20.             prev = slow;
  21.             slow = slow.next;
  22.             fast = fast.next.next;
  23.         }
  24.         prev.next = null;
  25.         ListNode l1 = sortList(head);
  26.         ListNode l2 = sortList(slow);
  27.         
  28.         return merge(l1, l2);
  29.     }
  30.    
  31.     private ListNode merge(ListNode l1, ListNode l2) {
  32.         ListNode dump = new ListNode(-1);
  33.         ListNode head = dump;
  34.         while(l1 != null && l2 != null) {
  35.             if (l1.val < l2.val) {
  36.                 head.next = l1;
  37.                 l1 = l1.next;
  38.             } else {
  39.               head.next = l2;
  40.               l2 = l2.next;
  41.             }
  42.             head = head.next;
  43.         }
  44.         if (l1 != null) {
  45.             head.next = l1;
  46.         }
  47.         if (l2 != null) {
  48.             head.next = l2;
  49.         }
  50.             
  51.         return dump.next;
  52.     }
  53. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-1 22:17:46 | 只看该作者
全局:
75. Sort Colors - two pointer
  1. class Solution {
  2.     public void sortColors(int[] nums) {
  3.         if (nums == null || nums.length <= 1) {
  4.             return;
  5.         }
  6.         int cur = 0;
  7.         int zeroIndex = 0;
  8.         int twoIndex = nums.length - 1;
  9.         while(cur <= twoIndex) { // the last
  10.             if (nums[cur] == 0) {
  11.               swap(nums, cur++, zeroIndex++);
  12.             }else if (nums[cur] == 2) {
  13.               swap(nums, cur, twoIndex--); // don't increment cur
  14.             }else {
  15.               cur++;
  16.             }
  17.         }
  18.         return;
  19.     }
  20.    
  21.     private void swap(int[] nums, int index, int indexTwo) {
  22.         int temp = nums[index];
  23.         nums[index] = nums[indexTwo];
  24.         nums[indexTwo] = temp;
  25.     }
  26. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-2 04:01:33 | 只看该作者
全局:
56. Merge Intervals
  1. class Solution { // sorting
  2.     public int[][] merge(int[][] intervals) {
  3.         if (intervals == null || intervals.length == 0) {
  4.             return intervals;
  5.         }
  6.         List<int[]> ans = new ArrayList<>();
  7.         Arrays.sort(intervals, (a, b) -> a[1] - b[1]);
  8.         //loop reversely
  9.         //if intervals[i - 1][1] <= intervals[i][0]
  10.         //then intervals[i - 1][0] must <= than intervals[i][0]
  11.         //if loop from start
  12.         //then intervals[i][0] could be < than intervals[i - 1][0]
  13.         for (int i = intervals.length - 1; i >= 0; i--) {
  14.             int[] array = intervals[i];
  15.             if (ans.size() == 0) {
  16.                 ans.add(Arrays.copyOf(array, 2));
  17.             }else {
  18.                 int[] last = ans.get(ans.size() - 1);
  19.                 if (array[1] >= last[0]) {
  20.                     int left = Math.min(last[0], array[0]);
  21.                     int right = Math.max(last[1], array[1]);
  22.                     last[0] = left;
  23.                     last[1] = right;
  24.                     ans.set(ans.size() - 1, last);
  25.                 }else {
  26.                     ans.add(Arrays.copyOf(array, 2));
  27.                 }
  28.             }
  29.         }
  30.         int[][] ansArray = new int[ans.size()][2];
  31.         for (int i = 0; i < ans.size(); i++) {
  32.             int[] unit = ans.get(i);
  33.             ansArray[i][0] = unit[0];
  34.             ansArray[i][1] = unit[1];
  35.         }
  36.         return ansArray;
  37.     }
  38. }
复制代码

回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-2 04:42:16 | 只看该作者
全局:
252. Meeting Rooms -- sorting

  1. class Solution {
  2.     public boolean canAttendMeetings(int[][] intervals) {
  3.         if (intervals == null || intervals.length <= 1) {
  4.             return true;
  5.         }
  6.         Arrays.sort(intervals, (a, b) -> a[0] - b[0]);
  7.         for (int i = 1; i < intervals.length; i++) {
  8.             if (intervals[i][0] < intervals[i-1][1]) {
  9.                 return false;
  10.             }
  11.         }
  12.         return true;
  13.     }
  14. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-2 07:14:04 | 只看该作者
全局:
445. Add Two Numbers II

  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 {// if you need to reverse originally order, keep stack inmind
  10.     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
  11.         if (l1 == null) {
  12.             return l2;
  13.         }
  14.         if (l2 == null) {
  15.             return l1;
  16.         }
  17.         Stack<ListNode> l1Stack = new Stack<>();
  18.         Stack<ListNode> l2Stack = new Stack<>();
  19.         while(l1 != null) {
  20.             l1Stack.push(l1);
  21.             l1 = l1.next;
  22.         }
  23.         while(l2 != null) {
  24.             l2Stack.push(l2);
  25.             l2 = l2.next;
  26.         }
  27.         ListNode prev = null;
  28.         ListNode head = null;
  29.         int carry = 0;
  30.         while(!l1Stack.isEmpty() && !l2Stack.isEmpty()) {
  31.             int val = l1Stack.pop().val + l2Stack.pop().val + carry;
  32.             head = new ListNode(val % 10);
  33.             carry = val / 10;
  34.             head.next = prev;
  35.             prev = head;
  36.         }
  37.         
  38.         while(!l1Stack.isEmpty()) {
  39.             int val = l1Stack.pop().val + carry;
  40.             head = new ListNode(val % 10);
  41.             carry = val / 10;
  42.             head.next = prev;
  43.             prev = head;
  44.         }
  45.         
  46.         while(!l2Stack.isEmpty()) {
  47.             int val = l2Stack.pop().val + carry;
  48.             head = new ListNode(val % 10);
  49.             carry = val / 10;
  50.             head.next = prev;
  51.             prev = head;
  52.         }
  53.         if (carry != 0) {
  54.             head = new ListNode(carry);
  55.             head.next = prev;
  56.         }
  57.         
  58.         return head;
  59.         
  60.     }
  61. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-2 08:02:46 | 只看该作者
全局:
147. Insertion Sort List -- basic

  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.     // insert the right node into right place
  11.     public ListNode insertionSortList(ListNode head) {
  12.         if (head == null || head.next == null) {
  13.             return head;
  14.         }
  15.         ListNode dummy = new ListNode(0);
  16.         ListNode prev = dummy;
  17.         while(head != null) {
  18.             ListNode next = head.next; // mark down the next node to be insert
  19.             if (head.val < prev.val) {
  20.                 prev = dummy; // reset the prev position
  21.             }
  22.             while(prev.next != null && prev.next.val < head.val) {// find the position
  23.                 prev = prev.next;
  24.             }
  25.             
  26.             head.next = prev.next;
  27.             prev.next = head;
  28.             head = next;
  29.         }
  30.         return dummy.next;
  31.     }
  32. }
复制代码
回复

使用道具 举报

🔗
Mr_rabbit 2019-6-2 08:54:51 | 只看该作者
全局:
同多伦多在职刷题!求组队
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-2 11:23:33 | 只看该作者
全局:
937. Reorder Log Files --sorting super slow
  1. class Solution {
  2.     public String[] reorderLogFiles(String[] logs) {
  3.         if (logs == null || logs.length == 0) {
  4.             return logs;
  5.         }
  6.         Arrays.sort(logs, (str1, str2) -> {
  7.             if (isDigitLog(str1) && isDigitLog(str2)) {
  8.                 return 0;
  9.             }else if (isDigitLog(str1)) {
  10.                 return 1;
  11.             }else if (isDigitLog(str2)) {
  12.                 return -1;
  13.             }else {
  14.                 int result = str1.substring(findLog(str1), str1.length()).compareTo(str2.substring(findLog(str2), str2.length()));
  15.                 return result == 0 ? str1.compareTo(str2) : result;
  16.             }
  17.         });
  18.         return logs;
  19.         
  20.     }
  21.    
  22.     private boolean isDigitLog(String log) {
  23.         String[] split = log.split("\\s");
  24.         return split[1].matches("\\d+");
  25.     }
  26.    
  27.     private int findLog(String log) {
  28.         int index = 0;
  29.         for (int i = 0; i < log.length(); i++) {
  30.             if (log.charAt(i) == ' ') {
  31.                 index = i;
  32.                 break;
  33.             }
  34.         }
  35.         return index;
  36.     }
  37. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-6-3 04:16:11 | 只看该作者
全局:
460. LFU Cache -- 自己搞了好长时间没用bug free了,找个个答案,思路明白了

  1. class Node {
  2.         public Node prev, next;
  3.         public final int count;
  4.         public LinkedHashSet<Integer> keys = new LinkedHashSet<>();

  5.         public Node(Node prev, Node next, int count, int key) {
  6.             this.prev = prev;
  7.             this.next = next;
  8.             this.count = count;
  9.             keys.add(key);
  10.         }
  11. }

  12. class LFUCache {
  13.     Node head = null;
  14.     final int capacity;
  15.     Map<Integer, Integer> valueMap = new HashMap<>();
  16.     Map<Integer, Node> nodeMap = new HashMap<>();

  17.     public LFUCache(int capacity) {
  18.         this.capacity = capacity;
  19.     }

  20.     public int get(int key) {
  21.         if (nodeMap.containsKey(key)) increase(key);
  22.         return valueMap.getOrDefault(key, -1);
  23.     }

  24.     private void increase(int key) {
  25.         Node node = nodeMap.get(key);
  26.         node.keys.remove(key);
  27.         if (Objects.isNull(node.next)) node.next = new Node(node, null, node.count + 1, key);
  28.         else if (node.next.count == node.count + 1) node.next.keys.add(key);
  29.         else node.next = node.next.prev = new Node(node, node.next, node.count + 1, key);
  30.         nodeMap.put(key, node.next);
  31.         if (node.keys.isEmpty()) remove(node);
  32.     }

  33.     private void remove(Node node) {
  34.         if (head == node) head = node.next;
  35.         else node.prev.next = node.next;
  36.         if (Objects.nonNull(node.next)) node.next.prev = node.prev;
  37.     }

  38.     public void put(int key, int value) {
  39.         if (0 == this.capacity) return;
  40.         if (Objects.nonNull(valueMap.put(key, value))) increase(key);
  41.         else {
  42.             if (nodeMap.size() == this.capacity) remove();
  43.             add(key);
  44.         }
  45.     }

  46.     private void add(int key) {
  47.         if (Objects.isNull(head)) head = new Node(null, null, 1, key);
  48.         else if (head.count == 1) head.keys.add(key);
  49.         else head = head.prev = new Node(null, head, 1, key);
  50.         nodeMap.put(key, head);
  51.     }

  52.     private void remove() {
  53.         int oldest = head.keys.iterator().next();
  54.         head.keys.remove(oldest);
  55.         if (head.keys.isEmpty()) remove(head);
  56.         nodeMap.remove(oldest);
  57.         valueMap.remove(oldest);
  58.     }
  59. }
复制代码
回复

使用道具 举报

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

本版积分规则

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