Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Miykael_xxm
leetcode
提交
c0f32a1b
L
leetcode
项目概览
Miykael_xxm
/
leetcode
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
L
leetcode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
c0f32a1b
编写于
1月 14, 2021
作者:
L
lucifer
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: cpp code
上级
fe985d25
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
242 addition
and
14 deletion
+242
-14
problems/32.longest-valid-parentheses.md
problems/32.longest-valid-parentheses.md
+74
-4
problems/39.combination-sum.md
problems/39.combination-sum.md
+34
-1
problems/46.permutations.md
problems/46.permutations.md
+31
-1
problems/47.permutations-ii.md
problems/47.permutations-ii.md
+31
-3
problems/48.rotate-image.md
problems/48.rotate-image.md
+23
-3
problems/49.group-anagrams.md
problems/49.group-anagrams.md
+25
-1
problems/50.pow-x-n.md
problems/50.pow-x-n.md
+24
-1
未找到文件。
problems/32.longest-valid-parentheses.md
浏览文件 @
c0f32a1b
...
...
@@ -85,7 +85,7 @@ class Solution:
### 代码
-
语言支持: Python, javascript
-
语言支持: Python, javascript
, CPP
javascript code:
...
...
@@ -133,6 +133,26 @@ class Solution:
return
res
```
CPP Code:
```
cpp
class
Solution
{
public:
int
longestValidParentheses
(
string
s
)
{
stack
<
int
>
st
;
st
.
push
(
-
1
);
int
ans
=
0
;
for
(
int
i
=
0
;
i
<
s
.
size
();
++
i
)
{
if
(
s
[
i
]
==
')'
&&
st
.
top
()
!=
-
1
&&
s
[
st
.
top
()]
==
'('
)
{
st
.
pop
();
ans
=
max
(
ans
,
i
-
st
.
top
());
}
else
st
.
push
(
i
);
}
return
ans
;
}
};
```
**复杂度分析**
-
时间复杂度:$$O(N)$$
...
...
@@ -154,7 +174,9 @@ class Solution:
### 代码
代码支持:Java,Python
代码支持:Java,Python3, CPP
Java Code:
```
java
public
class
Solution
{
...
...
@@ -192,6 +214,8 @@ public class Solution {
}
```
Python3 Code:
```
py
class
Solution
:
def
longestValidParentheses
(
self
,
s
:
str
)
->
int
:
...
...
@@ -219,6 +243,31 @@ class Solution:
return
ans
```
CPP Code:
```
cpp
class
Solution
{
public:
int
longestValidParentheses
(
string
s
)
{
int
left
=
0
,
right
=
0
,
ans
=
0
,
N
=
s
.
size
();
for
(
int
i
=
0
;
i
<
N
;
++
i
)
{
left
+=
s
[
i
]
==
'('
;
right
+=
s
[
i
]
==
')'
;
if
(
left
==
right
)
ans
=
max
(
ans
,
left
+
right
);
else
if
(
right
>
left
)
left
=
right
=
0
;
}
left
=
0
,
right
=
0
;
for
(
int
i
=
N
-
1
;
i
>=
0
;
--
i
)
{
left
+=
s
[
i
]
==
'('
;
right
+=
s
[
i
]
==
')'
;
if
(
left
==
right
)
ans
=
max
(
ans
,
left
+
right
);
else
if
(
left
>
right
)
left
=
right
=
0
;
}
return
ans
;
}
};
```
## 动态规划
### 思路
...
...
@@ -245,7 +294,9 @@ s = '(())())'
### 代码
Python Code:
代码支持:Python3, CPP
Python3 Code:
```
py
class
Solution
:
...
...
@@ -273,6 +324,26 @@ class Solution:
return
mlen
```
CPP Code:
```
cpp
class
Solution
{
public:
int
longestValidParentheses
(
string
s
)
{
vector
<
int
>
dp
(
s
.
size
()
+
1
,
0
);
int
ans
=
0
;
for
(
int
i
=
0
;
i
<
s
.
size
();
++
i
)
{
if
(
s
[
i
]
==
'('
)
continue
;
int
start
=
i
-
dp
[
i
]
-
1
;
if
(
start
>=
0
&&
s
[
start
]
==
'('
)
dp
[
i
+
1
]
=
dp
[
i
]
+
2
+
dp
[
start
];
ans
=
max
(
ans
,
dp
[
i
+
1
]);
}
return
ans
;
}
};
```
**复杂度分析**
-
时间复杂度:$$O(N)$$
...
...
@@ -292,7 +363,6 @@ class Solution:
1.
如果判断的不仅仅只有'('和')', 还有'[', ']', '{'和'}', 该怎么办?
2.
如果输出的不是长度, 而是任意一个最长有效括号对的字符串, 该怎么办?
大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。
大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。
![](
https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg
)
problems/39.combination-sum.md
浏览文件 @
c0f32a1b
...
...
@@ -76,7 +76,9 @@ candidate 中的每个元素都是独一无二的。
## 代码
-
语言支持: Javascript,Python3
-
语言支持: Javascript,Python3,CPP
JS Code:
```
js
function
backtrack
(
list
,
tempList
,
nums
,
remain
,
start
)
{
...
...
@@ -147,6 +149,33 @@ class Solution:
path
.
pop
()
```
CPP Code:
```
cpp
class
Solution
{
private:
vector
<
vector
<
int
>>
res
;
void
dfs
(
vector
<
int
>
&
c
,
int
t
,
int
start
,
vector
<
int
>
&
v
)
{
if
(
!
t
)
{
res
.
push_back
(
v
);
return
;
}
for
(
int
i
=
start
;
i
<
c
.
size
()
&&
t
>=
c
[
i
];
++
i
)
{
v
.
push_back
(
c
[
i
]);
dfs
(
c
,
t
-
c
[
i
],
i
,
v
);
v
.
pop_back
();
}
}
public:
vector
<
vector
<
int
>>
combinationSum
(
vector
<
int
>&
candidates
,
int
target
)
{
sort
(
candidates
.
begin
(),
candidates
.
end
());
vector
<
int
>
v
;
dfs
(
candidates
,
target
,
0
,
v
);
return
res
;
}
};
```
## 相关题目
-
[
40.combination-sum-ii
](
./40.combination-sum-ii.md
)
...
...
@@ -156,3 +185,7 @@ class Solution:
-
[
90.subsets-ii
](
./90.subsets-ii.md
)
-
[
113.path-sum-ii
](
./113.path-sum-ii.md
)
-
[
131.palindrome-partitioning
](
./131.palindrome-partitioning.md
)
```
```
problems/46.permutations.md
浏览文件 @
c0f32a1b
...
...
@@ -63,7 +63,7 @@ https://leetcode-cn.com/problems/permutations/
## 代码
-
语言支持: Javascript, Python3
-
语言支持: Javascript, Python3
,CPP
Javascript Code:
...
...
@@ -130,6 +130,36 @@ class Solution:
return res
```
CPP Code:
```
cpp
class
Solution
{
vector
<
vector
<
int
>>
ans
;
void
dfs
(
vector
<
int
>
&
nums
,
int
start
)
{
if
(
start
==
nums
.
size
()
-
1
)
{
ans
.
push_back
(
nums
);
return
;
}
for
(
int
i
=
start
;
i
<
nums
.
size
();
++
i
)
{
swap
(
nums
[
i
],
nums
[
start
]);
dfs
(
nums
,
start
+
1
);
swap
(
nums
[
i
],
nums
[
start
]);
}
}
public:
vector
<
vector
<
int
>>
permute
(
vector
<
int
>&
nums
)
{
dfs
(
nums
,
0
);
return
ans
;
}
};
```
**复杂度分析**
令 N 为数组长度。
-
时间复杂度:$O(N!)$
-
空间复杂度:$O(N)$
## 相关题目
-
[
31.next-permutation
](
./31.next-permutation.md
)
...
...
problems/47.permutations-ii.md
浏览文件 @
c0f32a1b
...
...
@@ -49,11 +49,11 @@ https://leetcode-cn.com/problems/permutations-ii/
如果继续沿用上面的逻辑,那么我们是永远无法拿到三个数字的,因此我们的逻辑需要变化。 这里我的算法是记录每一个被选取的索引,而不是值,这就保证了
**同一个数字不会被选取多次,并且可以选取所有数字了**
。
不过这还是有一个问题。 仍然以 [1,1,2]
为例,如果第一次选取了第一个1,第二次选取了第二个
1,这就产生了一个组合 [1,1,2]。 如果继续第一次选取了第二个 1,而第二次选取了第一个 1,那么又会产生组合 [1,1,2],可以看出这两个组合是重复的。
不过这还是有一个问题。 仍然以 [1,1,2]
为例,如果第一次选取了第一个 1,第二次选取了第二个
1,这就产生了一个组合 [1,1,2]。 如果继续第一次选取了第二个 1,而第二次选取了第一个 1,那么又会产生组合 [1,1,2],可以看出这两个组合是重复的。
一个解决方案是对 nums 进行一次排序,并规定如果
**i > 0 and nums[i] == nums[i - 1] and visited[i - 1]**
, 则不进行选取即可。
经过这样的处理,每次实际上都是从后往前依次重复的数。仍然以上面的 [1,1,2]
为例。[1,1,2] 这个排列一定是先取的第二个 1,再取第一个 1,最后取的 2。因为如果先取的第一个 1,那么永远无法取到三个数,便形成了一个不可行解。
经过这样的处理,每次实际上都是从后往前依次重复的数。仍然以上面的 [1,1,2] 为例。[1,1,2] 这个排列一定是先取的第二个 1,再取第一个 1,最后取的 2。因为如果先取的第一个 1,那么永远无法取到三个数,便形成了一个不可行解。
## 关键点解析
...
...
@@ -62,7 +62,9 @@ https://leetcode-cn.com/problems/permutations-ii/
## 代码
-
语言支持: Javascript,Python3
-
语言支持: Javascript,Python3, CPP
JS Code:
```
js
/*
...
...
@@ -131,6 +133,32 @@ class Solution:
return res
```
CPP Code:
```
cpp
class
Solution
{
private:
vector
<
vector
<
int
>>
ans
;
void
permute
(
vector
<
int
>
nums
,
int
start
)
{
if
(
start
==
nums
.
size
()
-
1
)
{
ans
.
push_back
(
nums
);
return
;
}
for
(
int
i
=
start
;
i
<
nums
.
size
();
++
i
)
{
if
(
i
!=
start
&&
nums
[
i
]
==
nums
[
start
])
continue
;
swap
(
nums
[
i
],
nums
[
start
]);
permute
(
nums
,
start
+
1
);
}
}
public:
vector
<
vector
<
int
>>
permuteUnique
(
vector
<
int
>&
nums
)
{
sort
(
nums
.
begin
(),
nums
.
end
());
permute
(
nums
,
0
);
return
ans
;
}
};
```
## 相关题目
-
[
31.next-permutation
](
./31.next-permutation.md
)
...
...
problems/48.rotate-image.md
浏览文件 @
c0f32a1b
...
...
@@ -15,7 +15,7 @@ https://leetcode-cn.com/problems/rotate-image/
示例 1:
给定 matrix =
给定 matrix =
[
[1,2,3],
[4,5,6],
...
...
@@ -36,7 +36,7 @@ https://leetcode-cn.com/problems/rotate-image/
[ 2, 4, 8,10],
[13, 3, 6, 7],
[15,14,12,16]
],
],
原地旋转输入矩阵,使其变为:
[
...
...
@@ -100,7 +100,7 @@ var rotate = function (matrix) {
## 代码
-
语言支持: Javascript,Python3
-
语言支持: Javascript,Python3
, CPP
```
js
/*
...
...
@@ -161,6 +161,26 @@ class Solution:
matrix[:] = map(list, zip(*matrix[::-1]))
```
CPP Code:
```
cpp
class
Solution
{
public:
void
rotate
(
vector
<
vector
<
int
>>&
matrix
)
{
int
N
=
matrix
.
size
();
for
(
int
i
=
0
;
i
<
N
/
2
;
++
i
)
{
for
(
int
j
=
i
;
j
<
N
-
i
-
1
;
++
j
)
{
int
tmp
=
matrix
[
i
][
j
];
matrix
[
i
][
j
]
=
matrix
[
N
-
j
-
1
][
i
];
matrix
[
N
-
j
-
1
][
i
]
=
matrix
[
N
-
i
-
1
][
N
-
j
-
1
];
matrix
[
N
-
i
-
1
][
N
-
j
-
1
]
=
matrix
[
j
][
N
-
i
-
1
];
matrix
[
j
][
N
-
i
-
1
]
=
tmp
;
}
}
}
};
```
**复杂度分析**
-
时间复杂度:$$O(M
*
N)$$
...
...
problems/49.group-anagrams.md
浏览文件 @
c0f32a1b
...
...
@@ -80,7 +80,9 @@ var groupAnagrams = function (strs) {
## 代码
-
语言支持: Javascript,Python3
-
语言支持: Javascript,Python3, CPP
JS Code:
```
js
/*
...
...
@@ -135,6 +137,28 @@ class Solution:
return list(str_dict.values())
```
CPP Code:
```
cpp
class
Solution
{
public:
vector
<
vector
<
string
>>
groupAnagrams
(
vector
<
string
>&
A
)
{
unordered_map
<
string
,
int
>
m
;
vector
<
vector
<
string
>>
ans
;
for
(
auto
&
s
:
A
)
{
auto
p
=
s
;
sort
(
p
.
begin
(),
p
.
end
());
if
(
!
m
.
count
(
p
))
{
m
[
p
]
=
ans
.
size
();
ans
.
push_back
({});
}
ans
[
m
[
p
]].
push_back
(
s
);
}
return
ans
;
}
};
```
**复杂度分析**
其中 N 为 strs 的长度, M 为 strs 中字符串的平均长度。
...
...
problems/50.pow-x-n.md
浏览文件 @
c0f32a1b
...
...
@@ -123,7 +123,7 @@ class Solution:
### 代码
语言支持: Python3
语言支持: Python3
, CPP
Python3 Code:
...
...
@@ -139,6 +139,29 @@ class Solution:
return
self
.
myPow
(
x
_
x
,
n
//
2
)
if
n
%
2
==
0
else
x
_
self
.
myPow
(
x
,
n
-
1
)
```
CPP Code:
```
cpp
class
Solution
{
double
myPow
(
double
x
,
long
n
)
{
if
(
n
<
0
)
return
1
/
myPow
(
x
,
-
n
);
if
(
n
==
0
)
return
1
;
if
(
n
==
1
)
return
x
;
if
(
n
==
2
)
return
x
*
x
;
return
myPow
(
myPow
(
x
,
n
/
2
),
2
)
*
(
n
%
2
?
x
:
1
);
}
public:
double
myPow
(
double
x
,
int
n
)
{
return
myPow
(
x
,
(
long
)
n
);
}
};
```
**复杂度分析**
-
时间复杂度:$$O(logN)$$
-
空间复杂度:$$O(logN)$$
## 解法三 - 位运算
### 思路
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录