Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
yaozc
ProjectLeet
Commits
0c359694
Commit
0c359694
authored
Oct 29, 2019
by
yaozc
Browse files
keep going
parent
3e509ac5
Changes
1
Hide whitespace changes
Inline
Side-by-side
all_in_one.cpp
View file @
0c359694
...
@@ -3186,6 +3186,323 @@ public:
...
@@ -3186,6 +3186,323 @@ public:
Unique
Paths
//选择python纯粹是因为把(m,n)这样的结构放进map里要好写很多
//最简单的写法为
/*
int uniquePaths(int m, int n) {
if(m==0 || n==0){
return 0;
}
if(m == 1 && n == 1){
return 1;
}
return uniquePaths(m, n-1) + uniquePaths(m-1, n);
}
*/
//但这样超时,所以加了一个map来查找当前的m n是否有计算过
//讲道理这个reursion写法是我好早以前想出来的,但这也太聪明了吧,我现在好像已经有点理解不了了
class
Solution
:
def
__init__
(
self
)
:
self
.
dp
=
dict
()
def
uniquePaths
(
self
,
m
:
int
,
n
:
int
)
->
int
:
self
.
dp
[(
0
,
0
)]
=
0
self
.
dp
[(
1
,
1
)]
=
1
return
self
.
helper
(
m
,
n
)
def
helper
(
self
,
m
,
n
)
->
int
:
if
(
m
,
n
)
in
self
.
dp
:
return
self
.
dp
[(
m
,
n
)]
if
m
==
0
or
n
==
0
:
return
0
if
m
==
1
and
n
==
1
:
return
1
self
.
dp
[(
m
,
n
)]
=
self
.
helper
(
m
-
1
,
n
)
+
self
.
helper
(
m
,
n
-
1
)
return
self
.
dp
[(
m
,
n
)]
Rotate
Array
//没有很难,但外面的for loop有点难想到,详见solution
class
Solution
{
public:
void
rotate
(
vector
<
int
>&
nums
,
int
k
)
{
if
(
nums
.
size
()
<=
1
)
return
;
if
(
k
==
nums
.
size
())
return
;
if
(
k
>
nums
.
size
())
k
%=
nums
.
size
();
int
count
=
0
;
// for loop存在是为了解决k太小而导致有些位置in-place不到问题
for
(
int
begin
=
0
;
count
<
nums
.
size
();
begin
++
){
bool
first
=
true
;
int
start
=
begin
;
int
prev
=
nums
[
start
];
while
(
first
||
begin
!=
start
){
first
=
false
;
count
++
;
int
next
=
start
+
k
;
if
(
next
>=
nums
.
size
())
next
%=
nums
.
size
();
//cout<<next<<endl;
int
temp
=
nums
[
next
];
nums
[
next
]
=
prev
;
prev
=
temp
;
start
=
next
;
}
}
}
}
;
//solution2, 两次reverse,最简单的写法
void
rotate
(
vector
<
int
>&
nums
,
int
k
)
{
if
(
nums
.
size
()
<=
1
)
return
;
if
(
k
==
nums
.
size
())
return
;
if
(
k
>
nums
.
size
())
k
%=
nums
.
size
();
reverse
(
nums
.
begin
(),
nums
.
end
());
reverse
(
nums
.
begin
(),
nums
.
begin
()
+
k
);
reverse
(
nums
.
begin
()
+
k
,
nums
.
end
());
}
Plus
One
//一看就知道是要在借位问题上搞花头的
//很多边角料的test case,全都考虑进去就行了
class
Solution
{
public:
vector
<
int
>
plusOne
(
vector
<
int
>&
digits
)
{
if
(
digits
.
empty
()){
digits
.
push_back
(
1
);
return
digits
;
}
if
(
digits
[
digits
.
size
()
-
1
]
!=
9
){
digits
[
digits
.
size
()
-
1
]
+=
1
;
return
digits
;
}
if
(
digits
.
size
()
==
1
){
digits
[
0
]
=
1
;
digits
.
push_back
(
0
);
return
digits
;
}
bool
brrow
=
false
;
for
(
int
i
=
digits
.
size
()
-
1
;
i
>=
0
;
i
--
){
if
(
i
==
0
&&
brrow
){
if
(
digits
[
0
]
==
9
){
digits
.
insert
(
digits
.
begin
(),
1
);}
else
{
digits
[
0
]
+=
1
;}
digits
[
i
+
1
]
=
0
;
return
digits
;
}
if
(
digits
[
i
]
==
9
){
brrow
=
true
;
digits
[
i
]
=
0
;
}
else
{
if
(
brrow
){
digits
[
i
]
=
digits
[
i
]
+
1
;
return
digits
;
}
else
{
return
digits
;
}
}
}
cout
<<
"error"
<<
endl
;
return
digits
;
}
};
First
Unique
Character
in
a
String
//一个比用hash table再优雅一点的办法就是用一个长度为26的array来代表所有char
//这题也太简单了吧。。。
class
Solution
{
public:
int
firstUniqChar
(
string
s
)
{
// char, time appears
vector
<
int
>
alphbate
(
26
,
0
);
for
(
auto
k
:
s
){
alphbate
[
k
-
'a'
]
+=
1
;
}
for
(
int
i
=
0
;
i
<
s
.
size
();
i
++
){
if
(
alphbate
[
s
[
i
]
-
'a'
]
==
1
)
return
i
;
}
return
-
1
;
}
};
Best
Time
to
Buy
and
Sell
Stock
II
//既然知道明天股票多少钱就买今天的呗
//就把今天和明天的股票比,要是低就把差价加到profit里
//原理就是 比如1 2 3数列,虽然 1 到 3 差了 2,但 2-1 + 3-2 也差 2 呀
class
Solution
{
public:
int
maxProfit
(
vector
<
int
>&
prices
)
{
if
(
prices
.
empty
())
return
0
;
int
profit
=
0
;
for
(
int
i
=
0
;
i
<
prices
.
size
()
-
1
;
i
++
){
if
(
prices
[
i
+
1
]
>
prices
[
i
]){
profit
+=
prices
[
i
+
1
]
-
prices
[
i
];
}
}
return
profit
;
}
};
Valid
Anagram
//easy
class
Solution
{
public:
bool
isAnagram
(
string
s
,
string
t
)
{
if
(
s
.
size
()
!=
t
.
size
())
return
false
;
unordered_map
<
char
,
int
>
hash
;
for
(
auto
k
:
s
){
hash
[
k
]
+=
1
;
}
for
(
auto
k
:
t
){
if
(
hash
.
find
(
k
)
!=
hash
.
end
()){
hash
[
k
]
--
;
if
(
hash
[
k
]
<
0
)
return
false
;
if
(
hash
[
k
]
==
0
){
hash
.
erase
(
k
);
}
}
else
return
false
;
}
return
hash
.
empty
();
}
};
Nested
List
Weight
Sum
II
//和 I 差不多,但需要一个
/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* class NestedInteger {
* public:
* // Constructor initializes an empty nested list.
* NestedInteger();
*
* // Constructor initializes a single integer.
* NestedInteger(int value);
*
* // Return true if this NestedInteger holds a single integer, rather than a nested list.
* bool isInteger() const;
*
* // Return the single integer that this NestedInteger holds, if it holds a single integer
* // The result is undefined if this NestedInteger holds a nested list
* int getInteger() const;
*
* // Set this NestedInteger to hold a single integer.
* void setInteger(int value);
*
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
* void add(const NestedInteger &ni);
*
* // Return the nested list that this NestedInteger holds, if it holds a nested list
* // The result is undefined if this NestedInteger holds a single integer
* const vector<NestedInteger> &getList() const;
* };
*/
class
Solution
{
public:
int
depthSumInverse
(
vector
<
NestedInteger
>&
nestedList
)
{
if
(
nestedList
.
empty
())
return
0
;
int
deepest
=
depth_digger
(
nestedList
);
cout
<<
deepest
<<
endl
;
return
depthsum
(
nestedList
,
deepest
);
}
int
depthsum
(
vector
<
NestedInteger
>&
list
,
int
depth
){
int
sum
=
0
;
for
(
auto
&
n
:
list
){
if
(
n
.
isInteger
())
sum
+=
depth
*
n
.
getInteger
();
else
sum
+=
depthsum
(
n
.
getList
(),
depth
-
1
);
}
return
sum
;
}
int
depth_digger
(
vector
<
NestedInteger
>&
n
){
if
(
n
.
empty
())
return
0
;
int
deepest
=
1
;
for
(
auto
k
:
n
){
if
(
!
k
.
isInteger
()){
deepest
=
max
(
deepest
,
1
+
depth_digger
(
k
.
getList
()));
}
}
return
deepest
;
}
};
Longest
Common
Prefix
//没啥难的 直接想出来就写
//反正就找到最短的那个,然后把他和其他单词逐个字母遍历呗,要是char对不上就把最短的那个给再截一下
class
Solution
{
public:
string
longestCommonPrefix
(
vector
<
string
>&
strs
)
{
if
(
strs
.
empty
())
return
""
;
string
shortest
=
strs
[
0
];
for
(
auto
&
k
:
strs
){
if
(
shortest
.
size
()
>
k
.
size
())
shortest
=
k
;
}
for
(
auto
&
k
:
strs
){
for
(
int
i
=
0
;
i
<
shortest
.
size
();
i
++
){
if
(
k
[
i
]
!=
shortest
[
i
]){
if
(
i
==
0
)
return
""
;
else
{
shortest
=
shortest
.
substr
(
0
,
i
);
}
}
}
}
return
shortest
;
}
};
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment