📣 VIP通行证夏日特惠 限时立减$68
楼主: AChris
跳转到指定楼层
上一主题 下一主题
收起左侧

刷题skill+编程cultivation+记录

🔗
 楼主| AChris 2019-4-17 02:39:41 | 只看该作者
全局:

04/16/2019 Day41
LeetCode 53. Maximum Subarray
Importantce【4】

解题思路:
利用动态规划。
res 记录到目前为止最大值。
dp[i] 记录结尾是nums[i]可以得到的最大值


  1. class Solution {
  2.     // method1:
  3.     public int maxSubArray(int[] nums) {
  4.         // time: O(n) 97.39%
  5.         // space: O(1) 90.84%
  6.         if (nums == null || nums.length == 0) return 0;
  7.         int maxIncludeCur = nums[0];
  8.         int res = nums[0];
  9.         
  10.         for (int i = 1; i < nums.length; i++) {
  11.             if (maxIncludeCur > 0) {
  12.                 maxIncludeCur += nums[i];
  13.             } else {
  14.                 maxIncludeCur = nums[i];
  15.             }
  16.             res = Math.max(maxIncludeCur, res);
  17.         }
  18.         return res;
  19.     }
  20.    
  21. //     // method2:
  22. //     public int maxSubArray(int[] nums) {
  23. //         // time: O(n) 97.39%
  24. //         // space: O(n) 91.14%
  25. //         if (nums == null || nums.length == 0) return 0;
  26. //         int[] dp = new int[nums.length];

  27. //         dp[0] = nums[0];
  28. //         int res = dp[0];
  29. //         for (int i = 1; i < nums.length; i++) {
  30. //             dp[i] = nums[i] + (dp[i - 1] > 0? dp[i - 1] : 0);
  31. //             res = Math.max(dp[i], res);
  32. //         }
  33. //         return res;
  34. //     }
  35. }
复制代码
回复

使用道具 举报

🔗
 楼主| AChris 2019-4-18 03:59:52 | 只看该作者
全局:


04/17/2019 Day42
LeetCode 67. Add Binary
Importantce【3】

解题思路:
从后往前依次相加。i 表示 a 的位数。j 表示b的位数。
sb 依次 append

  1. class Solution {
  2.     public String addBinary(String a, String b) {
  3.         // time: O(n) 99.93%
  4.         // space: O(n) 18.47%
  5.         StringBuilder sb = new StringBuilder();
  6.         int i = a.length() - 1;
  7.         int j = b.length() - 1;
  8.         int sum = 0;
  9.         
  10.         while (i >= 0 || j >= 0) {
  11.             if (i >= 0) sum += a.charAt(i--) - '0';
  12.             if (j >= 0) sum += b.charAt(j--) - '0';
  13.             sb.append(sum % 2);
  14.             sum = sum / 2;
  15.         }
  16.         
  17.         if (sum == 1) sb.append('1');
  18.         
  19.         return sb.reverse().toString();
  20.         
  21.     }
  22. }
复制代码
回复

使用道具 举报

🔗
 楼主| AChris 2019-4-20 03:56:21 | 只看该作者
全局:
04/19/2019 Day43
LeetCode 58. Length of the Last Word
Importantce【4】

解题思路:
trim后的长度 减去最后一个空格的index 再减一



  1. class Solution {
  2.     public int lengthOfLastWord(String s) {
  3.         // time: O(1) 100%
  4.         // space: O(1) 11.67%
  5.         return s.trim().length() - s.trim().lastIndexOf(" ") - 1;
  6.     }
  7. }
复制代码
回复

使用道具 举报

🔗
 楼主| AChris 2019-4-20 05:23:48 | 只看该作者
全局:
04/19/2019 Day43
LeetCode 80. Remove Duplicates from Sorted Array II
Importantce【4】

解题思路:
主要是count的时候,记住,count是即将要被赋值的位置,count - 2上不能重复的位置


class Solution {
    public int removeDuplicates(int[] nums) {
        // time: O(n) 97.40%
        // space: O(1) 94.46%
        
        // nums = [ 1, 1, 1, 2, 2, 3, 3]
        //                         c
        //                            i
        //        [ 1, 1, 2, 2, 3, 3, 3]
        
        if (nums == null) return 0;
        if (nums.length <= 2) return nums.length;
        int count = 2;
        for (int i = 2; i < nums.length; i++) {
            if (nums[i] != nums[count-2]) {
                nums[count++] = nums[i];
            }
        }
        return count;
    }
}
回复

使用道具 举报

🔗
 楼主| AChris 2019-4-28 02:21:44 | 只看该作者
全局:

04/27/2019 Day44
LeetCode 37. Sudoku Solver
Importantce【5】

