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

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

🔗
 楼主| zhangrz2 2019-7-28 00:30:13 | 只看该作者
全局:
61. Rotate List

  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.     //two pointer
  11.     public ListNode rotateRight(ListNode head, int k) {
  12.         if (head == null || head.next == null || k <= 0) {
  13.             return head;
  14.         }
  15.         int len = 0;
  16.         ListNode dumpHead = head;
  17.         while(dumpHead != null) {
  18.             dumpHead = dumpHead.next;
  19.             len++;
  20.         }
  21.         k = k % len;
  22.         if (k == 0) {
  23.             return head;
  24.         }
  25.         int offset = len - k;
  26.         ListNode newHead = head;
  27.         while(offset > 0) {
  28.             if (offset == 1) {
  29.                 ListNode next = newHead.next;
  30.                 newHead.next = null;
  31.                 newHead = next;
  32.             }else {
  33.                 newHead = newHead.next;
  34.             }
  35.             offset--;
  36.         }
  37.         dumpHead = newHead;
  38.         while(dumpHead.next != null) {
  39.             dumpHead = dumpHead.next;
  40.         }
  41.         dumpHead.next = head;
  42.         return newHead;
  43.     }
  44. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-7-29 02:32:33 | 只看该作者
全局:
787. Cheapest Flights Within K Stops

  1. class Solution {
  2.     public int findCheapestPrice(int n, int[][] flights, int src, int dst, int K) {
  3.         //using Dijkstra with priorityqueue, so we have global min cost
  4.         Map<Integer, Map<Integer, Integer>> prices = new HashMap<>();
  5.         for (int[] flight : flights) {
  6.             Map<Integer, Integer> unit = prices.getOrDefault(flight[0], new HashMap<>());
  7.             unit.put(flight[1], flight[2]);
  8.             prices.put(flight[0], unit);
  9.         }
  10.         PriorityQueue<int[]> queue = new PriorityQueue<>((a ,b) -> a[0]- b[0]);
  11.         queue.offer(new int[]{0, src, K + 1}); // K stops between, count the dst, so K + 1
  12.         while(!queue.isEmpty()) {
  13.             int[] cur = queue.poll();
  14.             if (cur[1] == dst) {
  15.                 return cur[0];
  16.             }
  17.             if (cur[2] > 0) {
  18.                 Map<Integer, Integer> connections = prices.getOrDefault(cur[1], new HashMap<>());
  19.                 for (int conn : connections.keySet()) {
  20.                     queue.offer(new int[]{cur[0] + connections.get(conn), conn, cur[2] - 1});
  21.                 }
  22.             }
  23.             
  24.         }
  25.         
  26.         return -1;
  27.     }
  28. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-7-29 05:08:35 | 只看该作者
全局:
289. Game of Life
  1. class Solution {
  2.     private int[][] directions = new int[][]{{0,1}, {0,-1}, {1,0}, {-1,0}, {1,1}, {-1,-1}, {-1,1}, {1,-1}};
  3.     public void gameOfLife(int[][] board) {
  4.         if (board == null || board.length == 0) {
  5.             return;
  6.         }
  7.         for (int i = 0; i < board.length; i++) {
  8.             for (int j = 0 ; j < board[0].length; j++) {
  9.                 int liveNei = getNumOfLiveCellsAround(i, j, board);
  10.                 if (isLiveCell(i, j, board)) {
  11.                     //live rule 1:
  12.                     if (liveNei < 2 || liveNei > 3) {
  13.                         board[i][j] = -1;
  14.                     }
  15.                 }else {
  16.                     board[i][j] = liveNei == 3 ? 2 : board[i][j];
  17.                 }
  18.             }
  19.    
  20.         }
  21.         
  22.         //return back to required
  23.         for (int i = 0; i < board.length; i++) {
  24.             for (int j = 0; j < board[0].length; j++) {
  25.                 board[i][j] = board[i][j] < 0 ? 0 : board[i][j];
  26.                 board[i][j] = board[i][j] > 1 ? 1 : board[i][j];
  27.             }
  28.         }
  29.         return;
  30.     }
  31.                                          
  32.     private int getNumOfLiveCellsAround(int row, int col, int[][] board) {
  33.         int ans = 0;
  34.         for (int[] direction : directions) {
  35.             int newRow = row + direction[0];
  36.             int newCol = col + direction[1];
  37.             if (isPositionValid(newRow, newCol, board)) {
  38.                 ans = board[newRow][newCol] == 1 || board[newRow][newCol] == -1 ? ans + 1 : ans;
  39.             }
  40.         }
  41.         return ans;
  42.     }
  43.                                          
  44.     private boolean isPositionValid(int row, int col, int[][] board) {
  45.         return row >= 0 && col >= 0 && row < board.length && col < board[0].length;
  46.     }
  47.                                          
  48.     private boolean isLiveCell(int row, int col, int[][] board) {
  49.         return board[row][col] == 1;
  50.     }
  51. }
复制代码
回复

使用道具 举报

🔗
ZT98211 2019-7-31 10:29:38 | 只看该作者
全局:
zhangrz2 发表于 2019-7-29 05:08
289. Game of Life[mw_shl_code=java,true]class Solution {
    private int[][] directions = new int[] ...

今天也做了game of life,不看solution真想不出来。。。
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-8-1 09:22:26 | 只看该作者
全局:
708. Insert into a Cyclic Sorted List

  1. class Solution {
  2.     //using TreeMap to find the store the reference
  3.     public Node insert(Node head, int insertVal) {
  4.         if (head == null) {
  5.             Node ans = new Node(insertVal);
  6.             ans.next = ans;
  7.             return ans;
  8.         }
  9.         TreeMap<Integer, List<Node>> map = new TreeMap<>();
  10.         Node dumpHead = head;
  11.         Node max = head;
  12.         Node min = head;
  13.         boolean init = true;
  14.         while(dumpHead != head || init) {
  15.             if (init) {
  16.                 init = !init;
  17.             }
  18.             if (max.val <= dumpHead.val) {
  19.                 max = dumpHead;
  20.             }
  21.             if (min.val >= dumpHead.val) {
  22.                 min = dumpHead;
  23.             }
  24.             if(!map.containsKey(dumpHead.val)) {
  25.                 List<Node> list = new ArrayList<>();
  26.                 list.add(dumpHead);
  27.                 map.put(dumpHead.val, list);
  28.             }else {
  29.                 if (dumpHead.val != dumpHead.next.val) {
  30.                     if (dumpHead != map.get(dumpHead.val).get(0)) {
  31.                         map.get(dumpHead.val).add(dumpHead);
  32.                     }
  33.                 }
  34.             }
  35.             dumpHead = dumpHead.next;
  36.         }
  37.         Integer floorKey = map.floorKey(insertVal);
  38.         if (floorKey == null) {
  39.             Integer ceilingKey = map.ceilingKey(insertVal);
  40.             Node target = new Node(insertVal);
  41.             target.next = map.get(ceilingKey).get(0);
  42.             max.next = target;
  43.         }else {
  44.             Node target = new Node(insertVal);
  45.             Node next = map.get(floorKey).get(map.get(floorKey).size() - 1).next;
  46.             map.get(floorKey).get(map.get(floorKey).size() - 1).next = target;
  47.             target.next = next;
  48.         }
  49.         return head;
  50.         
  51.     }
  52. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-8-2 08:54:09 | 只看该作者
全局:
300. Longest Increasing Subsequence

  1. class Solution {
  2.     //dp, one nested loop, from small to larget, keep updating the result
  3.     public int lengthOfLIS(int[] nums) {
  4.         if (nums == null || nums.length == 0) {
  5.             return 0;
  6.         }
  7.         int[] dp = new int[nums.length];
  8.         dp[0] = 1; // each element has length one
  9.         int ans = 1;
  10.         for (int i = 1; i < nums.length; i++) {
  11.             int cur = 0;
  12.             for (int j = 0; j < i; j++) {
  13.                 if (nums[i] > nums[j]) {
  14.                     cur = Math.max(cur, dp[j]);
  15.                 }
  16.             }
  17.             dp[i] = cur + 1; // count itself
  18.             ans = Math.max(dp[i], ans);
  19.         }
  20.         return ans;
  21.         
  22.     }
  23. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-8-8 08:40:23 | 只看该作者
全局:
443. String Compression

  1. class Solution {
  2.     //two pointer
  3.     public int compress(char[] chars) {
  4.         if(chars == null || chars.length == 0) {
  5.             return 0;
  6.         }
  7.         if (chars.length == 1) {
  8.             return 1;
  9.         }
  10.         char last = chars[0];
  11.         int lastPosition = 0;
  12.         int lastLen = 1;
  13.         for (int i = 1; i < chars.length; i++) {
  14.             char cur = chars[i];
  15.             if (cur == last) {
  16.                 lastLen++;
  17.             } else {
  18.                 last = cur;
  19.                 if (lastLen > 1) {
  20.                     Stack<Character> order = new Stack<>();
  21.                     while(lastLen > 0){
  22.                         order.push((char)('0' + lastLen % 10));
  23.                         lastLen /= 10;
  24.                     }
  25.                     while(!order.isEmpty()) {
  26.                         chars[++lastPosition] = order.pop();
  27.                     }
  28.                 }
  29.                 chars[++lastPosition] = last;
  30.                 lastLen = 1;
  31.             }
  32.         }
  33.         if (lastLen > 1) {
  34.             Stack<Character> order = new Stack<>();
  35.             while(lastLen > 0){
  36.                 order.push((char)('0' + lastLen % 10));
  37.                 lastLen /= 10;
  38.             }
  39.             while(!order.isEmpty()) {
  40.                 chars[++lastPosition] = order.pop();
  41.             }
  42.         }
  43.         return lastPosition + 1; // return new length
  44.     }
  45. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-8-12 02:40:58 | 只看该作者
全局:
28. Implement strStr() -- check out the office implementation of String class in JDK

  1. class Solution {
  2.     public int strStr(String haystack, String needle) {
  3.         if (needle == null || needle.length() == 0) { // easy question: how to handle conner case
  4.             return 0;
  5.         }
  6.         int needleLen = needle.length();
  7.         int haystackLen = haystack.length();
  8.         for (int i = 0; i < haystackLen; i++) {
  9.             inner:
  10.             for (int j = 0; j < needleLen; j++) {
  11.                 if ( i + j >= haystackLen) {
  12.                     return -1;
  13.                 }
  14.                 if (haystack.charAt(i + j) != needle.charAt(j)) {
  15.                     break inner;
  16.                 }
  17.                 if (j == needleLen - 1 && haystack.charAt(i + j) == needle.charAt(j)) {
  18.                     return i;
  19.                 }
  20.             }
  21.             
  22.         }
  23.         return -1;
  24.         
  25.     }
  26. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-8-12 08:59:51 | 只看该作者
全局:
361. Bomb Enemy

  1. class Solution {
  2.     //DP can't find the optimum dp solution
  3.     public int maxKilledEnemies(char[][] grid) {
  4.         if (grid == null || grid.length == 0) {
  5.             return 0;
  6.         }
  7.         int rowcount = 0;
  8.         int[] colCount = new int[grid[0].length];
  9.         int ans = 0;
  10.         for (int i = 0 ; i < grid.length; i++) {
  11.             for (int j = 0; j < grid[0].length; j++) {
  12.                 if (j == 0 || grid[i][j - 1] == 'W') {
  13.                     rowcount = 0;
  14.                     for (int k = j; k < grid[0].length && grid[i][k] != 'W'; k++) {
  15.                         rowcount = grid[i][k] == 'E' ? rowcount + 1 : rowcount + 0;
  16.                     }
  17.                 }
  18.                 if (i == 0 || grid[i - 1][j] == 'W') {
  19.                     colCount[j] = 0;
  20.                     for (int k = i; k < grid.length && grid[k][j] != 'W'; k++) {
  21.                         colCount[j] = grid[k][j] == 'E' ? colCount[j] + 1 : colCount[j] + 0;
  22.                     }
  23.                 }
  24.                 if (grid[i][j] == '0') { // bomb only target at empty place
  25.                     ans = Math.max(ans, rowcount + colCount[j]);
  26.                 }
  27.             }
  28.         }
  29.         return ans;
  30.     }
  31. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-8-12 09:34:27 | 只看该作者
全局:
325. Maximum Size Subarray Sum Equals k

  1. class Solution {
  2.     public int maxSubArrayLen(int[] nums, int k) {
  3.         if (nums == null || nums.length == 0) {
  4.             return 0;
  5.         }
  6.         Map<Integer, List<Integer>> map = new HashMap<>();
  7.         int[] sums = new int[nums.length];
  8.         List<Integer> init = new ArrayList<>();
  9.         init.add(-1);
  10.         map.put(0, init);
  11.         for (int i = 0; i < nums.length; i++) {
  12.             if (i == 0) {
  13.                 sums[i] = nums[i];
  14.             } else {
  15.                 sums[i] = sums[i - 1] + nums[i];
  16.             }
  17.             if (map.containsKey(sums[i])) {
  18.                 map.get(sums[i]).add(i);
  19.             }else {
  20.                 List<Integer> list = new ArrayList<>();
  21.                 list.add(i);
  22.                 map.put(sums[i], list);
  23.             }
  24.         }
  25.         int ans = 0;
  26.         for (int i = sums.length - 1; i >= 0; i--) {
  27.             if (map.containsKey(sums[i] - k)) {
  28.                 ans = Math.max(i - map.get(sums[i] - k).get(0), ans);
  29.             }
  30.         }
  31.         return ans;
  32.     }
  33. }
复制代码
回复

使用道具 举报

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

本版积分规则

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