Skip to content

Commit 873b8f0

Browse files
committed
init markdown dir
1 parent 1b9e5ee commit 873b8f0

40 files changed

+2126
-8
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
### [11\. Container With Most Water](https://leetcode.com/problems/container-with-most-water/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Given _n_ non-negative integers _a<sub style="display: inline;">1</sub>_, _a<sub style="display: inline;">2</sub>_, ..., _a<sub style="display: inline;">n </sub>_, where each represents a point at coordinate (_i_, _a<sub style="display: inline;">i</sub>_). _n_ vertical lines are drawn such that the two endpoints of line _i_ is at (_i_, _a<sub style="display: inline;">i</sub>_) and (_i_, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
7+
8+
**Note: **You may not slant the container and _n_ is at least 2.
9+
10+
![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg)
11+
12+
<small style="display: inline;">The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49\.</small>
13+
14+
**Example:**
15+
16+
```
17+
Input: [1,8,6,2,5,4,8,3,7]
18+
Output: 49```
19+
20+
21+
#### Solution
22+
23+
Language: **Java**
24+
25+
```java
26+
class Solution {
27+
   public int maxArea(int[] height) {
28+
       int i = 0;
29+
       int j = height.length - 1;
30+
       int water = 0;
31+
       while (i < j) {
32+
           water = Math.max((j - i) * Math.min(height[i], height[j]), water);
33+
           if (height[i] < height[j]) {
34+
               i++;
35+
          } else {
36+
               j--;
37+
          }
38+
      }
39+
       return water;
40+
  }
41+
}
42+
```

markdown/12. Integer to Roman.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
### [12\. Integer to Roman](https://leetcode.com/problems/integer-to-roman/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D` and `M`.
7+
8+
```
9+
Symbol Value
10+
I 1
11+
V 5
12+
X 10
13+
L 50
14+
C 100
15+
D 500
16+
M 1000```
17+
18+
For example, two is written as `II` in Roman numeral, just two one's added together. Twelve is written as, `XII`, which is simply `X` + `II`. The number twenty seven is written as `XXVII`, which is `XX` + `V` + `II`.
19+
20+
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not `IIII`. Instead, the number four is written as `IV`. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as `IX`. There are six instances where subtraction is used:
21+
22+
* `I` can be placed before `V` (5) and `X` (10) to make 4 and 9. 
23+
* `X` can be placed before `L` (50) and `C` (100) to make 40 and 90. 
24+
* `C` can be placed before `D` (500) and `M` (1000) to make 400 and 900.
25+
26+
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.
27+
28+
**Example 1:**
29+
30+
```
31+
Input: 3
32+
Output: "III"```
33+
34+
**Example 2:**
35+
36+
```
37+
Input: 4
38+
Output: "IV"```
39+
40+
**Example 3:**
41+
42+
```
43+
Input: 9
44+
Output: "IX"```
45+
46+
**Example 4:**
47+
48+
```
49+
Input: 58
50+
Output: "LVIII"
51+
Explanation: L = 50, V = 5, III = 3.
52+
```
53+
54+
**Example 5:**
55+
56+
```
57+
Input: 1994
58+
Output: "MCMXCIV"
59+
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.```
60+
61+
62+
#### Solution
63+
64+
Language: **Java**
65+
66+
```java
67+
class Solution {
68+
   public String intToRoman(int num) {
69+
       String M[] = {"", "M", "MM", "MMM"};
70+
       String C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
71+
       String X[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
72+
       String I[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
73+
       return M[num/1000] + C[(num%1000)/100] + X[(num%100)/10] + I[num%10];
74+
  }
75+
}
76+
```

markdown/13. Roman to Integer.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
### [13\. Roman to Integer](https://leetcode.com/problems/roman-to-integer/)
2+
3+
Difficulty: **Easy**
4+
5+
6+
Roman numerals are represented by seven different symbols: `I`, `V`, `X`, `L`, `C`, `D` and `M`.
7+
8+
```
9+
Symbol Value
10+
I 1
11+
V 5
12+
X 10
13+
L 50
14+
C 100
15+
D 500
16+
M 1000```
17+
18+
For example, two is written as `II` in Roman numeral, just two one's added together. Twelve is written as, `XII`, which is simply `X` + `II`. The number twenty seven is written as `XXVII`, which is `XX` + `V` + `II`.
19+
20+
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not `IIII`. Instead, the number four is written as `IV`. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as `IX`. There are six instances where subtraction is used:
21+
22+
* `I` can be placed before `V` (5) and `X` (10) to make 4 and 9. 
23+
* `X` can be placed before `L` (50) and `C` (100) to make 40 and 90. 
24+
* `C` can be placed before `D` (500) and `M` (1000) to make 400 and 900.
25+
26+
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
27+
28+
**Example 1:**
29+
30+
```
31+
Input: "III"
32+
Output: 3```
33+
34+
**Example 2:**
35+
36+
```
37+
Input: "IV"
38+
Output: 4```
39+
40+
**Example 3:**
41+
42+
```
43+
Input: "IX"
44+
Output: 9```
45+
46+
**Example 4:**
47+
48+
```
49+
Input: "LVIII"
50+
Output: 58
51+
Explanation: L = 50, V= 5, III = 3.
52+
```
53+
54+
**Example 5:**
55+
56+
```
57+
Input: "MCMXCIV"
58+
Output: 1994
59+
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.```
60+
61+
62+
#### Solution
63+
64+
Language: **Java**
65+
66+
```java
67+
class Solution {
68+
   public int romanToInt(String s) {
69+
       int result = 0;
70+
       for (int i = 0; i < s.length(); i++) {
71+
           int cur = getInt(s.charAt(i));
72+
           if (i + 1 < s.length() && getInt(s.charAt(i + 1)) > getInt(s.charAt(i))) {
73+
               result -= cur;
74+
          } else {
75+
               result += cur;
76+
          }
77+
      }
78+
       return result;
79+
  }
80+
81+
   private int getInt(char ch) {
82+
       switch (ch) {
83+
           case 'I':
84+
               return 1;
85+
           case 'V':
86+
               return 5;
87+
           case 'X':
88+
               return 10;
89+
           case 'L':
90+
               return 50;
91+
           case 'C':
92+
               return 100;
93+
           case 'D':
94+
               return 500;
95+
           case 'M':
96+
               return 1000;
97+
           default:
98+
               return 0;
99+
      }
100+
  }
101+
}
102+
```

