Skip to content

Commit 7cb1fe2

Browse files
authored
Merge branch 'master' into master
2 parents 80f6bef + d5129cb commit 7cb1fe2

File tree

14 files changed

+230
-29
lines changed

14 files changed

+230
-29
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,6 @@ I'm looking for long-term contributors/partners to this repo! Send me [PRs](http
130130
| <center> [<img src="https://avatars3.githubusercontent.com/u/21008209?v=4" width="80px;"/>](https://github.com/yanglbme) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/23625436?v=4" width="80px;"/>](https://github.com/chakyam) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/10081554?v=4" width="80px;"/>](https://github.com/zhkmxx9302013) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/40383345?v=4" width="80px;"/>](https://github.com/MarkKuang1991) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/12371194?v=4" width="80px;"/>](https://github.com/fonxian) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/25222367?v=4" width="80px;"/>](https://github.com/zhanary) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/42396616?v=4" width="80px;"/>](https://github.com/ZhouTingZhaobiu) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/31923541?v=4" width="80px;"/>](https://github.com/zouwx2cs) </center> |
131131
|---|---|---|---|---|---|---|---|
132132
| <center> [<img src="https://avatars3.githubusercontent.com/u/20679510?v=4" width="80px;"/>](https://github.com/Mrzhudky) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/44309823?v=4" width="80px;"/>](https://github.com/KongJHong) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/18181519?v=4" width="80px;"/>](https://github.com/limbowandering) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/37685012?v=4" width="80px;"/>](https://github.com/jxdeng3989) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/44314231?v=4" width="80px;"/>](https://github.com/igayhub) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/30177307?v=4" width="80px;"/>](https://github.com/MCN1998) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/5793058?v=4" width="80px;"/>](https://github.com/Fairyhead) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/24841082?v=4" width="80px;"/>](https://github.com/zhng1456) </center> |
133-
| <center> [<img src="https://avatars3.githubusercontent.com/u/32598987?v=4" width="80px;"/>](https://github.com/xiapengchng) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/37660444?v=4" width="80px;"/>](https://github.com/Mcnwork2018) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/22535595?v=4" width="80px;"/>](https://github.com/bluesword12350) </center> |
133+
| <center> [<img src="https://avatars3.githubusercontent.com/u/32598987?v=4" width="80px;"/>](https://github.com/xiapengchng) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/37660444?v=4" width="80px;"/>](https://github.com/Mcnwork2018) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/22535595?v=4" width="80px;"/>](https://github.com/bluesword12350) </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/39827514?v=4" width="80px;"/>](https://github.com/ashwek) </center> |
134134

135135
<!-- ALL-CONTRIBUTORS-LIST:END -->
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class Solution:
2+
def isPalindrome(self, num):
3+
"""
4+
:type num: int
5+
:rtype: bool
6+
"""
7+
if num < 0:
8+
return False
9+
10+
return str(num) == str(num)[::-1]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
static int x=[](){
2+
std::ios::sync_with_stdio(false);
3+
cin.tie(NULL);
4+
return 0;
5+
}();
6+
string Compare(string s1,string s2)
7+
{
8+
if(s1.size()==0||s2.size()==0)
9+
return "";
10+
int num=s1.size()<s2.size()?s1.size():s2.size();
11+
string s;
12+
for(int i=0;i<num;i++)
13+
{
14+
if(s1[i]==s2[i])
15+
{
16+
s.push_back(s1[i]);
17+
}
18+
else
19+
break;
20+
21+
}
22+
return s;
23+
}
24+
class Solution {
25+
public:
26+
string longestCommonPrefix(vector<string>& strs) {
27+
if(strs.size()==0)
28+
return "";
29+
string prefix=strs[0];
30+
for(int i=1;i<strs.size();i++)
31+
{
32+
prefix=Compare(prefix,strs[i]);
33+
}
34+
return prefix;
35+
36+
}
37+
};
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int threeSumClosest(int[] nums, int target) {
3+
int result = nums[0]+nums[1]+nums[2];
4+
Arrays.sort(nums);
5+
for(int i = 0;i<nums.length-2;i++){
6+
int start = i+1,end=nums.length-1;
7+
while(start<end){
8+
int cache = nums[i]+nums[start]+nums[end];
9+
if(Math.abs(cache-target)<Math.abs(result-target)) result = cache;
10+
if(cache < target ) start++;
11+
else if(cache > target) end--;
12+
else return result;
13+
}
14+
}
15+
return result;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public List<String> letterCombinations(String digits) {
3+
char[] cs = digits.toCharArray();
4+
List<String> result = new ArrayList<>();
5+
for (char a : cs) {
6+
char[] charArray;
7+
switch (a) {
8+
case '2': charArray = new char[]{'a','b','c'}; break;
9+
case '3': charArray = new char[]{'d','e','f'}; break;
10+
case '4': charArray = new char[]{'g','h','i'}; break;
11+
case '5': charArray = new char[]{'j','k','l'}; break;
12+
case '6': charArray = new char[]{'m','n','o'}; break;
13+
case '7': charArray = new char[]{'p','q','r','s'}; break;
14+
case '8': charArray = new char[]{'t','u','v'}; break;
15+
case '9': charArray = new char[]{'w','x','y','z'}; break;
16+
default: return null;
17+
}
18+
if (result.size() == 0) {
19+
for (char aCharArray : charArray) result.add(String.valueOf(aCharArray));
20+
} else {
21+
List<String> cache = new ArrayList<>();
22+
for (String string : result) {
23+
for (char aCharArray : charArray) cache.add(string + aCharArray);
24+
}
25+
result = cache;
26+
}
27+
}
28+
return result;
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
12+
ListNode *a1=new ListNode(0);
13+
ListNode *head=a1;
14+
while(l1!=NULL&&l2!=NULL)
15+
{
16+
if(l1->val<l2->val)
17+
{
18+
a1->next=l1;
19+
l1=l1->next;
20+
a1=a1->next;
21+
}
22+
else
23+
{
24+
a1->next=l2;
25+
l2=l2->next;
26+
a1=a1->next;
27+
}
28+
}
29+
if(l1==NULL)
30+
a1->next=l2;
31+
if(l2==NULL)
32+
a1->next=l1;
33+
return head->next;
34+
35+
36+
}
37+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const maxSubArray = function(nums){
2+
if(nums.length === 0) return 0;
3+
let ans = nums[0], tmp = nums[0];
4+
for(let i = 1; i < nums.length; i++){
5+
tmp = Math.max(tmp+nums[i], nums[i]);
6+
ans = Math.max(ans,tmp);
7+
}
8+
return ans;
9+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
int uniquePaths(int m, int n) {
4+
if(m<=0||n<=0)
5+
return 0;
6+
if(m==1||n==1)
7+
return 1;
8+
int arr[m][n]={0};
9+
arr[0][0]=1;
10+
for(int i=1;i<m;i++)
11+
arr[i][0]=1;
12+
for(int j=1;j<n;j++)
13+
arr[0][j]=1;
14+
for(int i=1;i<m;i++)
15+
for(int j=1;j<n;j++)
16+
{
17+
arr[i][j]=arr[i-1][j]+arr[i][j-1];
18+
}
19+
return arr[m-1][n-1];
20+
}
21+
};
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int climbStairs(int n) {
4+
5+
if(n==1)
6+
return 1;
7+
if(n==2)
8+
return 2;
9+
int arr[n]={0};
10+
arr[0]=1;
11+
arr[1]=2;
12+
for(int i=2;i<n;i++)
13+
arr[i]=arr[i-1]+arr[i-2];
14+
return arr[n-1];
15+
16+
}
17+
};
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const climbStairs = function(n){
2+
let arr = [];
3+
arr[0] = 1;
4+
arr[1] = 1;
5+
for(let i = 2; i <= n; i++){
6+
arr[i] = arr[i-1]+arr[i-2];
7+
}
8+
return arr[n];
9+
};

solution/075.Sort Colors/Solution2.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution2:
2+
def sortColors(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: void Do not return anything, modify nums in-place instead.
6+
"""
7+
Count = [nums.count(color) for color in range(3)]
8+
9+
color = i = 0
10+
11+
while i < len(nums):
12+
for j in range(Count[color]):
13+
nums[i] = color
14+
i += 1
15+
color += 1

solution/084.Largest Rectangle in Histogram/README.md

+5-28
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,16 @@
1919
输出: 10
2020
```
2121

22-
### 解法1
22+
### 解法
23+
#### 思路一
2324
从前往后遍历 heightss[0...n]
2425

2526
- 若 heightss[i] > heightss[i - 1],则将 i 压入栈中;
2627
- 若 heightss[i] <= heightss[i - 1],则依次弹出栈,计算栈中能得到的最大矩形面积。
2728

2829
注意,压入栈中的是柱子的索引,而非柱子的高度。(通过索引可以获得高度、距离差)
2930

30-
### 解法2:构建升序栈
3131

32-
[见原文(方法二)](https://blog.csdn.net/jingsuwen1/article/details/51577983)
33-
34-
其实是对第一种解法的一种补充解释,同样是构建升序栈
35-
36-
数学模型分析:
37-
>假设题目中有升序水柱 {1,2,3,4,5,6},构建成栈内元素stack = {1,2,3,4,5,6}
38-
>
39-
>假设第7个入栈元素(水柱)值为4时,栈顶元素6出栈,因为是首位出栈的元素,count = 1。表示高度为6的柱最大宽度是1,最大面积是ans = 6 * 1;
40-
>
41-
>stack={1,2,3,4,5};想入栈的元素还是刚才那个4,栈顶元素:5>4,所以5要出栈,因为是连续出栈的第二个元素,所以count++。表示高度为5的柱最大宽度是2,最大面积是ans = max(ans,5 * 2) = 10;
42-
>
43-
>stack={1,2,3,4};想入栈的还是那个4,现在栈顶元素:4=4了,直接入栈:stack={1,2,3,4,4}
44-
>
45-
>因为count=2,柱子数量应该和heights[i]下标i相同来补位,所以要进去两个数,即要补count个heights[i]
46-
>
47-
>stack={1,2,3,4,4,4,4}
48-
49-
1. 初始化一个空栈,下标`i = 0`,数组首位元素入栈,下标`i++`,出栈个数`count = 0`
50-
2. 当heights[i]>=stack.top(),heights[i]入栈`i++,count =0`;否则,栈顶出栈,设置计数位count++,统计以栈顶元素出栈个数,`ans = max(ans,stack.top()*count)`,循环这一步操作
51-
3. 若count>0,补全栈内元素;count=0不处理
52-
4. 循环结束后计算升序栈以各元素为高的水柱面积
53-
54-
55-
------------------------------
56-
### JAVA(解法1)
5732

5833
```java
5934
class Solution {
@@ -94,7 +69,9 @@ class Solution {
9469
}
9570
```
9671

97-
### CPP(解法2)
72+
73+
74+
**C++版实现:**
9875

9976
```CPP
10077
class Solution {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const maxProfit1 = function(prices){
2+
let min = prices[0];
3+
let profit = 0;
4+
for(let i = 0; i < prices.length; i++){
5+
if(prices[i] < min){
6+
min = prices[i];
7+
}
8+
if(profit < (prices[i] - min)){
9+
profit = prices[i] - min;
10+
}
11+
}
12+
return profit;
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const maxProfit2 = function(prices){
2+
let profit = 0;
3+
for(let i = 1; i < prices.length; i++){
4+
if(prices[i]-prices[i-1] > 0){
5+
profit+=prices[i]-prices[i-1];
6+
}
7+
}
8+
return profit;
9+
}

0 commit comments

Comments
 (0)