diff --git a/README.md b/README.md index 7f7fa5da..cf129ccc 100644 --- a/README.md +++ b/README.md @@ -25,86 +25,88 @@ ## Easy -| # | Title | Tag | -| :--- | :---------------------------------------------------------------- | :------------------------------------------------ | -| 1 | [Two Sum][0001] | Array, Hash Table | -| 7 | [Reverse Integer][0007] | Math | -| 9 | [Palindrome Number][0009] | Math | -| 13 | [Roman to Integer][0013] | Math, String | -| 14 | [Longest Common Prefix][0014] | String | -| 16.11| [跳水板(Diving Board LCCI)][16_11] | 递归、记忆化 | -| 20 | [Valid Parentheses][0020] | Stack, String | -| 21 | [Merge Two Sorted Lists][0021] | Linked List | -| 26 | [Remove Duplicates from Sorted Array][0026] | Array, Two Pointers | -| 27 | [Remove Element][0027] | Array, Two Pointers | -| 28 | [Implement strStr()][0028] | Two Pointers, String | -| 35 | [Search Insert Position][0035] | String | -| 38 | [Count and Say][0038] | String | -| 53 | [Maximum Subarray][0053] | Array, Divide and Conquer, Dynamic Programming | -| 58 | [Length of Last Word][0058] | String | -| 66 | [Plus One][0066] | Array, Math | -| 67 | [Add Binary][0067] | Math, String | -| 69 | [Sqrt(x)][0069] | Binary Search, Math | -| 70 | [Climbing Stairs][0070] | Dynamic Programming | -| 83 | [Remove Duplicates from Sorted List][0083] | Linked List | -| 88 | [Merge Sorted Array][0088] | Array, Two Pointers | -| 100 | [Same Tree][0100] | Tree, Depth-first Search | -| 101 | [Symmetric Tree][0101] | Tree, Depth-first Search, Breadth-first Search | -| 104 | [Maximum Depth of Binary Tree][0104] | Tree, Depth-first Search | -| 107 | [Binary Tree Level Order Traversal II][107] | Tree, Breadth-first Search | -| 108 | [Convert Sorted Array to Binary Search Tree][0108] | Tree, Depth-first Search | -| 110 | [Balanced Binary Tree][0110] | Tree, Depth-first Search | -| 111 | [Minimum Depth of Binary Tree][0111] | Tree, Depth-first Search, Breadth-first Search | -| 112 | [Path Sum][0112] | Tree, Depth-first Search | -| 118 | [Pascal's Triangle][0118] | Array | -| 119 | [Pascal's Triangle II][0119] | Array | -| 121 | [Best Time to Buy and Sell Stock][0121] | Array, Dynamic Programmin | -| 122 | [Best Time to Buy and Sell Stock II][0122] | Array, Greedy | -| 543 | [Diameter of Binary Tree][0543] | Tree | +| # | Title | Tag | remar | +| :---- | :------------------------------------------------- | :--------------------------------------------- | ----- | +| 1 | [Two Sum][0001] | Array, Hash Table | | +| 7 | [Reverse Integer][0007] | Math | | +| 9 | [Palindrome Number][0009] | Math | | +| 13 | [Roman to Integer][0013] | Math, String | | +| 14 | [Longest Common Prefix][0014] | String | | +| 16.11 | [跳水板(Diving Board LCCI)][16_11] | 递归、记忆化 | | +| 20 | [Valid Parentheses][0020] | Stack, String | | +| 21 | [Merge Two Sorted Lists][0021] | Linked List | | +| 26 | [Remove Duplicates from Sorted Array][0026] | Array, Two Pointers | | +| 27 | [Remove Element][0027] | Array, Two Pointers | | +| 28 | [Implement strStr()][0028] | Two Pointers, String | | +| 35 | [Search Insert Position][0035] | String | | +| 38 | [Count and Say][0038] | String | | +| 53 | [Maximum Subarray][0053] | Array, Divide and Conquer, Dynamic Programming | | +| 58 | [Length of Last Word][0058] | String | | +| 66 | [Plus One][0066] | Array, Math | | +| 67 | [Add Binary][0067] | Math, String | | +| 69 | [Sqrt(x)][0069] | Binary Search, Math | | +| 70 | [Climbing Stairs][0070] | Dynamic Programming | | +| 83 | [Remove Duplicates from Sorted List][0083] | Linked List | | +| 88 | [Merge Sorted Array][0088] | Array, Two Pointers | | +| 100 | [Same Tree][0100] | Tree, Depth-first Search | | +| 101 | [Symmetric Tree][0101] | Tree, Depth-first Search, Breadth-first Search | | +| 104 | [Maximum Depth of Binary Tree][0104] | Tree, Depth-first Search | | +| 107 | [Binary Tree Level Order Traversal II][107] | Tree, Breadth-first Search | | +| 108 | [Convert Sorted Array to Binary Search Tree][0108] | Tree, Depth-first Search | | +| 110 | [Balanced Binary Tree][0110] | Tree, Depth-first Search | | +| 111 | [Minimum Depth of Binary Tree][0111] | Tree, Depth-first Search, Breadth-first Search | | +| 112 | [Path Sum][0112] | Tree, Depth-first Search | | +| 118 | [Pascal's Triangle][0118] | Array | | +| 119 | [Pascal's Triangle II][0119] | Array | | +| 121 | [Best Time to Buy and Sell Stock][0121] | Array, Dynamic Programmin | | +| 122 | [Best Time to Buy and Sell Stock II][0122] | Array, Greedy | | +| 543 | [Diameter of Binary Tree][0543] | Tree | | ## Medium -| # | Title | Tag | -| :--- | :---------------------------------------------------------------- | :------------------------------------------------ | -| 2 | [Add Two Numbers][0002] | Linked List, Math | -| 3 | [Longest Substring Without Repeating Characters][0003] | Hash Table, Two Pointers, String | -| 5 | [Longest Palindromic Substring][0005] | String, Dynamic Programming | -| 6 | [ZigZag Conversion][0006] | String | -| 8 | [String to Integer (atoi)][0008] | Math, String | -| 11 | [Container With Most Water][0011] | Array, Two Pointers | -| 12 | [Integer to Roman][0012] | Math, String | -| 15 | [3Sum][0015] | Array, Two Pointers | -| 15 | [3Sum Closest][0016] | Array, Two Pointers | -| 17 | [Letter Combinations of a Phone Number][0017] | String, Backtracking | -| 18 | [4Sum][0018] | Array, Hash Table, Two Pointers | -| 19 | [Remove Nth Node From End of List][0019] | Linked List, Two Pointers | -| 22 | [Generate Parentheses][0022] | String, Backtracking | -| 24 | [Swap Nodes in Pairs][0024] | Linked List | -| 29 | [Divide Two Integers][0029] | Math, Binary Search | -| 33 | [Search in Rotated Sorted Array][0033] | Arrays, Binary Search | -| 43 | [Multiply Strings][0043] | Math, String | -| 49 | [Group Anagrams][0049] | Hash Table, String | -| 50 | [Pow(x, n)][0050] | Math, Binary Search | -| 56 | [Merge Intervals][0056] | Array, Sort | -| 63 | [不同路径 II(Unique Paths II)][0063] | 数组、动态规划 | -| 209 | [长度最小的子数组(Minimum Size Subarray Sum)][0209] | 数组、双指针、二分查找 | -| 215 | [数组中的第K个最大元素(Kth Largest Element in an Array)][0215] | 堆、分治算法 | -| 554 | [Brick Wall][0554] | Hash Table | -| 1014 | [最佳观光组合(Best Sightseeing Pair)][1014] | 数组 | +| # | Title | Tag | +| :--- | :----------------------------------------------------------- | :------------------------------- | +| 2 | [Add Two Numbers][0002] | Linked List, Math | +| 3 | [Longest Substring Without Repeating Characters][0003] | Hash Table, Two Pointers, String | +| 5 | [Longest Palindromic Substring][0005] | String, Dynamic Programming | +| 6 | [ZigZag Conversion][0006] | String | +| 8 | [String to Integer (atoi)][0008] | Math, String | +| 11 | [Container With Most Water][0011] | Array, Two Pointers | +| 12 | [Integer to Roman][0012] | Math, String | +| 15 | [3Sum][0015] | Array, Two Pointers | +| 15 | [3Sum Closest][0016] | Array, Two Pointers | +| 17 | [Letter Combinations of a Phone Number][0017] | String, Backtracking | +| 18 | [4Sum][0018] | Array, Hash Table, Two Pointers | +| 19 | [Remove Nth Node From End of List][0019] | Linked List, Two Pointers | +| 22 | [Generate Parentheses][0022] | String, Backtracking | +| 24 | [Swap Nodes in Pairs][0024] | Linked List | +| 29 | [Divide Two Integers][0029] | Math, Binary Search | +| 33 | [Search in Rotated Sorted Array][0033] | Arrays, Binary Search | +| 43 | [Multiply Strings][0043] | Math, String | +| 49 | [Group Anagrams][0049] | Hash Table, String | +| 50 | [Pow(x, n)][0050] | Math, Binary Search | +| 56 | [Merge Intervals][0056] | Array, Sort | +| 63 | [不同路径 II(Unique Paths II)][0063] | 数组、动态规划 | +| 209 | [长度最小的子数组(Minimum Size Subarray Sum)][0209] | 数组、双指针、二分查找 | +| 215 | [数组中的第K个最大元素(Kth Largest Element in an Array)][0215] | 堆、分治算法 | +| 554 | [Brick Wall][0554] | Hash Table | +| 1014 | [最佳观光组合(Best Sightseeing Pair)][1014] | 数组 | +| 11 | [](https://leetcode-cn.com/problems/container-with-most-water/) | | ## Hard -| # | Title | Tag | -| :--- | :---------------------------------------------------------------- | :------------------------------------------------ | -| 4 | [Median of Two Sorted Arrays][0004] | Array, Binary Search, Divide and Conquer | -| 10 | [Regular Expression Matching][0010] | String, Dynamic Programming, Backtracking | -| 23 | [Merge k Sorted Lists][0023] | Linked List, Divide and Conquer, Heap | -| 25 | [Reverse Nodes in k-Group][0025] | Linked List | -| 30 | [Substring with Concatenation of All Words][0030] | Hash Table, Two Pointers, String | -| 44 | [Wildcard Matching][0044] | String, Dynamic Programming, Backtracking, Greedy | -| 57 | [Insert Interval][0057] | Array, Sort | -| 68 | [Text Justification][0068] | String | -| 1028 | [从先序遍历还原二叉树(Recover a Tree From Preorder Traversal)][1028] | 树、深度优先搜索 | +| # | Title | Tag | +| :--- | :----------------------------------------------------------- | :------------------------------------------------ | +| 4 | [Median of Two Sorted Arrays][0004] | Array, Binary Search, Divide and Conquer | +| 10 | [Regular Expression Matching][0010] | String, Dynamic Programming, Backtracking | +| 23 | [Merge k Sorted Lists][0023] | Linked List, Divide and Conquer, Heap | +| 25 | [Reverse Nodes in k-Group][0025] | Linked List | +| 30 | [Substring with Concatenation of All Words][0030] | Hash Table, Two Pointers, String | +| 44 | [Wildcard Matching][0044] | String, Dynamic Programming, Backtracking, Greedy | +| 57 | [Insert Interval][0057] | Array, Sort | +| 68 | [Text Justification][0068] | String | +| 1028 | [从先序遍历还原二叉树(Recover a Tree From Preorder Traversal)][1028] | 树、深度优先搜索 | +| 42 | [11. 盛最多水的容器 - 力扣(LeetCode) (leetcode-cn.com)](https://leetcode-cn.com/problems/container-with-most-water/) | array | ## 打个小广告 diff --git a/src/com/blankj/medium/_0011/ON/Slution.java b/src/com/blankj/medium/_0011/ON/Slution.java new file mode 100644 index 00000000..f2d2e81c --- /dev/null +++ b/src/com/blankj/medium/_0011/ON/Slution.java @@ -0,0 +1,25 @@ +package com.blankj.medium._011.ON; + +/** + * 暴力求解 + * + * @author weir + */ +@SuppressWarnings("ALL") +public class Slution { + public static void main(String[] args) { + Slution slution = new Slution(); + System.out.println("maxArea = " + slution.maxArea(new int[]{1, 1})); //1 + System.out.println("maxArea = " + slution.maxArea(new int[]{1,8,6,2,5,4,8,3,7})); /* 49 */ + } + + public int maxArea(int[] height) { + int maxArea = 0; + for (int i = 0, j = height.length - 1; i < j; ) { + int grap = j - i; + int area = height[i] < height[j] ? grap * height[i++] : grap * height[j--]; + maxArea = Math.max(area, maxArea); + } + return maxArea; + } +} diff --git a/src/com/blankj/medium/_0011/ON2/Slution.java b/src/com/blankj/medium/_0011/ON2/Slution.java new file mode 100644 index 00000000..a48c90e6 --- /dev/null +++ b/src/com/blankj/medium/_0011/ON2/Slution.java @@ -0,0 +1,31 @@ +package com.blankj.medium._0011.ON2; + +/** + * 暴力求解 + * @author weir + */ +public class Slution { + public static void main(String[] args) { + Slution slution = new Slution(); + int trap = slution.trap(new int[]{1,8,6,2,5,4,8,3,7}); + System.out.println("trap = " + trap); + } + + public int trap(int[] height) { + int maxArea = 0; + for (int i = 0; i < height.length - 1; i++) { + for (int j = i + 1; j < height.length; j++) { + //计算面积 + int area = calcArea(i, j, height); + maxArea = Math.max(maxArea, area); + } + } + return maxArea; + } + + private int calcArea(int i, int j, int[] height) { + int min = Math.min(height[i], height[j]); + return min * (j - i); + } + +} diff --git a/src/com/blankj/medium/_0011/Solution.java b/src/com/blankj/medium/_0011/Solution.java index 89ee60ca..4e6ec0f0 100644 --- a/src/com/blankj/medium/_0011/Solution.java +++ b/src/com/blankj/medium/_0011/Solution.java @@ -26,4 +26,4 @@ public static void main(String[] args) { System.out.println(solution.maxArea(new int[]{1, 2, 4, 3})); // 4 System.out.println(solution.maxArea(new int[]{1, 8, 6, 2, 5, 4, 8, 3, 7}));// 49 } -} \ No newline at end of file +}2 \ No newline at end of file