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
76916012
Commit
76916012
authored
Aug 02, 2021
by
yaozc
Browse files
Update the old uncommited changes
parent
0c359694
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
all_in_one.cpp
View file @
76916012
...
...
@@ -444,7 +444,9 @@ public:
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
left
->
val
==
right
->
val
&&
helper
(
left
->
left
,
right
->
right
)
&&
helper
(
left
->
right
,
right
->
left
);
}
return
false
;
}
...
...
@@ -3487,6 +3489,180 @@ public:
Reverse
Linked
List
/**
* 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
*
prev
=
nullptr
;
ListNode
*
curr
=
head
;
// current is not null
while
(
curr
){
ListNode
*
next
=
curr
->
next
;
curr
->
next
=
prev
;
prev
=
curr
;
curr
=
next
;
}
return
prev
;
}
};
152.
Maximum
Product
Subarray
/*
两个 dp 数组,其中 big[i] 表示子数组 [0, i] 范围内并且一定包含 nums[i]
数字的最大子数组乘积,small[i] 表示子数组 [0, i] 范围内并且一定包含 nums[i]
数字的最小子数组乘积,初始化时 big[0] 和 small[0] 都初始化为 nums[0],
其余都初始化为0。那么从数组的第二个数字开始遍历,那么此时的最大值和最小值只会
在这三个数字之间产生,即 big[i-1]*nums[i],small[i-1]*nums[i],和 nums[i]。*/
class
Solution
{
public:
int
maxProduct
(
vector
<
int
>&
nums
)
{
if
(
nums
.
empty
())
return
0
;
vector
<
int
>
big
(
nums
.
size
());
vector
<
int
>
small
(
nums
.
size
());
big
[
0
]
=
nums
[
0
];
small
[
0
]
=
nums
[
0
];
int
res
=
nums
[
0
];
for
(
int
i
=
1
;
i
<
nums
.
size
();
i
++
){
big
[
i
]
=
std
::
max
(
nums
[
i
],
std
::
max
(
big
[
i
-
1
]
*
nums
[
i
],
small
[
i
-
1
]
*
nums
[
i
]));
small
[
i
]
=
std
::
min
(
nums
[
i
],
std
::
min
(
big
[
i
-
1
]
*
nums
[
i
],
small
[
i
-
1
]
*
nums
[
i
]));
res
=
std
::
max
(
res
,
big
[
i
]);
}
return
res
;
}
};
285.
Inorder
Successor
in
BST
//讲道理这道题还是让我出了一身汗的
//说白了就是用一个bool来确定下一个是不是要找的那个,毕竟我们能确定的是前一个
//但要注意,首先bool值要pass by reference,然后要记住在将global var res
//的值给定好后,要设回false,因为否则会不断的去记录上一层recursion的val,直到最顶层
//注释掉的部分是用stack来做recursion的方法,要注意 1.while里套while,直到当前node没有left了
//2. while之后是把指针设置为stack的top,as you can see,这里和recursion其实是一个逻辑, 第一步无限进入recursion后,
//最先处理的node其实是stack最top的那个
//然后就是处理当前的right了,仔细想想,这才是最符合recursion的写法
class
Solution
{
public:
TreeNode
*
res
=
NULL
;
TreeNode
*
inorderSuccessor
(
TreeNode
*
root
,
TreeNode
*
p
)
{
// stack<TreeNode*> s;
// TreeNode* temp = root;
// while(!s.empty() || temp){
// while(temp){
// s.push(temp);
// temp = temp->left;
// }
// temp = s.top(); s.pop();
// //deal
// cout<<temp->val<<endl;
// temp = temp->right;
// }
// return root;
bool
found
=
false
;
helper
(
root
,
p
,
found
);
return
res
;
}
void
helper
(
TreeNode
*
curr
,
TreeNode
*
p
,
bool
&
found
){
if
(
!
curr
)
return
;
helper
(
curr
->
left
,
p
,
found
);
if
(
found
){
res
=
curr
;
found
=
false
;
return
;
}
if
(
curr
==
p
)
found
=
true
;
helper
(
curr
->
right
,
p
,
found
);
}
};
Serialize
and
Deserialize
Binary
Tree
//讲道理这题binary tree和bst一点区别也没有
//就是在重新读string进来的地方,用istringstream会很方便
//其他也没什么trick,就是遍历的问题
class
Codec
{
private:
void
helper
(
TreeNode
*
root
,
string
&
res
){
if
(
!
root
){
res
+=
" #"
;
return
;
}
else
{
res
+=
" "
;
res
+=
to_string
(
root
->
val
);
helper
(
root
->
left
,
res
);
helper
(
root
->
right
,
res
);
}
}
TreeNode
*
de_helper
(
istringstream
&
is
){
string
temp
;
is
>>
temp
;
if
(
temp
==
"#"
){
return
NULL
;
}
TreeNode
*
curr
=
new
TreeNode
(
stoi
(
temp
));
curr
->
left
=
de_helper
(
is
);
curr
->
right
=
de_helper
(
is
);
return
curr
;
}
public:
// Encodes a tree to a single string.
string
serialize
(
TreeNode
*
root
)
{
string
res
;
helper
(
root
,
res
);
//cout<<res<<endl;
return
res
;
}
// Decodes your encoded data to tree.
TreeNode
*
deserialize
(
string
data
)
{
istringstream
in
(
data
);
return
de_helper
(
in
);
}
};
...
...
microsoft.cpp
0 → 100644
View file @
76916012
This diff is collapsed.
Click to expand it.
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