Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
wwwanlingxiao
LeetCodeAnimation
Commits
c1aef7e0
"...src/main/git@ustchcs.com:gujinli1118/eladmin.git" did not exist on "3f80af66414d3148a7ae560bd67c392b3d9292da"
Commit
c1aef7e0
authored
May 02, 2019
by
misterbooo
Browse files
Remove Code
parent
5871d9af
Changes
146
Hide whitespace changes
Inline
Side-by-side
0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution3.java
deleted
100755 → 0
View file @
5871d9af
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
/// Author : liuyubobobo
/// Time : 2018-05-30
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Stack
;
// Non-Recursive
// Using a tag to record whether the node has been visited
//
// Time Complexity: O(n), n is the node number in the tree
// Space Complexity: O(h), h is the height of the tree
public
class
Solution3
{
private
class
TagNode
{
TreeNode
node
;
boolean
isFirst
;
TagNode
(
TreeNode
node
){
this
.
node
=
node
;
this
.
isFirst
=
false
;
}
};
public
List
<
Integer
>
postorderTraversal
(
TreeNode
root
)
{
ArrayList
<
Integer
>
res
=
new
ArrayList
<
Integer
>();
if
(
root
==
null
)
return
res
;
Stack
<
TagNode
>
stack
=
new
Stack
<>();
TreeNode
cur
=
root
;
while
(
cur
!=
null
||
!
stack
.
empty
()){
while
(
cur
!=
null
){
stack
.
push
(
new
TagNode
(
cur
));
cur
=
cur
.
left
;
}
TagNode
tagNode
=
stack
.
pop
();
cur
=
tagNode
.
node
;
if
(
tagNode
.
isFirst
==
false
){
tagNode
.
isFirst
=
true
;
stack
.
push
(
tagNode
);
cur
=
cur
.
right
;
}
else
{
res
.
add
(
cur
.
val
);
cur
=
null
;
}
}
return
res
;
}
}
0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution4.java
deleted
100755 → 0
View file @
5871d9af
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
/// Author : liuyubobobo
/// Time : 2018-05-30
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Stack
;
// Non-Recursive
// Using two stacks, Reverse the Preorder Traversal!
//
// Time Complexity: O(n)
// Space Complexity: O(n)
public
class
Solution4
{
public
List
<
Integer
>
postorderTraversal
(
TreeNode
root
)
{
ArrayList
<
Integer
>
res
=
new
ArrayList
<
Integer
>();
if
(
root
==
null
)
return
res
;
Stack
<
TreeNode
>
stack
=
new
Stack
<>();
Stack
<
Integer
>
output
=
new
Stack
<>();
stack
.
push
(
root
);
while
(!
stack
.
empty
()){
TreeNode
cur
=
stack
.
pop
();
output
.
push
(
cur
.
val
);
if
(
cur
.
left
!=
null
)
stack
.
push
(
cur
.
left
);
if
(
cur
.
right
!=
null
)
stack
.
push
(
cur
.
right
);
}
while
(!
output
.
empty
())
res
.
add
(
output
.
pop
());
return
res
;
}
}
0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution5.java
deleted
100755 → 0
View file @
5871d9af
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
/// Author : liuyubobobo
/// Time : 2018-05-30
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Stack
;
import
java.util.LinkedList
;
// Non-Recursive
// Using two stacks, Reverse the Preorder Traversal!
//
// Time Complexity: O(n)
// Space Complexity: O(n)
public
class
Solution5
{
public
List
<
Integer
>
postorderTraversal
(
TreeNode
root
){
Stack
<
TreeNode
>
stack
=
new
Stack
<>();
LinkedList
<
TreeNode
>
output
=
new
LinkedList
<>();
TreeNode
p
=
root
;
while
(
p
!=
null
||
!
stack
.
isEmpty
()){
if
(
p
!=
null
){
stack
.
push
(
p
);
output
.
push
(
p
);
p
=
p
.
right
;
}
else
{
p
=
stack
.
pop
();
p
=
p
.
left
;
}
}
List
<
Integer
>
res
=
new
ArrayList
<>();
while
(!
output
.
isEmpty
())
res
.
add
(
output
.
pop
().
val
);
return
res
;
}
}
0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution6.java
deleted
100755 → 0
View file @
5871d9af
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
/// Author : liuyubobobo
/// Time : 2018-05-31
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Stack
;
// Non-Recursive
// Using a pre pointer to record the last visted node
//
// Time Complexity: O(n)
// Space Complexity: O(h)
public
class
Solution6
{
public
List
<
Integer
>
postorderTraversal
(
TreeNode
root
)
{
ArrayList
<
Integer
>
res
=
new
ArrayList
<
Integer
>();
if
(
root
==
null
)
return
res
;
Stack
<
TreeNode
>
stack
=
new
Stack
<>();
TreeNode
pre
=
null
;
stack
.
push
(
root
);
while
(!
stack
.
empty
()){
TreeNode
cur
=
stack
.
pop
();
if
((
cur
.
left
==
null
&&
cur
.
right
==
null
)
||
(
pre
!=
null
&&
pre
==
cur
.
left
&&
cur
.
right
==
null
)
||
(
pre
!=
null
&&
pre
==
cur
.
right
)){
res
.
add
(
cur
.
val
);
pre
=
cur
;
}
else
{
stack
.
push
(
cur
);
if
(
cur
.
right
!=
null
)
stack
.
push
(
cur
.
right
);
if
(
cur
.
left
!=
null
)
stack
.
push
(
cur
.
left
);
}
}
return
res
;
}
}
0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution7.java
deleted
100755 → 0
View file @
5871d9af
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
/// Author : liuyubobobo
/// Time : 2018-05-31
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Stack
;
// Classic Non-Recursive
// Using a pre pointer to record the last visted node
//
// Time Complexity: O(n)
// Space Complexity: O(h)
public
class
Solution7
{
public
List
<
Integer
>
postorderTraversal
(
TreeNode
root
)
{
ArrayList
<
Integer
>
res
=
new
ArrayList
<
Integer
>();
if
(
root
==
null
)
return
res
;
Stack
<
TreeNode
>
stack
=
new
Stack
<>();
TreeNode
pre
=
null
;
TreeNode
cur
=
root
;
while
(
cur
!=
null
||
!
stack
.
empty
()){
while
(
cur
!=
null
){
stack
.
push
(
cur
);
cur
=
cur
.
left
;
}
cur
=
stack
.
pop
();
if
(
cur
.
right
==
null
||
pre
==
cur
.
right
){
res
.
add
(
cur
.
val
);
pre
=
cur
;
cur
=
null
;
}
else
{
stack
.
push
(
cur
);
cur
=
cur
.
right
;
}
}
return
res
;
}
}
0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution8.java
deleted
100755 → 0
View file @
5871d9af
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
/// Author : liuyubobobo
/// Time : 2018-05-31
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Stack
;
// Classic Non-Recursive
// Using a pre pointer to record the last visted node
//
// Time Complexity: O(n)
// Space Complexity: O(h)
public
class
Solution8
{
public
List
<
Integer
>
postorderTraversal
(
TreeNode
root
)
{
ArrayList
<
Integer
>
res
=
new
ArrayList
<
Integer
>();
if
(
root
==
null
)
return
res
;
Stack
<
TreeNode
>
stack
=
new
Stack
<>();
TreeNode
pre
=
null
;
TreeNode
cur
=
root
;
while
(
cur
!=
null
||
!
stack
.
empty
()){
if
(
cur
!=
null
){
stack
.
push
(
cur
);
cur
=
cur
.
left
;
}
else
{
cur
=
stack
.
pop
();
if
(
cur
.
right
==
null
||
pre
==
cur
.
right
){
res
.
add
(
cur
.
val
);
pre
=
cur
;
cur
=
null
;
}
else
{
stack
.
push
(
cur
);
cur
=
cur
.
right
;
}
}
}
return
res
;
}
}
0145-Binary-Tree-Postorder-Traversal/java-0145/src/Solution9.java
deleted
100755 → 0
View file @
5871d9af
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description/
/// Author : liuyubobobo
/// Time : 2018-05-31
import
java.util.ArrayList
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.Stack
;
// Morris PostOrder Traversal
//
// Time Complexity: O(n)
// Space Complexity: O(1)
public
class
Solution9
{
public
List
<
Integer
>
postorderTraversal
(
TreeNode
root
)
{
ArrayList
<
Integer
>
res
=
new
ArrayList
<
Integer
>();
if
(
root
==
null
)
return
res
;
TreeNode
dummyRoot
=
new
TreeNode
(-
1
);
dummyRoot
.
left
=
root
;
TreeNode
cur
=
dummyRoot
;
while
(
cur
!=
null
){
if
(
cur
.
left
==
null
)
cur
=
cur
.
right
;
else
{
TreeNode
pre
=
cur
.
left
;
while
(
pre
.
right
!=
null
&&
pre
.
right
!=
cur
)
pre
=
pre
.
right
;
if
(
pre
.
right
==
null
){
pre
.
right
=
cur
;
cur
=
cur
.
left
;
}
else
{
pre
.
right
=
null
;
reverseTraversal
(
cur
.
left
,
res
);
cur
=
cur
.
right
;
}
}
}
return
res
;
}
private
void
reverseTraversal
(
TreeNode
node
,
ArrayList
<
Integer
>
res
){
int
start
=
res
.
size
();
while
(
node
!=
null
){
res
.
add
(
node
.
val
);
node
=
node
.
right
;
}
int
i
=
start
,
j
=
res
.
size
()
-
1
;
while
(
i
<
j
){
Integer
t
=
res
.
get
(
i
);
res
.
set
(
i
,
res
.
get
(
j
));
res
.
set
(
j
,
t
);
i
++;
j
--;
}
}
}
0145-Binary-Tree-Postorder-Traversal/java-0145/src/TreeNode.java
deleted
100755 → 0
View file @
5871d9af
// Definition for a binary tree node.
public
class
TreeNode
{
int
val
;
TreeNode
left
;
TreeNode
right
;
TreeNode
(
int
x
)
{
val
=
x
;
}
}
\ No newline at end of file
0150-Evaluate-Reverse-Polish-Notation/cpp-0150/CMakeLists.txt
deleted
100755 → 0
View file @
5871d9af
cmake_minimum_required
(
VERSION 3.5
)
project
(
cpp_0150
)
set
(
CMAKE_CXX_FLAGS
"
${
CMAKE_CXX_FLAGS
}
-std=c++11"
)
set
(
SOURCE_FILES main.cpp
)
add_executable
(
cpp_0150
${
SOURCE_FILES
}
)
\ No newline at end of file
0150-Evaluate-Reverse-Polish-Notation/cpp-0150/main.cpp
deleted
100755 → 0
View file @
5871d9af
/// Source : https://leetcode.com/problems/evaluate-reverse-polish-notation/description/
/// Author : liuyubobobo
/// Time : 2018-08-29
#include
<iostream>
#include
<stack>
#include
<vector>
using
namespace
std
;
/// Two stacks
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class
Solution
{
public:
int
evalRPN
(
vector
<
string
>&
tokens
)
{
stack
<
int
>
nums
;
stack
<
char
>
ops
;
for
(
const
string
&
s
:
tokens
){
if
(
s
==
"+"
||
s
==
"-"
||
s
==
"*"
||
s
==
"/"
){
int
a
=
nums
.
top
();
nums
.
pop
();
int
b
=
nums
.
top
();
nums
.
pop
();
if
(
s
==
"+"
){
nums
.
push
(
b
+
a
);
}
else
if
(
s
==
"-"
){
nums
.
push
(
b
-
a
);
}
else
if
(
s
==
"*"
){
nums
.
push
(
b
*
a
);
}
else
if
(
s
==
"/"
){
nums
.
push
(
b
/
a
);
}
}
else
{
nums
.
push
(
atoi
(
s
.
c_str
()));
}
}
return
nums
.
top
();
}
};
int
main
()
{
return
0
;
}
0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/CMakeLists.txt
deleted
100755 → 0
View file @
5871d9af
cmake_minimum_required
(
VERSION 3.5
)
project
(
04_Two_Sum_II
)
set
(
CMAKE_CXX_FLAGS
"
${
CMAKE_CXX_FLAGS
}
-std=c++11"
)
set
(
SOURCE_FILES main3.cpp
)
add_executable
(
04_Two_Sum_II
${
SOURCE_FILES
}
)
\ No newline at end of file
0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/main.cpp
deleted
100755 → 0
View file @
5871d9af
/// Source : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/
/// Author : liuyubobobo
/// Time : 2017-11-13
#include
<iostream>
#include
<vector>
#include
<cassert>
using
namespace
std
;
// Brute Force
// Time Complexity: O(n^2)
// Space Complexity: O(1)
class
Solution
{
public:
vector
<
int
>
twoSum
(
vector
<
int
>&
numbers
,
int
target
)
{
assert
(
numbers
.
size
()
>=
2
);
// assert(isSorted(numbers));
for
(
int
i
=
0
;
i
<
numbers
.
size
()
;
i
++
)
for
(
int
j
=
i
+
1
;
j
<
numbers
.
size
()
;
j
++
)
if
(
numbers
[
i
]
+
numbers
[
j
]
==
target
){
int
res
[
2
]
=
{
i
+
1
,
j
+
1
};
return
vector
<
int
>
(
res
,
res
+
2
);
}
throw
invalid_argument
(
"the input has no solution"
);
}
private:
bool
isSorted
(
const
vector
<
int
>&
numbers
){
for
(
int
i
=
1
;
i
<
numbers
.
size
()
;
i
++
)
if
(
numbers
[
i
]
<
numbers
[
i
-
1
])
return
false
;
return
true
;
}
};
void
printVec
(
const
vector
<
int
>&
vec
){
for
(
int
e
:
vec
)
cout
<<
e
<<
" "
;
cout
<<
endl
;
}
int
main
()
{
int
nums
[]
=
{
2
,
7
,
11
,
15
};
vector
<
int
>
vec
(
nums
,
nums
+
sizeof
(
nums
)
/
sizeof
(
int
));
int
target
=
9
;
printVec
(
Solution
().
twoSum
(
vec
,
target
));
return
0
;
}
\ No newline at end of file
0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/main2.cpp
deleted
100755 → 0
View file @
5871d9af
/// Source : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/
/// Author : liuyubobobo
/// Time : 2017-11-13
#include
<iostream>
#include
<vector>
#include
<cassert>
using
namespace
std
;
// Binary Search
// Time Complexity: O(nlogn)
// Space Complexity: O(1)
class
Solution
{
public:
vector
<
int
>
twoSum
(
vector
<
int
>&
numbers
,
int
target
)
{
assert
(
numbers
.
size
()
>=
2
);
// assert(isSorted(numbers));
for
(
int
i
=
0
;
i
<
numbers
.
size
()
-
1
;
i
++
){
int
j
=
binarySearch
(
numbers
,
i
+
1
,
numbers
.
size
()
-
1
,
target
-
numbers
[
i
]);
if
(
j
!=
-
1
){
int
res
[
2
]
=
{
i
+
1
,
j
+
1
};
return
vector
<
int
>
(
res
,
res
+
2
);
}
}
throw
invalid_argument
(
"the input has no solution"
);
}
private:
int
binarySearch
(
const
vector
<
int
>
&
nums
,
int
l
,
int
r
,
int
target
){
assert
(
l
>=
0
&&
l
<
nums
.
size
());
assert
(
r
>=
0
&&
r
<
nums
.
size
());
while
(
l
<=
r
){
int
mid
=
l
+
(
r
-
l
)
/
2
;
if
(
nums
[
mid
]
==
target
)
return
mid
;
if
(
target
>
nums
[
mid
])
l
=
mid
+
1
;
else
r
=
mid
-
1
;
}
return
-
1
;
}
bool
isSorted
(
const
vector
<
int
>&
numbers
){
for
(
int
i
=
1
;
i
<
numbers
.
size
()
;
i
++
)
if
(
numbers
[
i
]
<
numbers
[
i
-
1
])
return
false
;
return
true
;
}
};
void
printVec
(
const
vector
<
int
>&
vec
){
for
(
int
e
:
vec
)
cout
<<
e
<<
" "
;
cout
<<
endl
;
}
int
main
()
{
int
nums
[]
=
{
2
,
7
,
11
,
15
};
vector
<
int
>
vec
(
nums
,
nums
+
sizeof
(
nums
)
/
sizeof
(
int
));
int
target
=
9
;
printVec
(
Solution
().
twoSum
(
vec
,
target
));
return
0
;
}
\ No newline at end of file
0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/main3.cpp
deleted
100755 → 0
View file @
5871d9af
/// Source : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/
/// Author : liuyubobobo
/// Time : 2017-11-13
#include
<iostream>
#include
<vector>
#include
<cassert>
using
namespace
std
;
// Two Pointers
// Time Complexity: O(n)
// Space Complexity: O(1)
class
Solution
{
public:
vector
<
int
>
twoSum
(
vector
<
int
>&
numbers
,
int
target
)
{
assert
(
numbers
.
size
()
>=
2
);
// assert(isSorted(numbers));
int
l
=
0
,
r
=
numbers
.
size
()
-
1
;
while
(
l
<
r
){
if
(
numbers
[
l
]
+
numbers
[
r
]
==
target
){
int
res
[
2
]
=
{
l
+
1
,
r
+
1
};
return
vector
<
int
>
(
res
,
res
+
2
);
}
else
if
(
numbers
[
l
]
+
numbers
[
r
]
<
target
)
l
++
;
else
// numbers[l] + numbers[r] > target
r
--
;
}
throw
invalid_argument
(
"the input has no solution"
);
}
private:
bool
isSorted
(
const
vector
<
int
>&
numbers
){
for
(
int
i
=
1
;
i
<
numbers
.
size
()
;
i
++
)
if
(
numbers
[
i
]
<
numbers
[
i
-
1
])
return
false
;
return
true
;
}
};
void
printVec
(
const
vector
<
int
>&
vec
){
for
(
int
e
:
vec
)
cout
<<
e
<<
" "
;
cout
<<
endl
;
}
int
main
()
{
int
nums
[]
=
{
2
,
7
,
11
,
15
};
vector
<
int
>
vec
(
nums
,
nums
+
sizeof
(
nums
)
/
sizeof
(
int
));
int
target
=
9
;
printVec
(
Solution
().
twoSum
(
vec
,
target
));
return
0
;
}
\ No newline at end of file
0167-Two-Sum-II-Input-array-is-sorted/java-0167/src/Solution1.java
deleted
100755 → 0
View file @
5871d9af
/// Source : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/
/// Author : liuyubobobo
/// Time : 2017-11-13
// Brute Force
// Time Complexity: O(n^2)
// Space Complexity: O(1)
public
class
Solution1
{
public
int
[]
twoSum
(
int
[]
numbers
,
int
target
)
{
if
(
numbers
.
length
<
2
/*|| !isSorted(numbers)*/
)
throw
new
IllegalArgumentException
(
"Illegal argument numbers"
);
for
(
int
i
=
0
;
i
<
numbers
.
length
;
i
++)
for
(
int
j
=
i
+
1
;
j
<
numbers
.
length
;
j
++)
if
(
numbers
[
i
]
+
numbers
[
j
]
==
target
){
int
[]
res
=
{
i
+
1
,
j
+
1
};
return
res
;
}
throw
new
IllegalStateException
(
"The input has no solution"
);
}
private
boolean
isSorted
(
int
[]
numbers
){
for
(
int
i
=
1
;
i
<
numbers
.
length
;
i
++)
if
(
numbers
[
i
]
<
numbers
[
i
-
1
])
return
false
;
return
true
;
}
private
static
void
printArr
(
int
[]
nums
){
for
(
int
num:
nums
)
System
.
out
.
print
(
num
+
" "
);
System
.
out
.
println
();
}
public
static
void
main
(
String
[]
args
)
{
int
[]
nums
=
{
2
,
7
,
11
,
15
};
int
target
=
9
;
printArr
((
new
Solution1
()).
twoSum
(
nums
,
target
));
}
}
0167-Two-Sum-II-Input-array-is-sorted/java-0167/src/Solution2.java
deleted
100755 → 0
View file @
5871d9af
/// Source : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/
/// Author : liuyubobobo
/// Time : 2017-11-13
// Binary Search
// Time Complexity: O(nlogn)
// Space Complexity: O(1)
public
class
Solution2
{
public
int
[]
twoSum
(
int
[]
numbers
,
int
target
)
{
if
(
numbers
.
length
<
2
/*|| !isSorted(numbers)*/
)
throw
new
IllegalArgumentException
(
"Illegal argument numbers"
);
for
(
int
i
=
0
;
i
<
numbers
.
length
-
1
;
i
++){
int
j
=
binarySearch
(
numbers
,
i
+
1
,
numbers
.
length
-
1
,
target
-
numbers
[
i
]);
if
(
j
!=
-
1
){
int
[]
res
=
{
i
+
1
,
j
+
1
};
return
res
;
}
}
throw
new
IllegalStateException
(
"The input has no solution"
);
}
private
int
binarySearch
(
int
[]
nums
,
int
l
,
int
r
,
int
target
){
if
(
l
<
0
||
l
>
nums
.
length
)
throw
new
IllegalArgumentException
(
"l is out of bound"
);
if
(
r
<
0
||
r
>
nums
.
length
)
throw
new
IllegalArgumentException
(
"r is out of bound"
);
while
(
l
<=
r
){
int
mid
=
l
+
(
r
-
l
)/
2
;
if
(
nums
[
mid
]
==
target
)
return
mid
;
if
(
target
>
nums
[
mid
])
l
=
mid
+
1
;
else
r
=
mid
-
1
;
}
return
-
1
;
}
private
boolean
isSorted
(
int
[]
numbers
){
for
(
int
i
=
1
;
i
<
numbers
.
length
;
i
++)
if
(
numbers
[
i
]
<
numbers
[
i
-
1
])
return
false
;
return
true
;
}
private
static
void
printArr
(
int
[]
nums
){
for
(
int
num:
nums
)
System
.
out
.
print
(
num
+
" "
);
System
.
out
.
println
();
}
public
static
void
main
(
String
[]
args
)
{
int
[]
nums
=
{
2
,
7
,
11
,
15
};
int
target
=
9
;
printArr
((
new
Solution2
()).
twoSum
(
nums
,
target
));
}
}
0167-Two-Sum-II-Input-array-is-sorted/java-0167/src/Solution3.java
deleted
100755 → 0
View file @
5871d9af
/// Source : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/
/// Author : liuyubobobo
/// Time : 2017-11-13
// Two Pointers
// Time Complexity: O(n)
// Space Complexity: O(1)
public
class
Solution3
{
public
int
[]
twoSum
(
int
[]
numbers
,
int
target
)
{
if
(
numbers
.
length
<
2
/*|| !isSorted(numbers)*/
)
throw
new
IllegalArgumentException
(
"Illegal argument numbers"
);
int
l
=
0
,
r
=
numbers
.
length
-
1
;
while
(
l
<
r
){
if
(
numbers
[
l
]
+
numbers
[
r
]
==
target
){
int
[]
res
=
{
l
+
1
,
r
+
1
};
return
res
;
}
else
if
(
numbers
[
l
]
+
numbers
[
r
]
<
target
)
l
++;
else
// numbers[l] + numbers[r] > target
r
--;
}
throw
new
IllegalStateException
(
"The input has no solution"
);
}
private
boolean
isSorted
(
int
[]
numbers
){
for
(
int
i
=
1
;
i
<
numbers
.
length
;
i
++)
if
(
numbers
[
i
]
<
numbers
[
i
-
1
])
return
false
;
return
true
;
}
private
static
void
printArr
(
int
[]
nums
){
for
(
int
num:
nums
)
System
.
out
.
print
(
num
+
" "
);
System
.
out
.
println
();
}
public
static
void
main
(
String
[]
args
)
{
int
[]
nums
=
{
2
,
7
,
11
,
15
};
int
target
=
9
;
printArr
((
new
Solution3
()).
twoSum
(
nums
,
target
));
}
}
0203-Remove-Linked-List-Elements/cpp-0203/CMakeLists.txt
deleted
100755 → 0
View file @
5871d9af
cmake_minimum_required
(
VERSION 3.5
)
project
(
cpp_0203
)
set
(
CMAKE_CXX_FLAGS
"
${
CMAKE_CXX_FLAGS
}
-std=c++11"
)
set
(
SOURCE_FILES main2.cpp
)
add_executable
(
cpp_0203
${
SOURCE_FILES
}
)
\ No newline at end of file
0203-Remove-Linked-List-Elements/cpp-0203/main.cpp
deleted
100755 → 0
View file @
5871d9af
/// Source : https://leetcode.com/problems/remove-linked-list-elements/description/
/// Author : liuyubobobo
/// Time : 2017-11-15
#include
<iostream>
using
namespace
std
;
///Definition for singly-linked list.
struct
ListNode
{
int
val
;
ListNode
*
next
;
ListNode
(
int
x
)
:
val
(
x
),
next
(
NULL
)
{}
};
/// LinkedList Test Helper Functions
ListNode
*
createLinkedList
(
int
arr
[],
int
n
){
if
(
n
==
0
)
return
NULL
;
ListNode
*
head
=
new
ListNode
(
arr
[
0
]);
ListNode
*
curNode
=
head
;
for
(
int
i
=
1
;
i
<
n
;
i
++
){
curNode
->
next
=
new
ListNode
(
arr
[
i
]);
curNode
=
curNode
->
next
;
}
return
head
;
}
void
printLinkedList
(
ListNode
*
head
){
if
(
head
==
NULL
){
cout
<<
"NULL"
<<
endl
;
return
;
}
ListNode
*
curNode
=
head
;
while
(
curNode
!=
NULL
){
cout
<<
curNode
->
val
;
if
(
curNode
->
next
!=
NULL
)
cout
<<
" -> "
;
curNode
=
curNode
->
next
;
}
cout
<<
endl
;
return
;
}
void
deleteLinkedList
(
ListNode
*
head
){
ListNode
*
curNode
=
head
;
while
(
curNode
!=
NULL
){
ListNode
*
delNode
=
curNode
;
curNode
=
curNode
->
next
;
delete
delNode
;
}
return
;
}
/// Linear Scan with dummy head
/// Time Complexity: O(n)
/// Space Complexity: O(1)
class
Solution
{
public:
ListNode
*
removeElements
(
ListNode
*
head
,
int
val
)
{
ListNode
*
dummyHead
=
new
ListNode
(
0
);
dummyHead
->
next
=
head
;
ListNode
*
cur
=
dummyHead
;
while
(
cur
->
next
!=
NULL
){
if
(
cur
->
next
->
val
==
val
){
ListNode
*
delNode
=
cur
->
next
;
cur
->
next
=
delNode
->
next
;
delete
delNode
;
}
else
cur
=
cur
->
next
;
}
ListNode
*
retNode
=
dummyHead
->
next
;
delete
dummyHead
;
return
retNode
;
}
};
int
main
()
{
int
arr
[]
=
{
1
,
2
,
6
,
3
,
4
,
5
,
6
};
int
n
=
sizeof
(
arr
)
/
sizeof
(
int
);
ListNode
*
head
=
createLinkedList
(
arr
,
n
);
printLinkedList
(
head
);
Solution
().
removeElements
(
head
,
6
);
printLinkedList
(
head
);
deleteLinkedList
(
head
);
return
0
;
}
\ No newline at end of file
0203-Remove-Linked-List-Elements/cpp-0203/main2.cpp
deleted
100755 → 0
View file @
5871d9af
/// Source : https://leetcode.com/problems/remove-linked-list-elements/description/
/// Author : liuyubobobo
/// Time : 2018-09-17
#include
<iostream>
using
namespace
std
;
///Definition for singly-linked list.
struct
ListNode
{
int
val
;
ListNode
*
next
;
ListNode
(
int
x
)
:
val
(
x
),
next
(
NULL
)
{}
};
/// Recursive
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class
Solution
{
public:
ListNode
*
removeElements
(
ListNode
*
head
,
int
val
)
{
if
(
!
head
)
return
head
;
if
(
head
->
val
==
val
){
ListNode
*
node
=
head
->
next
;
delete
head
;
return
removeElements
(
node
,
val
);
}
head
->
next
=
removeElements
(
head
->
next
,
val
);
return
head
;
}
};
int
main
()
{
return
0
;
}
\ No newline at end of file
Prev
1
2
3
4
5
6
7
8
Next
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