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
92a19242
Commit
92a19242
authored
Jul 11, 2019
by
zhayao
Browse files
Merge branch 'master' of gitlab.eecs.umich.edu:yaozc/projectleet
parents
ad2a9951
3e4e27dd
Changes
1
Hide whitespace changes
Inline
Side-by-side
amazon.cpp
View file @
92a19242
...
...
@@ -90,14 +90,14 @@ public:
int
sum
=
nums
[
outer
]
+
nums
[
left
]
+
nums
[
right
];
int
diff
=
target
-
sum
;
if
(
abs
(
diff
)
<
closest
)
{
closest
=
abs
(
diff
);
result
=
sum
;
closest
=
abs
(
diff
);
result
=
sum
;
}
if
(
diff
<
0
)
{
right
--
;
right
--
;
}
else
if
(
diff
>
0
)
{
left
++
;
left
++
;
}
else
{
return
result
;
...
...
@@ -116,7 +116,7 @@ public:
// step 2.若遇到不符,skip多少个由查ips表所知
/*
lps[i] = the longest proper prefix of pat[0..i]
which is also a suffix of pat[0..i].
which is also a suffix of pat[0..i].
For the pattern “ABCDE”,
pat = "AAAA"
...
...
@@ -136,25 +136,25 @@ lps[] is [0, 1, 0, 1, 2, 0, 1, 2, 3, 4, 5]
// 然后对称线swap
// temp作为对角线终点变量(每一行到哪里停)
/*
* clockwise rotate
* first reverse up to down, then swap the symmetry
* 1 2 3 7 8 9 7 4 1
* 4 5 6 => 4 5 6 => 8 5 2
* 7 8 9 1 2 3 9 6 3
* clockwise rotate
* first reverse up to down, then swap the symmetry
* 1 2 3 7 8 9 7 4 1
* 4 5 6 => 4 5 6 => 8 5 2
* 7 8 9 1 2 3 9 6 3
*/
class
Solution
{
public:
void
rotate
(
vector
<
vector
<
int
>>&
matrix
)
{
swap
(
matrix
.
begin
(),
matrix
.
end
());
int
temp
=
1
;
for
(
int
outer
=
0
;
outer
<
matrix
.
size
();
outer
++
)
{
for
(
int
inner
=
0
;
inner
<
temp
;
inner
++
)
{
swap
(
matrix
[
outer
][
inner
],
matrix
[
inner
][
outer
]);
}
temp
++
;
}
void
rotate
(
vector
<
vector
<
int
>>&
matrix
)
{
swap
(
matrix
.
begin
(),
matrix
.
end
());
int
temp
=
1
;
for
(
int
outer
=
0
;
outer
<
matrix
.
size
();
outer
++
)
{
for
(
int
inner
=
0
;
inner
<
temp
;
inner
++
)
{
swap
(
matrix
[
outer
][
inner
],
matrix
[
inner
][
outer
]);
}
temp
++
;
}
}
};
...
...
@@ -165,21 +165,21 @@ public:
// 因为1+2+3 = 2 + 2 + 2且几乎无法区分!
class
Solution
{
public:
vector
<
vector
<
string
>>
groupAnagrams
(
vector
<
string
>&
strs
)
{
vector
<
vector
<
string
>>
result
;
map
<
string
,
std
::
vector
<
string
>>
table
;
for
(
int
i
=
0
;
i
<
strs
.
size
();
i
++
)
{
string
the_key
=
strs
[
i
];
sort
(
the_key
.
begin
(),
the_key
.
end
());
table
[
the_key
].
push_back
(
strs
[
i
]);
}
vector
<
vector
<
string
>>
groupAnagrams
(
vector
<
string
>&
strs
)
{
vector
<
vector
<
string
>>
result
;
map
<
string
,
std
::
vector
<
string
>>
table
;
for
(
int
i
=
0
;
i
<
strs
.
size
();
i
++
)
{
string
the_key
=
strs
[
i
];
sort
(
the_key
.
begin
(),
the_key
.
end
());
table
[
the_key
].
push_back
(
strs
[
i
]);
}
for
(
auto
it
=
table
.
begin
();
it
!=
table
.
end
();
it
++
)
{
result
.
push_back
(
it
->
second
);
}
return
result
;
for
(
auto
it
=
table
.
begin
();
it
!=
table
.
end
();
it
++
)
{
result
.
push_back
(
it
->
second
);
}
return
result
;
}
};
// First Unique Character in a String
...
...
@@ -187,18 +187,18 @@ public:
// it's much better than have a map
class
Solution
{
public:
int
firstUniqChar
(
string
s
)
{
vector
<
int
>
count
(
26
,
0
);
for
(
int
i
=
0
;
i
<
s
.
size
();
i
++
){
count
[
s
[
i
]
-
'a'
]
++
;
}
for
(
int
i
=
0
;
i
<
s
.
size
();
i
++
){
if
(
count
[
s
[
i
]
-
'a'
]
==
1
){
return
i
;
}
}
return
-
1
;
int
firstUniqChar
(
string
s
)
{
vector
<
int
>
count
(
26
,
0
);
for
(
int
i
=
0
;
i
<
s
.
size
();
i
++
){
count
[
s
[
i
]
-
'a'
]
++
;
}
for
(
int
i
=
0
;
i
<
s
.
size
();
i
++
){
if
(
count
[
s
[
i
]
-
'a'
]
==
1
){
return
i
;
}
}
return
-
1
;
}
};
...
...
@@ -207,40 +207,360 @@ public:
// solution looks ugly but work, no really better solutions
class
Solution
{
public:
bool
isValid
(
string
s
)
{
stack
<
char
>
mystack
;
mystack
.
push
(
'1'
);
for
(
int
i
=
0
;
i
<
s
.
size
();
i
++
){
if
(
s
[
i
]
==
']'
){
if
(
mystack
.
top
()
==
'['
){
mystack
.
pop
();
}
else
{
return
false
;
}
}
else
if
(
s
[
i
]
==
'}'
){
if
(
mystack
.
top
()
==
'{'
){
mystack
.
pop
();
}
else
{
return
false
;
}
bool
isValid
(
string
s
)
{
stack
<
char
>
mystack
;
mystack
.
push
(
'1'
);
for
(
int
i
=
0
;
i
<
s
.
size
();
i
++
){
if
(
s
[
i
]
==
']'
){
if
(
mystack
.
top
()
==
'['
){
mystack
.
pop
();
}
else
{
return
false
;
}
}
else
if
(
s
[
i
]
==
'}'
){
if
(
mystack
.
top
()
==
'{'
){
mystack
.
pop
();
}
else
{
return
false
;
}
}
else
if
(
s
[
i
]
==
')'
){
if
(
mystack
.
top
()
==
'('
){
mystack
.
pop
();
}
else
{
return
false
;
}
}
else
{
mystack
.
push
(
s
[
i
]);
}
}
return
mystack
.
top
()
==
'1'
?
true
:
false
;
}
};
int
trap
(
vector
<
int
>&
height
)
{
int
sum
=
0
;
int
left
=
-
1
;
int
right
=
-
1
;
for
(
int
i
=
0
;
i
<
height
.
size
();
i
++
){
}
}
// Merge two sorted list
// Example:
//
// Input: 1->2->4, 1->3->4
// Output: 1->1->2->3->4->4
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class
Solution
{
public:
ListNode
*
mergeTwoLists
(
ListNode
*
l1
,
ListNode
*
l2
)
{
// create a dummy node which is am empty node and never being filled
ListNode
dummy
(
0
);
ListNode
*
tail
=
&
dummy
;
while
(
l1
&&
l2
){
if
(
l1
->
val
<
l2
->
val
){
tail
->
next
=
l1
;
l1
=
l1
->
next
;
}
else
{
tail
->
next
=
l2
;
l2
=
l2
->
next
;
}
tail
=
tail
->
next
;
}
tail
->
next
=
l1
?
l1
:
l2
;
return
dummy
.
next
;
}
};
// solution2, recursive one, think one layer only!!! so elegent!!
class
Solution
{
public:
ListNode
*
mergeTwoLists
(
ListNode
*
l1
,
ListNode
*
l2
)
{
if
(
!
l1
)
return
l2
;
if
(
!
l2
)
return
l1
;
if
(
l1
->
val
<
l2
->
val
){
l1
->
next
=
mergeTwoLists
(
l1
->
next
,
l2
);
return
l1
;
}
else
{
l2
->
next
=
mergeTwoLists
(
l1
,
l2
->
next
);
return
l2
;
}
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
// solution1: recursive
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class
Solution
{
public:
ListNode
*
mergeKLists
(
vector
<
ListNode
*>&
lists
)
{
if
(
lists
.
empty
()){
return
nullptr
;
}
while
(
lists
.
size
()
>
1
)
{
lists
.
push_back
(
merge_two_linked_lists
(
lists
[
0
],
lists
[
1
]));
lists
.
erase
(
lists
.
begin
(),
++++
lists
.
begin
());
}
return
lists
[
0
];
}
ListNode
*
merge_two_linked_lists
(
ListNode
*
l1
,
ListNode
*
l2
){
if
(
!
l1
)
return
l2
;
if
(
!
l2
)
return
l1
;
if
(
l1
->
val
<
l2
->
val
)
{
l1
->
next
=
merge_two_linked_lists
(
l1
->
next
,
l2
);
return
l1
;
}
else
{
l2
->
next
=
merge_two_linked_lists
(
l1
,
l2
->
next
);
return
l2
;
}
}
};
//Reverse a linked list
// solution1: iteratively
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class
Solution
{
public:
ListNode
*
reverseList
(
ListNode
*
head
)
{
ListNode
*
current
=
head
;
ListNode
*
prev
=
NULL
;
ListNode
*
next
=
NULL
;
while
(
current
){
next
=
current
->
next
;
current
->
next
=
prev
;
prev
=
current
;
current
=
next
;
}
return
prev
;
}
};
//solution2: recursively
class
Solution
{
public:
ListNode
*
reverseList
(
ListNode
*
head
)
{
if
(
!
head
)
return
nullptr
;
if
(
!
head
->
next
)
return
head
;
head
->
next
=
reverseList
(
head
->
next
);
return
head
;
}
};
//今天是yorkevin账号购买LeetCode的第一天。新的开始!让我们我们在这里留下想法和心得!
// Validate Binary Search Tree
//每个点都应该在一个特定的range之内,具体大小为:root左边的第一个点在(负无穷到root->val)
// root右边的第一个点在(正无穷到root->val)
//每call一次recursion就update一个max和min
class
Solution
{
public:
bool
isValidBST
(
TreeNode
*
root
)
{
return
checker
(
root
,
LONG_MAX
,
LONG_MIN
);
}
bool
checker
(
TreeNode
*
root
,
long
max
,
long
min
){
if
(
!
root
)
return
true
;
else
if
(
root
->
val
>=
max
||
root
->
val
<=
min
)
return
false
;
return
checker
(
root
->
left
,
root
->
val
,
min
)
&&
checker
(
root
->
right
,
max
,
root
->
val
);
}
};
// Symmetric Tree
// https://www.youtube.com/watch?v=XV7Sg2hJO3Q&t=613s
// 本质为check leftsub.left == rightsub.right && leftsub.right == rightsub.left
// 遇到这种问题首先想想是不是只要比较 == 一次就ok
class
Solution
{
public:
bool
isSymmetric
(
TreeNode
*
root
)
{
if
(
!
root
)
return
true
;
return
helper
(
root
->
left
,
root
->
right
);
}
bool
helper
(
TreeNode
*
left
,
TreeNode
*
right
){
if
(
!
left
&&
!
right
)
return
true
;
else
if
(
left
&&
right
){
return
left
->
val
==
right
->
val
&&
helper
(
left
->
left
,
right
->
right
)
&&
helper
(
left
->
right
,
right
->
left
);
}
return
false
;
}
};
// Binary Tree Level Order Traversal
// nothing fancy,这道题主要考BFS,记住BFS广度搜索要用queue,DFS用stack
// 主要细节在与while中的for loop。不可以用while套while来代替!! 因为queue的size一直在变
// 所以每个level的大小都是有这个循环开始时 queue的大小来决定的!!!
// https://www.youtube.com/watch?v=gcR28Hc2TNQ
// https://www.youtube.com/watch?v=XZnWETlZZ14
class
Solution
{
public:
vector
<
vector
<
int
>>
levelOrder
(
TreeNode
*
root
)
{
vector
<
vector
<
int
>>
res
;
queue
<
TreeNode
*>
q
;
if
(
!
root
)
return
res
;
q
.
push
(
root
);
while
(
!
q
.
empty
()){
vector
<
int
>
temp
;
int
end
=
q
.
size
();
for
(
int
i
=
0
;
i
<
end
;
i
++
){
TreeNode
*
current
=
q
.
front
();
q
.
pop
();
temp
.
push_back
(
current
->
val
);
if
(
current
->
left
)
q
.
push
(
current
->
left
);
if
(
current
->
right
)
q
.
push
(
current
->
right
);
}
else
if
(
s
[
i
]
==
')'
){
if
(
mystack
.
top
()
==
'('
){
mystack
.
pop
();
res
.
push_back
(
temp
);
}
return
res
;
}
};
Binary
Tree
Zigzag
Level
Order
Traversal
//和上一题BFS不同的是,zigzag使用的是2个stack,奇数层用s1,偶数层用s2。
// 奇数层先看left sub tree,然后right sub tree. 偶数层先看right sub tree, 然后left sub tree
// https://www.youtube.com/watch?v=YsLko6sSKh8&t=49s
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class
Solution
{
public:
vector
<
vector
<
int
>>
zigzagLevelOrder
(
TreeNode
*
root
)
{
vector
<
vector
<
int
>>
res
;
if
(
!
root
)
return
res
;
stack
<
TreeNode
*>
s1
;
stack
<
TreeNode
*>
s2
;
s1
.
push
(
root
);
int
level
=
0
;
while
(
!
s1
.
empty
()
||
!
s2
.
empty
())
{
if
(
level
%
2
==
0
){
vector
<
int
>
temp
;
while
(
!
s1
.
empty
()){
cout
<<
"1"
<<
endl
;
TreeNode
*
curr
=
s1
.
top
();
s1
.
pop
();
temp
.
push_back
(
curr
->
val
);
if
(
curr
->
left
)
s2
.
push
(
curr
->
left
);
if
(
curr
->
right
)
s2
.
push
(
curr
->
right
);
}
else
{
return
false
;
}
res
.
push_back
(
temp
);
}
else
{
mystack
.
push
(
s
[
i
]);
vector
<
int
>
temp
;
while
(
!
s2
.
empty
()){
cout
<<
"2"
<<
endl
;
TreeNode
*
curr
=
s2
.
top
();
s2
.
pop
();
temp
.
push_back
(
curr
->
val
);
if
(
curr
->
right
)
s1
.
push
(
curr
->
right
);
if
(
curr
->
left
)
s1
.
push
(
curr
->
left
);
}
res
.
push_back
(
temp
);
}
level
++
;
cout
<<
"3"
<<
endl
;
}
return
mystack
.
top
()
==
'1'
?
true
:
false
;
return
res
;
}
};
Binary
Tree
Maximum
Path
Sum
// not fully understood
// 大体意思为,因为可以不包含root,所以随时要考虑是否应该用 new_path,来代替包含root的old path
// 要随时和0比较是因为有可能出现左右分支都小于0的情况 (避免sum有可能被加上了较小的那个负数情况)
// return的内容是为recursion服务的,传回的值为parent加上一个较大的noded情况
class
Solution
{
int
sum
=
INT_MIN
;
public:
int
maxPathSum
(
TreeNode
*
root
)
{
max_gain
(
root
);
return
sum
;
}
int
max_gain
(
TreeNode
*
parent
){
if
(
!
parent
)
return
0
;
int
left
=
max
(
max_gain
(
parent
->
left
),
0
);
int
right
=
max
(
max_gain
(
parent
->
right
),
0
);
int
new_path
=
left
+
right
+
parent
->
val
;
sum
=
max
(
sum
,
new_path
);
return
max
(
parent
->
val
+
max
(
left
,
right
),
0
);
}
};
// Reverse Nodes in k-Group
...
...
Write
Preview
Markdown
is supported
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