解题思路:
DFS搜索,注意在什么时候return false


  1. class Solution {
  2.     public void solveSudoku(char[][] board) {
  3.         // time: unkown 43.48%
  4.         // space: unknown  100%
  5.         if (board == null || board.length == 0 || board[0] == null || board[0].length == 0) return;
  6.         solve(board);
  7.     }
  8.    
  9.     private boolean solve (char[][] board) {
  10.         for (int i = 0; i < 9; i++) {
  11.             for (int j = 0; j < 9; j++) {
  12.                 if (board[i][j] == '.') {
  13.                     for (char c = '1'; c <= '9'; c++) {
  14.                         if (isValid(board, c, i, j)) {
  15.                             board[i][j] = c;
  16.                             if (solve(board)) {
  17.                                 return true;
  18.                             } else {
  19.                                 board[i][j] = '.';
  20.                             }
  21.                         }
  22.                     }
  23.                     return false;
  24.                 }
  25.             }
  26.         }
  27.         return true;
  28.     }
  29.    
  30.     private boolean isValid(char[][] board, char c, int i, int j) {
  31.         for (int k = 0; k < 9; k++) {
  32.             if (board[i][k] == c) return false;
  33.             if (board[k][j] == c) return false;
  34.             if (board[i / 3 * 3 + k / 3][j / 3 * 3 + k % 3] == c) return false;
  35.         }
  36.         return true;
  37.     }
  38. }
复制代码
回复

使用道具 举报

🔗
 楼主| AChris 2019-4-28 05:13:45 | 只看该作者
全局:
04/27/2019 Day44
LeetCode 23. Merge k Sorted Lists
Importantce【5】

解题思路:
主要有两种方法。
第一种:divide and conquer
把整个问题化简成两个两个单独的list合并的问题

第二种:
利用 PriorityQueue,把每个list的头部的listnode加进去,然后每次都从queue中提取出最小的元素,并添加进去新的头部元素

  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. //     // method 1: Divide and Conquer
  11. //     // time: O(nlogk) n = number of elements, k = lists.length 94.47%
  12. //     // space: O(n) 24.60%
  13. //     public ListNode mergeKLists(ListNode[] lists) {
  14. //         if (lists == null || lists.length == 0) return null;
  15. //         return sort(lists, 0, lists.length - 1);
  16. //     }
  17.    
  18. //     private ListNode sort(ListNode[] lists, int low, int high) {
  19. //         // low <= high which can be proved by the mathematical induction
  20. //         if (low == high) return lists[low];
  21. //         int mid = low + (high - low) / 2;
  22. //         ListNode l1 = sort(lists, low, mid);
  23. //         ListNode l2 = sort(lists, mid + 1, high);
  24. //         return merge(l1, l2);
  25. //     }
  26.    
  27. //     private ListNode merge(ListNode l1, ListNode l2) {
  28. //         if (l1 == null) return l2;
  29. //         if (l2 == null) return l1;
  30. //         if (l1.val < l2.val) {
  31. //             l1.next = merge(l1.next, l2);
  32. //             return l1;
  33. //         }
  34. //         l2.next = merge(l1, l2.next);
  35. //         return l2;
  36. //     }
  37.    
  38.     // method 2: Priority Queue
  39.     // time: O(nlogk) n = number of elements, k = lists.length 40.52%
  40.     // space: O(n) 56.54%
  41.     public ListNode mergeKLists(ListNode[] lists) {
  42.         // boundary case
  43.         if (lists == null || lists.length == 0) return null;
  44.         
  45.         // initilization
  46.         PriorityQueue<ListNode> queue = new PriorityQueue<> (lists.length, (a, b) -> a.val - b.val);
  47.         for (ListNode list: lists) {
  48.             if (list != null){
  49.                 queue.add(list);
  50.             }
  51.         }
  52.         
  53.         // sort
  54.         ListNode dummy = new ListNode(-1);
  55.         ListNode cur = dummy;
  56.         while (!queue.isEmpty()) {
  57.             cur.next = queue.poll();
  58.             cur = cur.next;
  59.             if (cur.next != null) {
  60.                 queue.add(cur.next);
  61.             }
  62.         }
  63.         return dummy.next;
  64.     }
  65.    
  66. }
复制代码
回复

使用道具 举报

🔗
 楼主| AChris 2019-4-29 09:16:52 | 只看该作者
全局:

04/27/2019 Day45
LeetCode 41. First Missing Positive
Importantce【5】

解题思路:
主要是利用count sort中的思想,如果1、2、3、。。。n所有的数都有的话,
理想的输出list应该是
index 0, 1, 2, 3, 4, ... i
num. 1, 2, 3, 4, 5, ... i+1
然后通过swap,争取依次把所有的数都swap到对应的位置上。
最后再loop一遍,检查有没有不符合要求的数并输出


  1. class Solution {
  2.     public int firstMissingPositive(int[] nums) {
  3.         // time: O(n) 100%
  4.         // space: O(1) 89.70%
  5.         if (nums == null || nums.length == 0) return 1;
  6.         for (int i = 0; i < nums.length; i++) {
  7.             while (nums[i] >= 1 && nums[i] <= nums.length && nums[i] != nums[nums[i] - 1]) {
  8.                 int temp = nums[nums[i] - 1];
  9.                 nums[nums[i] - 1] = nums[i];
  10.                 nums[i] = temp;
  11.             }
  12.         }
  13.         for (int i = 0; i < nums.length; i++) {
  14.             if (nums[i] != i + 1) return i + 1;
  15.         }
  16.         return nums.length + 1;
  17.     }
  18. }
