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
921cc799
Commit
921cc799
authored
Dec 07, 2018
by
MisterBigbooo
Browse files
添加仓库代码
parent
4e5b4643
Changes
137
Hide whitespace changes
Inline
Side-by-side
.gitattributes
deleted
100644 → 0
View file @
4e5b4643
*.html linguist-language=java
0001-Two-Sum/cpp-0001/CMakeLists.txt
0 → 100755
View file @
921cc799
cmake_minimum_required
(
VERSION 3.5
)
project
(
cpp_0001
)
set
(
CMAKE_CXX_FLAGS
"
${
CMAKE_CXX_FLAGS
}
-std=c++11"
)
set
(
SOURCE_FILES main2.cpp
)
add_executable
(
cpp_0001
${
SOURCE_FILES
}
)
\ No newline at end of file
0001-Two-Sum/cpp-0001/main.cpp
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/two-sum/description/
/// Author : liuyubobobo
/// Time : 2017-11-15
#include
<iostream>
#include
<vector>
using
namespace
std
;
/// Brute Force
/// Time Complexity: O(n^2)
/// Space Complexity: O(1)
class
Solution
{
public:
vector
<
int
>
twoSum
(
vector
<
int
>&
nums
,
int
target
)
{
for
(
int
i
=
0
;
i
<
nums
.
size
()
;
i
++
)
for
(
int
j
=
i
+
1
;
j
<
nums
.
size
()
;
j
++
)
if
(
nums
[
i
]
+
nums
[
j
]
==
target
){
int
res
[]
=
{
i
,
j
};
return
vector
<
int
>
(
res
,
res
+
2
);
}
throw
invalid_argument
(
"the input has no solution"
);
}
};
void
printVec
(
const
vector
<
int
>&
vec
){
for
(
int
e
:
vec
)
cout
<<
e
<<
" "
;
cout
<<
endl
;
}
int
main
()
{
const
int
nums
[]
=
{
0
,
4
,
3
,
0
};
vector
<
int
>
nums_vec
(
nums
,
nums
+
sizeof
(
nums
)
/
sizeof
(
int
)
);
int
target
=
0
;
printVec
(
Solution
().
twoSum
(
nums_vec
,
target
));
return
0
;
}
\ No newline at end of file
0001-Two-Sum/cpp-0001/main2.cpp
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/two-sum/description/
/// Author : liuyubobobo
/// Time : 2017-11-15
#include
<iostream>
#include
<vector>
#include
<cassert>
#include
<unordered_map>
using
namespace
std
;
/// Two-Pass Hash Table
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class
Solution
{
public:
vector
<
int
>
twoSum
(
vector
<
int
>&
nums
,
int
target
)
{
unordered_map
<
int
,
int
>
record
;
for
(
int
i
=
0
;
i
<
nums
.
size
()
;
i
++
)
record
[
nums
[
i
]]
=
i
;
for
(
int
i
=
0
;
i
<
nums
.
size
()
;
i
++
){
unordered_map
<
int
,
int
>::
iterator
iter
=
record
.
find
(
target
-
nums
[
i
]);
if
(
iter
!=
record
.
end
()
&&
iter
->
second
!=
i
){
int
res
[]
=
{
i
,
iter
->
second
};
return
vector
<
int
>
(
res
,
res
+
2
);
}
}
throw
invalid_argument
(
"the input has no solution"
);
}
};
void
printVec
(
const
vector
<
int
>&
vec
){
for
(
int
e
:
vec
)
cout
<<
e
<<
" "
;
cout
<<
endl
;
}
int
main
()
{
const
int
nums
[]
=
{
0
,
4
,
3
,
0
};
vector
<
int
>
nums_vec
(
nums
,
nums
+
sizeof
(
nums
)
/
sizeof
(
int
)
);
int
target
=
0
;
printVec
(
Solution
().
twoSum
(
nums_vec
,
target
));
return
0
;
}
\ No newline at end of file
0001-Two-Sum/cpp-0001/main3.cpp
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/two-sum/description/
/// Author : liuyubobobo
/// Time : 2017-11-15
#include
<iostream>
#include
<vector>
#include
<cassert>
#include
<unordered_map>
using
namespace
std
;
/// One-Pass Hash Table
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class
Solution
{
public:
vector
<
int
>
twoSum
(
vector
<
int
>&
nums
,
int
target
)
{
unordered_map
<
int
,
int
>
record
;
for
(
int
i
=
0
;
i
<
nums
.
size
()
;
i
++
){
int
complement
=
target
-
nums
[
i
];
if
(
record
.
find
(
complement
)
!=
record
.
end
()){
int
res
[]
=
{
i
,
record
[
complement
]};
return
vector
<
int
>
(
res
,
res
+
2
);
}
record
[
nums
[
i
]]
=
i
;
}
throw
invalid_argument
(
"the input has no solution"
);
}
};
void
printVec
(
const
vector
<
int
>&
vec
){
for
(
int
e
:
vec
)
cout
<<
e
<<
" "
;
cout
<<
endl
;
}
int
main
()
{
const
int
nums
[]
=
{
0
,
4
,
3
,
0
};
vector
<
int
>
nums_vec
(
nums
,
nums
+
sizeof
(
nums
)
/
sizeof
(
int
)
);
int
target
=
0
;
printVec
(
Solution
().
twoSum
(
nums_vec
,
target
));
return
0
;
}
\ No newline at end of file
0001-Two-Sum/java-0001/src/Solution1.java
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/two-sum/description/
/// Author : liuyubobobo
/// Time : 2017-11-15
import
java.util.HashMap
;
/// Brute Force
/// Time Complexity: O(n^2)
/// Space Complexity: O(1)
public
class
Solution1
{
public
int
[]
twoSum
(
int
[]
nums
,
int
target
)
{
for
(
int
i
=
0
;
i
<
nums
.
length
;
i
++)
for
(
int
j
=
0
;
j
<
nums
.
length
;
j
++)
if
(
nums
[
i
]
+
nums
[
j
]
==
target
){
int
[]
res
=
{
i
,
j
};
return
res
;
}
throw
new
IllegalStateException
(
"the input has no solution"
);
}
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
=
{
0
,
4
,
3
,
0
};
int
target
=
0
;
printArr
((
new
Solution1
()).
twoSum
(
nums
,
target
));
}
}
0001-Two-Sum/java-0001/src/Solution2.java
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/two-sum/description/
/// Author : liuyubobobo
/// Time : 2017-11-15
import
java.util.HashMap
;
/// Two-Pass Hash Table
/// Time Complexity: O(n)
/// Space Complexity: O(n)
public
class
Solution2
{
public
int
[]
twoSum
(
int
[]
nums
,
int
target
)
{
HashMap
<
Integer
,
Integer
>
record
=
new
HashMap
<
Integer
,
Integer
>();
for
(
int
i
=
0
;
i
<
nums
.
length
;
i
++)
record
.
put
(
nums
[
i
],
i
);
for
(
int
i
=
0
;
i
<
nums
.
length
;
i
++){
Integer
index
=
record
.
get
(
target
-
nums
[
i
]);
if
(
index
!=
null
&&
index
!=
i
){
int
[]
res
=
{
i
,
index
};
return
res
;
}
record
.
put
(
nums
[
i
],
i
);
}
throw
new
IllegalStateException
(
"the input has no solution"
);
}
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
=
{
0
,
4
,
3
,
0
};
int
target
=
0
;
printArr
((
new
Solution2
()).
twoSum
(
nums
,
target
));
}
}
0001-Two-Sum/java-0001/src/Solution3.java
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/two-sum/description/
/// Author : liuyubobobo
/// Time : 2017-11-15
import
java.util.HashMap
;
/// One-Pass Hash Table
/// Time Complexity: O(n)
/// Space Complexity: O(n)
public
class
Solution3
{
public
int
[]
twoSum
(
int
[]
nums
,
int
target
)
{
HashMap
<
Integer
,
Integer
>
record
=
new
HashMap
<
Integer
,
Integer
>();
for
(
int
i
=
0
;
i
<
nums
.
length
;
i
++){
int
complement
=
target
-
nums
[
i
];
if
(
record
.
containsKey
(
complement
)){
int
[]
res
=
{
i
,
record
.
get
(
complement
)};
return
res
;
}
record
.
put
(
nums
[
i
],
i
);
}
throw
new
IllegalStateException
(
"the input has no solution"
);
}
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
=
{
0
,
4
,
3
,
0
};
int
target
=
0
;
printArr
((
new
Solution3
()).
twoSum
(
nums
,
target
));
}
}
0019-Remove-Nth-Node-From-End-of-List/cpp-0019/CMakeLists.txt
0 → 100755
View file @
921cc799
cmake_minimum_required
(
VERSION 3.5
)
project
(
cpp_0019
)
set
(
CMAKE_CXX_FLAGS
"
${
CMAKE_CXX_FLAGS
}
-std=c++11"
)
set
(
SOURCE_FILES main.cpp
)
add_executable
(
cpp_0019
${
SOURCE_FILES
}
)
\ No newline at end of file
0019-Remove-Nth-Node-From-End-of-List/cpp-0019/main.cpp
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/
/// Author : liuyubobobo
/// Time : 2017-11-15
#include
<iostream>
#include
<cassert>
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
;
}
/// Get the total length and remove the nth node
/// Two Pass Algorithm
///
/// Time Complexity: O(n)
/// Space Complexity: O(1)
class
Solution
{
public:
ListNode
*
removeNthFromEnd
(
ListNode
*
head
,
int
n
)
{
ListNode
*
dummyHead
=
new
ListNode
(
0
);
dummyHead
->
next
=
head
;
int
length
=
0
;
for
(
ListNode
*
cur
=
dummyHead
->
next
;
cur
!=
NULL
;
cur
=
cur
->
next
)
length
++
;
int
k
=
length
-
n
;
assert
(
k
>=
0
);
ListNode
*
cur
=
dummyHead
;
for
(
int
i
=
0
;
i
<
k
;
i
++
)
cur
=
cur
->
next
;
ListNode
*
delNode
=
cur
->
next
;
cur
->
next
=
delNode
->
next
;
delete
delNode
;
ListNode
*
retNode
=
dummyHead
->
next
;
delete
dummyHead
;
return
retNode
;
}
};
int
main
()
{
int
arr
[]
=
{
1
,
2
,
3
,
4
,
5
};
int
n
=
sizeof
(
arr
)
/
sizeof
(
int
);
ListNode
*
head
=
createLinkedList
(
arr
,
n
);
printLinkedList
(
head
);
head
=
Solution
().
removeNthFromEnd
(
head
,
2
);
printLinkedList
(
head
);
deleteLinkedList
(
head
);
return
0
;
}
\ No newline at end of file
0019-Remove-Nth-Node-From-End-of-List/cpp-0019/main2.cpp
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/
/// Author : liuyubobobo
/// Time : 2017-11-15
#include
<iostream>
#include
<cassert>
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
;
}
/// Two Pointers - One Pass Algorithm
/// Time Complexity: O(n)
/// Space Complexity: O(1)
class
Solution
{
public:
ListNode
*
removeNthFromEnd
(
ListNode
*
head
,
int
n
)
{
ListNode
*
dummyHead
=
new
ListNode
(
0
);
dummyHead
->
next
=
head
;
ListNode
*
p
=
dummyHead
;
ListNode
*
q
=
dummyHead
;
for
(
int
i
=
0
;
i
<
n
+
1
;
i
++
){
assert
(
q
);
q
=
q
->
next
;
}
while
(
q
){
p
=
p
->
next
;
q
=
q
->
next
;
}
ListNode
*
delNode
=
p
->
next
;
p
->
next
=
delNode
->
next
;
delete
delNode
;
ListNode
*
retNode
=
dummyHead
->
next
;
delete
dummyHead
;
return
retNode
;
}
};
int
main
()
{
int
arr
[]
=
{
1
,
2
,
3
,
4
,
5
};
int
n
=
sizeof
(
arr
)
/
sizeof
(
int
);
ListNode
*
head
=
createLinkedList
(
arr
,
n
);
printLinkedList
(
head
);
head
=
Solution
().
removeNthFromEnd
(
head
,
2
);
printLinkedList
(
head
);
deleteLinkedList
(
head
);
return
0
;
}
\ No newline at end of file
0019-Remove-Nth-Node-From-End-of-List/java-0019/src/ListNode.java
0 → 100755
View file @
921cc799
// Definition for singly-linked list.
// 在Java版本中,我们将LinkedList相关的测试辅助函数写在ListNode里
public
class
ListNode
{
public
int
val
;
public
ListNode
next
=
null
;
public
ListNode
(
int
x
)
{
val
=
x
;
}
// 根据n个元素的数组arr创建一个链表
// 使用arr为参数,创建另外一个ListNode的构造函数
public
ListNode
(
int
[]
arr
){
if
(
arr
==
null
||
arr
.
length
==
0
)
throw
new
IllegalArgumentException
(
"arr can not be empty"
);
this
.
val
=
arr
[
0
];
ListNode
curNode
=
this
;
for
(
int
i
=
1
;
i
<
arr
.
length
;
i
++){
curNode
.
next
=
new
ListNode
(
arr
[
i
]);
curNode
=
curNode
.
next
;
}
}
ListNode
findNode
(
int
x
){
ListNode
curNode
=
this
;
while
(
curNode
!=
null
){
if
(
curNode
.
val
==
x
)
return
curNode
;
curNode
=
curNode
.
next
;
}
return
null
;
}
// 返回以当前ListNode为头结点的链表信息字符串
@Override
public
String
toString
(){
StringBuilder
s
=
new
StringBuilder
(
""
);
ListNode
curNode
=
this
;
while
(
curNode
!=
null
){
s
.
append
(
Integer
.
toString
(
curNode
.
val
));
s
.
append
(
" -> "
);
curNode
=
curNode
.
next
;
}
s
.
append
(
"NULL"
);
return
s
.
toString
();
}
}
\ No newline at end of file
0019-Remove-Nth-Node-From-End-of-List/java-0019/src/Solution1.java
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/
/// Author : liuyubobobo
/// Time : 2017-11-15
//
// Time Complexity: O(n)
// Space Complexity: O(1)
public
class
Solution1
{
public
ListNode
removeNthFromEnd
(
ListNode
head
,
int
n
)
{
ListNode
dummyHead
=
new
ListNode
(
0
);
dummyHead
.
next
=
head
;
int
length
=
0
;
for
(
ListNode
cur
=
dummyHead
.
next
;
cur
!=
null
;
cur
=
cur
.
next
)
length
++;
int
k
=
length
-
n
;
assert
k
>=
0
;
ListNode
cur
=
dummyHead
;
for
(
int
i
=
0
;
i
<
k
;
i
++)
cur
=
cur
.
next
;
cur
.
next
=
cur
.
next
.
next
;
return
dummyHead
.
next
;
}
public
static
void
main
(
String
[]
args
)
{
int
arr
[]
=
{
1
,
2
,
3
,
4
,
5
};
ListNode
head
=
new
ListNode
(
arr
);
System
.
out
.
println
(
head
);
head
=
(
new
Solution1
()).
removeNthFromEnd
(
head
,
2
);
System
.
out
.
println
(
head
);
}
}
0019-Remove-Nth-Node-From-End-of-List/java-0019/src/Solution2.java
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/
/// Author : liuyubobobo
/// Time : 2017-11-15
//
// Two Pointers - One Pass Algorithm
// Time Complexity: O(n)
// Space Complexity: O(1)
public
class
Solution2
{
public
ListNode
removeNthFromEnd
(
ListNode
head
,
int
n
)
{
ListNode
dummyHead
=
new
ListNode
(
0
);
dummyHead
.
next
=
head
;
ListNode
p
=
dummyHead
;
ListNode
q
=
dummyHead
;
for
(
int
i
=
0
;
i
<
n
+
1
;
i
++
){
assert
q
!=
null
;
q
=
q
.
next
;
}
while
(
q
!=
null
){
p
=
p
.
next
;
q
=
q
.
next
;
}
p
.
next
=
p
.
next
.
next
;
return
dummyHead
.
next
;
}
public
static
void
main
(
String
[]
args
)
{
int
arr
[]
=
{
1
,
2
,
3
,
4
,
5
};
ListNode
head
=
new
ListNode
(
arr
);
System
.
out
.
println
(
head
);
head
=
(
new
Solution2
()).
removeNthFromEnd
(
head
,
2
);
System
.
out
.
println
(
head
);
}
}
0020-Valid-Parentheses/cpp-0020/CMakeLists.txt
0 → 100755
View file @
921cc799
cmake_minimum_required
(
VERSION 3.5
)
project
(
cpp_0020
)
set
(
CMAKE_CXX_FLAGS
"
${
CMAKE_CXX_FLAGS
}
-std=c++11"
)
set
(
SOURCE_FILES main.cpp
)
add_executable
(
cpp_0020
${
SOURCE_FILES
}
)
\ No newline at end of file
0020-Valid-Parentheses/cpp-0020/main.cpp
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/valid-parentheses/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
#include
<iostream>
#include
<stack>
#include
<cassert>
using
namespace
std
;
// Using Stack
// Time Complexity: O(n)
// Space Complexity: O(n)
class
Solution
{
public:
bool
isValid
(
string
s
)
{
stack
<
char
>
stack
;
for
(
int
i
=
0
;
i
<
s
.
size
()
;
i
++
)
if
(
s
[
i
]
==
'('
||
s
[
i
]
==
'{'
||
s
[
i
]
==
'['
)
stack
.
push
(
s
[
i
]);
else
{
if
(
stack
.
size
()
==
0
)
return
false
;
char
c
=
stack
.
top
();
stack
.
pop
();
char
match
;
if
(
s
[
i
]
==
')'
)
match
=
'('
;
else
if
(
s
[
i
]
==
']'
)
match
=
'['
;
else
{
assert
(
s
[
i
]
==
'}'
);
match
=
'{'
;
}
if
(
c
!=
match
)
return
false
;
}
if
(
stack
.
size
()
!=
0
)
return
false
;
return
true
;
}
};
void
printBool
(
bool
res
){
cout
<<
(
res
?
"True"
:
"False"
)
<<
endl
;
}
int
main
()
{
printBool
(
Solution
().
isValid
(
"()"
));
printBool
(
Solution
().
isValid
(
"()[]{}"
));
printBool
(
Solution
().
isValid
(
"(]"
));
printBool
(
Solution
().
isValid
(
"([)]"
));
return
0
;
}
\ No newline at end of file
0020-Valid-Parentheses/java-0020/src/Main.java
0 → 100755
View file @
921cc799
public
class
Main
{
public
static
void
main
(
String
[]
args
)
{
// write your code here
}
}
0020-Valid-Parentheses/java-0020/src/Solution.java
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/valid-parentheses/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
import
java.util.Stack
;
// Using Stack
// Time Complexity: O(n)
// Space Complexity: O(n)
public
class
Solution
{
public
boolean
isValid
(
String
s
)
{
Stack
<
Character
>
stack
=
new
Stack
<
Character
>();
for
(
int
i
=
0
;
i
<
s
.
length
()
;
i
++
)
if
(
s
.
charAt
(
i
)
==
'('
||
s
.
charAt
(
i
)
==
'{'
||
s
.
charAt
(
i
)
==
'['
)
stack
.
push
(
s
.
charAt
(
i
));
else
{
if
(
stack
.
size
()
==
0
)
return
false
;
Character
c
=
stack
.
pop
();
Character
match
;
if
(
s
.
charAt
(
i
)
==
')'
)
match
=
'('
;
else
if
(
s
.
charAt
(
i
)
==
']'
)
match
=
'['
;
else
{
assert
s
.
charAt
(
i
)
==
'}'
;
match
=
'{'
;
}
if
(
c
!=
match
)
return
false
;
}
if
(
stack
.
size
()
!=
0
)
return
false
;
return
true
;
}
private
static
void
printBool
(
boolean
b
){
System
.
out
.
println
(
b
?
"True"
:
"False"
);
}
public
static
void
main
(
String
[]
args
)
{
printBool
((
new
Solution
()).
isValid
(
"()"
));
printBool
((
new
Solution
()).
isValid
(
"()[]{}"
));
printBool
((
new
Solution
()).
isValid
(
"(]"
));
printBool
((
new
Solution
()).
isValid
(
"([)]"
));
}
}
0024-Swap-Nodes-in-Pairs/cpp-0024/CMakeLists.txt
0 → 100755
View file @
921cc799
cmake_minimum_required
(
VERSION 3.5
)
project
(
cpp_0024
)
set
(
CMAKE_CXX_FLAGS
"
${
CMAKE_CXX_FLAGS
}
-std=c++11"
)
set
(
SOURCE_FILES main.cpp
)
add_executable
(
cpp_0024
${
SOURCE_FILES
}
)
\ No newline at end of file
0024-Swap-Nodes-in-Pairs/cpp-0024/main.cpp
0 → 100755
View file @
921cc799
/// Source : https://leetcode.com/problems/swap-nodes-in-pairs/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
;
}
// Time Complexity: O(n)
// Space Complexity: O(1)
class
Solution
{
public:
ListNode
*
swapPairs
(
ListNode
*
head
)
{
ListNode
*
dummyHead
=
new
ListNode
(
0
);
dummyHead
->
next
=
head
;
ListNode
*
p
=
dummyHead
;
while
(
p
->
next
&&
p
->
next
->
next
){
ListNode
*
node1
=
p
->
next
;
ListNode
*
node2
=
node1
->
next
;
ListNode
*
next
=
node2
->
next
;
node2
->
next
=
node1
;
node1
->
next
=
next
;
p
->
next
=
node2
;
p
=
node1
;
}
ListNode
*
retHead
=
dummyHead
->
next
;
delete
dummyHead
;
return
retHead
;
}
};
int
main
()
{
int
arr
[]
=
{
1
,
2
,
3
,
4
};
int
n
=
sizeof
(
arr
)
/
sizeof
(
int
);
ListNode
*
head
=
createLinkedList
(
arr
,
n
);
printLinkedList
(
head
);
head
=
Solution
().
swapPairs
(
head
);
printLinkedList
(
head
);
deleteLinkedList
(
head
);
return
0
;
}
\ No newline at end of file
Prev
1
2
3
4
5
…
7
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