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

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

🔗
 楼主| zhangrz2 2019-8-13 09:28:39 | 只看该作者
全局:
529. Minesweeper

  1. class Solution {
  2.     //BFS
  3.     private int[][] directions = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1,- 1}, {-1, 1}};
  4.     public char[][] updateBoard(char[][] board, int[] click) {
  5.         if (board == null || board.length == 0 || click == null || click.length == 0) {
  6.             return board;
  7.         }
  8.         Queue<int[]> queue = new LinkedList<>();
  9.         queue.offer(click);
  10.         while(!queue.isEmpty()) {
  11.             int[] curPos = queue.poll();
  12.             int curRow = curPos[0];
  13.             int curCol = curPos[1];
  14.             if (board[curRow][curCol] == 'M') {
  15.                 board[curRow][curCol] = 'X';
  16.                 break;
  17.             }else if (board[curRow][curCol] == 'E'){
  18.                 int numOfMine = getNumOfMineAround(board, curRow, curCol);
  19.                 if (numOfMine == 0) {
  20.                     board[curRow][curCol] = 'B';
  21.                     for (int[] direction : directions) {
  22.                         int nextRow = curRow + direction[0];
  23.                         int nextCol = curCol + direction[1];
  24.                         if (isPositionValid(board, nextRow, nextCol) && board[nextRow][nextCol] == 'E') {
  25.                             queue.offer(new int[]{nextRow, nextCol});
  26.                         }
  27.                     }
  28.                 } else {
  29.                     board[curRow][curCol] = (char) ('0' + numOfMine);
  30.                 }
  31.             }
  32.         }
  33.         return board;
  34.         
  35.     }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-8-13 09:29:02 | 只看该作者