markdown/14. Longest Common Prefix.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
### [14\. Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/)
2+
3+
Difficulty: **Easy**
4+
5+
6+
Write a function to find the longest common prefix string amongst an array of strings.
7+
8+
If there is no common prefix, return an empty string `""`.
9+
10+
**Example 1:**
11+
12+
```
13+
Input: ["flower","flow","flight"]
14+
Output: "fl"
15+
```
16+
17+
**Example 2:**
18+
19+
```
20+
Input: ["dog","racecar","car"]
21+
Output: ""
22+
Explanation: There is no common prefix among the input strings.
23+
```
24+
25+
**Note:**
26+
27+
All given inputs are in lowercase letters `a-z`.
28+
29+
30+
#### Solution
31+
32+
Language: **Java**
33+
34+
```java
35+
class Solution {
36+
   public String longestCommonPrefix(String[] strs) {
37+
       if (strs.length == 0) {
38+
           return "";
39+
      }
40+
41+
       if (strs.length == 1) {
42+
           return strs[0];
43+
      }
44+
       int minLenth = strs[0].length();
45+
46+
       for (String str : strs) {
47+
           if (minLenth > str.length()) {
48+
               minLenth = str.length();
49+
          }
50+
      }
51+
52+
       for (int longPrefixIndex = 0; longPrefixIndex < minLenth; longPrefixIndex++) {
53+
           for (int j = 0; j < strs.length - 1; j++) {
54+
               if (strs[j].charAt(longPrefixIndex) != strs[j + 1].charAt(longPrefixIndex)) {
55+
                   return strs[0].substring(0, longPrefixIndex);
56+
              }
57+
          }
58+
      }
59+
       return "";
60+
  }
61+
}
62+
```

markdown/15. 3Sum.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
### [15\. 3Sum](https://leetcode.com/problems/3sum/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Given an array `nums` of _n_ integers, are there elements _a_, _b_, _c_ in `nums` such that _a_ + _b_ + _c_ = 0? Find all unique triplets in the array which gives the sum of zero.
7+
8+
**Note:**
9+
10+
The solution set must not contain duplicate triplets.
11+
12+
**Example:**
13+
14+
```
15+
Given array nums = [-1, 0, 1, 2, -1, -4],
16+
17+
A solution set is:
18+
[
19+
[-1, 0, 1],
20+
[-1, -1, 2]
21+
]
22+
```
23+
24+
25+
#### Solution
26+
27+
Language: **Java**
28+
29+
```java
30+
class Solution {
31+
   public List<List<Integer>> threeSum(int[] nums) {
32+
       if (nums.length < 3) {
33+
           return Collections.emptyList();
34+
      }
35+
36+
       Arrays.sort(nums); // 排序用于剔除重复项
37+
       Set<List<Integer>> resultSet = new HashSet<>();
38+
       Map<Integer, List<Integer>> index = new HashMap<>();
39+
       // List<Integer> numList = new ArrayList<>();
40+
       for (int i = 0; i < nums.length; i++) {
41+
           index.computeIfAbsent(nums[i], k -> new ArrayList<>());
42+
           index.get(nums[i]).add(i);
43+
      }
44+
45+
       for (int i = 0; i < nums.length; i++) {
46+
           if (nums[i] > 0) {
47+
               continue;
48+
          }
49+
           if (i > 0 && nums[i] == nums[i - 1]) {
50+
               continue;
51+
          }
52+
53+
           for (int j = i + 1; j < nums.length; j++) {
54+
               int sum = nums[i] + nums[j];
55+
               if (-sum < nums[j] || -sum > nums[nums.length - 1]) {
56+
                   continue;
57+
              }
58+
               List<Integer> integers = Arrays.asList(nums[i], nums[j], -sum);
59+
               if (resultSet.contains(integers)) {
60+
                   continue;
61+
              }
62+
               if (index.get(-sum) != null) {
63+
                   for (Integer integer : index.get(-sum)) {
64+
                       if (integer > j) {
65+
                           resultSet.add(integers);
66+
                      }
67+
                  }
68+
              }
69+
          }
70+
      }
71+
       return new ArrayList<>(resultSet);
72+
  }
73+
}
74+
75+
```

0 commit comments

Comments
 (0)