复制代码
回复

使用道具 举报

🔗
 楼主| AChris 2019-4-29 09:18:13 | 只看该作者
全局:
04/28/2019 Day45
LeetCode 25. Reverse Nodes in k-Group
Importantce【5】

解题思路:
递归


  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 reverseKGroup(ListNode head, int k) {
  11.         // time: O(n) 100%
  12.         // space: O(n) 95.02%
  13.         if (head == null || head.next == null) return head;
  14.         
  15.         int count = 0;
  16.         ListNode cur = head;
  17.         while (count != k && cur!= null) {
  18.             cur = cur.next;
  19.             count++;
  20.         }
  21.         /* k = 2 count = 0
  22.          1 -> 2 -> 3
  23.          h        cur
  24.          
  25.                     3
  26.                     ht
  27.          2  -> 1.---^
  28.          c   
  29.         */
  30.         if (count == k) {
  31.             cur = reverseKGroup(cur, k);
  32.             while (count-- > 0) {
  33.                 ListNode temp = head.next;
  34.                 head.next = cur;
  35.                 cur = head;
  36.                 head = temp;
  37.             }
  38.             head = cur;
  39.         }
  40.         return head;
  41.     }
  42. }
复制代码
回复

使用道具 举报

🔗
 楼主| AChris 2019-4-29 10:18:15 | 只看该作者
全局:

04/28/2019 Day45
LeetCode 30. Substring with Concatenation of All Words
Importantce【5】

解题思路:
主要是建立一个map, map里面对应的是words中的string和所需要的个数。
然后从s的每一个位置开始遍历,看是否能将所有的words的单词找到对应,如果可以找到的话,就将这个加进结果。


  1. class Solution {
  2.     public List<Integer> findSubstring(String s, String[] words) {
  3.         // time: O(n^2) 57.49%
  4.         // space: O(n) 39.37%
  5.         List<Integer> res = new ArrayList<> ();
  6.         if (s == null || s.length() == 0 || words == null || words.length == 0) return res;
  7.         
  8.         // initilization
  9.         int nWords = words.length;
  10.         int wordLength = words[0].length();
  11.         HashMap<String, Integer> map = new HashMap<> ();
  12.         for (String word: words) {
  13.             map.put(word, map.getOrDefault(word, 0) + 1);   
  14.         }
  15.         
  16.         //search
  17.         for (int i = 0; i <= s.length() - nWords * wordLength; i++) {
  18.             HashMap<String, Integer> copy = new HashMap<> (map);
  19.             int left = nWords;
  20.             int j = i;
  21.             while (left > 0) {
  22.                 String curWord = s.substring(j, j + wordLength);
  23.                 if (!copy.containsKey(curWord) || copy.get(curWord) == 0) break;
  24.                 copy.put(curWord, copy.get(curWord) - 1);
  25.                 j += wordLength;
  26.                 left--;
  27.             }
  28.             if (left == 0) res.add(i);
  29.         }
  30.         return res;
  31.     }
  32. }
复制代码
回复

使用道具 举报

🔗
 楼主| AChris 2019-4-29 10:42:08 | 只看该作者
全局:
04/28/2019 Day45
LeetCode 32. Longest Valid Parentheses
Importantce【5】

解题思路:
主要是利用stack,不停的把存在的(的位置push进stack里。然后看见)的话,在pop。
然后主要考虑)()()和 (())两种情况。

  1. class Solution {
  2.     public int longestValidParentheses(String s) {
  3.         // time: O(n)
  4.         // space: O(n)
  5.         int res = 0;
  6.         if (s == null || s.length() == 0) return res;
  7.         
  8.         Stack<Integer> stack = new Stack<> ();
  9.         int start = -1;
  10.         // we will use (start, i] to represent the valid substring
  11.         for (int i  = 0; i < s.length(); i++) {
  12.             if (s.charAt(i) == '(') {
  13.                 stack.push(i);
  14.             } else {
  15.                 if (stack.isEmpty()) {
  16.                     start = i;
  17.                 } else {
  18.                     stack.pop();
  19.                     if (stack.isEmpty()) {
  20.                         res = Math.max(res, i - start);
  21.                     } else {
  22.                         res = Math.max(res, i - stack.peek());
  23.                     }
  24.                 }
  25.             }
  26.         }
  27.         return res;
  28.     }
  29. }
复制代码
回复

使用道具 举报

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

本版积分规则

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