全局:
430. Flatten a Multilevel Doubly Linked List

  1. class Solution {
  2.     public Node flatten(Node head) {
  3.         if (head == null)
  4.         return head;
  5.         Node dump = head;
  6.         while(dump != null) {
  7.             //has child nor not
  8.             if (dump.child != null) {
  9.                 Node oriNext = dump.next;
  10.                 Node dumpChild = dump.child;
  11.                 dump.child = null;
  12.                 dumpChild.prev = dump;
  13.                 dump.next = dumpChild;
  14.                 while(dumpChild.next != null) {
  15.                     dumpChild = dumpChild.next;
  16.                 }
  17.                 dumpChild.next = oriNext;
  18.                 if (oriNext != null) {
  19.                     oriNext.prev = dumpChild;
  20.                 }
  21.             }else {
  22.                 dump = dump.next;
  23.                 continue;
  24.             }
  25.         }
  26.         return head;
  27.     }
  28.    
  29.    
  30. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-8-13 11:07:47 | 只看该作者
全局:
409. Longest Palindrome

  1. class Solution {
  2.     //solution has more elegant code
  3.     public int longestPalindrome(String s) {
  4.         if (s == null || s.length() == 0) {
  5.             return 0;
  6.         }
  7.         char[] array = s.toCharArray();
  8.         int[] count = new int[128];
  9.         for (char c : array) {
  10.             count[c - 'A']++;
  11.         }
  12.         int ans = 0;
  13.         for (int num : count) {
  14.             ans += (num / 2) * 2;
  15.             if (ans % 2 == 0 && num % 2 == 1) {
  16.                 ans++;
  17.             }
  18.         }
  19.         return ans;
  20.     }
  21. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-8-15 07:57:29 | 只看该作者
全局:
1046. Last Stone Weight

  1. class Solution {
  2.     //max queue
  3.     public int lastStoneWeight(int[] stones) {
  4.         PriorityQueue<Integer> q = new PriorityQueue<>(Comparator.reverseOrder());
  5.         for (int st : stones) { q.offer(st); }
  6.         while (q.size() > 1) {
  7.             q.offer(q.poll() - q.poll());
  8.         }
  9.         return q.peek();
  10.     }
  11. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-8-15 08:36:52 | 只看该作者
全局:
127. Word Ladder

  1. class Solution {
  2.     //did this problem before, one end BFS & two end BFS
  3.     public int ladderLength(String beginWord, String endWord, List<String> wordList) {
  4.         if (beginWord == null || endWord == null || beginWord.length() == 0 || endWord.length() == 0 || wordList == null) {
  5.             return 0;
  6.         }
  7.         Set<String> wordSet = new HashSet<>(wordList);
  8.         int wordLen = beginWord.length();
  9.         int ans = 1;
  10.         Queue<String> queue = new LinkedList<>();
  11.         queue.offer(beginWord);
  12.         StringBuilder sb = new StringBuilder();
  13.         while(!queue.isEmpty()) {
  14.             int size = queue.size();
  15.             for (int k = 0; k < size; k++) {
  16.                 String cur = queue.poll();
  17.                 if (cur.equals(endWord)) {
  18.                     return ans;
  19.                 }
  20.                 for (int i = 0; i < wordLen; i++) {
  21.                     sb.append(cur);
  22.                     for (char c = 'a'; c <= 'z'; c++) {
  23.                         if (c != cur.charAt(i)) {
  24.                             sb.setCharAt(i, c);
  25.                             if (wordSet.contains(sb.toString())) {
  26.                                 queue.offer(sb.toString());
  27.                                 wordSet.remove(sb.toString());
  28.                             }
  29.                         }
  30.                     }
  31.                     sb.setLength(0); //clear builder each time
  32.                 }
  33.             }
  34.             ans++;
  35.         }
  36.         return 0;f
  37.     }
  38. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-8-18 05:04:34 | 只看该作者
全局:
1086. High Five

  1. class Solution {
  2.     //using my idea, hashMap with List
  3.     public int[][] highFive(int[][] items) {
  4.         if (items == null || items.length == 0) {
  5.             return new int[0][0];
  6.         }
  7.         Map<Integer, List<Integer>> map = new HashMap<>();
  8.         for (int[] item : items) {
  9.             List<Integer> entry = map.getOrDefault(item[0], new ArrayList<Integer>());
  10.             entry.add(item[1]);
  11.             map.put(item[0], entry);
  12.         }
  13.         int[][] ans = new int[map.size()][2];
  14.         int index = 0;
  15.         for (Map.Entry<Integer, List<Integer>> entry : map.entrySet()) {
  16.             int ave = getAverage(entry.getValue());
  17.             ans[index][0] = entry.getKey();
  18.             ans[index][1] = ave;
  19.             index++;
  20.         }
  21.         Arrays.sort(ans, (a, b) -> a[0] - b[0]);
  22.         return ans;
  23.     }
  24.    
  25.     private int getAverage(List<Integer> list) {
  26.         Collections.sort(list, Comparator.reverseOrder());
  27.         int size = Math.min(5, list.size());
  28.         int sum = 0;
  29.         for (int i = 0; i < size; i++) {
  30.             sum += list.get(i);
  31.         }
  32.         return sum / size;

  33.     }
  34. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-8-18 10:10:22 | 只看该作者
全局:
388. Longest Absolute File Path

  1. class Solution {
  2.     public int lengthLongestPath(String input) {
  3.         if (input == null || input.length() == 0) {
  4.             return 0;
  5.         }
  6.         String[] tokens = input.split("\n");
  7.         Stack<Integer> stack = new Stack<>();
  8.         int ans = 0;
  9.         int curLen = 0;
  10.         for (String str : tokens) {
  11.             int curLevel = getCurLevel(str);
  12.             while(stack.size() > curLevel) {
  13.                 curLen -= stack.pop(); // past depth deeper than current
  14.             }
  15.             int len = str.replaceAll("\t", "").length() + 1; // plus one for '\'
  16.             curLen += len;
  17.             if (str.contains(".")) { // we found a file
  18.                 ans = curLen - 1 > ans ? curLen - 1 : ans;
  19.             }
  20.             stack.push(len);
  21.         }
  22.         return ans;
  23.     }
  24.    
  25.     private int getCurLevel(String str) {
  26.         String s = str.replaceAll("\t", "");
  27.         return str.length() - s.length();
  28.     }
  29. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-8-23 08:33:31 | 只看该作者
全局:
13. Roman to Integer

  1. class Solution {
  2.     private Map<Character, Integer> symbols = new HashMap<>();
  3.     public int romanToInt(String s) {
  4.         if (s == null || s.length() == 0) {
  5.             return 0;
  6.         }
  7.         symbols.put('I', 1);
  8.         symbols.put('V', 5);
  9.         symbols.put('X', 10);
  10.         symbols.put('L', 50);
  11.         symbols.put('C', 100);
  12.         symbols.put('D', 500);
  13.         symbols.put('M', 1000);
  14.         int ans = 0;
  15.         String newS = s.toUpperCase();
  16.         for (int i = 0; i < newS.length(); i++) {
  17.             if (i + 1 < newS.length()) {
  18.                 if (symbols.get(newS.charAt(i)) < symbols.get(newS.charAt(i + 1))) {
  19.                     ans -= symbols.get(newS.charAt(i));
  20.                 }else {
  21.                     ans += symbols.get(newS.charAt(i));
  22.                 }
  23.             }else {
  24.                 ans += symbols.get(newS.charAt(i));
  25.             }
  26.         }
  27.         return ans;
  28.     }
  29. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-8-23 10:52:03 | 只看该作者
全局:
179. Largest Number

  1. class Solution {
  2.     //Did before, but I forgot
  3.     public String largestNumber(int[] nums) {
  4.         if (nums == null || nums.length == 0) {
  5.             return "";
  6.         }
  7.         String[] array = new String[nums.length];
  8.         for (int i = 0; i < nums.length; i++) {
  9.             //convert to string array
  10.             array[i] = String.valueOf(nums[i]);
  11.         }
  12.         Arrays.sort(array, (a, b) -> (b + a).compareTo(a + b)); // triky part
  13.         if (array[0].equals("0")) {
  14.             return "0"; //conner case
  15.         }
  16.         String ans = "";
  17.         for (String str : array) {
  18.             ans += str;
  19.         }
  20.         return ans;
  21.     }
  22. }
复制代码
回复

使用道具 举报

🔗
 楼主| zhangrz2 2019-8-26 05:39:57 | 只看该作者
全局:
442. Find All Duplicates in an Array

  1. class Solution {
  2.     //Since some elements only appeared twice
  3.     //so just flip the sign
  4.     //stupid mistake for first submission
  5.     public List<Integer> findDuplicates(int[] nums) {
  6.         List<Integer> ans = new ArrayList<>();
  7.         if (nums == null || nums.length == 0) {
  8.             return ans;
  9.         }
  10.         for (int i = 0; i < nums.length; i++) {
  11.             int index = Math.abs(nums[i]) - 1;
  12.             if (nums[index] < 0) {
  13.                 ans.add(index + 1);
  14.             } else {
  15.                 nums[index] = -nums[index];
  16.             }
  17.         }
  18.         return ans;
  19.     }
  20. }
复制代码
回复

使用道具 举报

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

本版积分规则

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