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
e6726c84
Commit
e6726c84
authored
Jan 20, 2019
by
MisterBigbooo
Browse files
Add 279 code
parent
09a97c1f
Changes
7
Hide whitespace changes
Inline
Side-by-side
0279-Perfect-Squares/cpp-0279/CMakeLists.txt
0 → 100755
View file @
e6726c84
cmake_minimum_required
(
VERSION 3.5
)
project
(
cpp_0279
)
set
(
CMAKE_CXX_FLAGS
"
${
CMAKE_CXX_FLAGS
}
-std=c++11"
)
set
(
SOURCE_FILES main3.cpp
)
add_executable
(
cpp_0279
${
SOURCE_FILES
}
)
\ No newline at end of file
0279-Perfect-Squares/cpp-0279/main.cpp
0 → 100755
View file @
e6726c84
/// Source : https://leetcode.com/problems/perfect-squares/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
#include
<iostream>
#include
<vector>
#include
<queue>
#include
<stdexcept>
using
namespace
std
;
/// BFS
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class
Solution
{
public:
int
numSquares
(
int
n
)
{
if
(
n
==
0
)
return
0
;
queue
<
pair
<
int
,
int
>>
q
;
q
.
push
(
make_pair
(
n
,
0
));
vector
<
bool
>
visited
(
n
+
1
,
false
);
visited
[
n
]
=
true
;
while
(
!
q
.
empty
()){
int
num
=
q
.
front
().
first
;
int
step
=
q
.
front
().
second
;
q
.
pop
();
for
(
int
i
=
1
;
num
-
i
*
i
>=
0
;
i
++
){
int
a
=
num
-
i
*
i
;
if
(
!
visited
[
a
]){
if
(
a
==
0
)
return
step
+
1
;
q
.
push
(
make_pair
(
a
,
step
+
1
));
visited
[
a
]
=
true
;
}
}
}
throw
invalid_argument
(
"No Solution."
);
}
};
int
main
()
{
cout
<<
Solution
().
numSquares
(
12
)
<<
endl
;
cout
<<
Solution
().
numSquares
(
13
)
<<
endl
;
return
0
;
}
\ No newline at end of file
0279-Perfect-Squares/cpp-0279/main2.cpp
0 → 100755
View file @
e6726c84
/// Source : https://leetcode.com/problems/perfect-squares/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
#include
<iostream>
#include
<vector>
using
namespace
std
;
/// Memory Search
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class
Solution
{
public:
int
numSquares
(
int
n
)
{
vector
<
int
>
mem
(
n
+
1
,
-
1
);
return
numSquares
(
n
,
mem
);
}
private:
int
numSquares
(
int
n
,
vector
<
int
>&
mem
){
if
(
n
==
0
)
return
0
;
if
(
mem
[
n
]
!=
-
1
)
return
mem
[
n
];
int
res
=
INT_MAX
;
for
(
int
i
=
1
;
n
-
i
*
i
>=
0
;
i
++
)
res
=
min
(
res
,
1
+
numSquares
(
n
-
i
*
i
,
mem
));
return
mem
[
n
]
=
res
;
}
};
int
main
()
{
cout
<<
Solution
().
numSquares
(
12
)
<<
endl
;
cout
<<
Solution
().
numSquares
(
13
)
<<
endl
;
return
0
;
}
\ No newline at end of file
0279-Perfect-Squares/cpp-0279/main3.cpp
0 → 100755
View file @
e6726c84
/// Source : https://leetcode.com/problems/perfect-squares/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
#include
<iostream>
#include
<vector>
using
namespace
std
;
/// Dynamic Programming
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class
Solution
{
public:
int
numSquares
(
int
n
)
{
vector
<
int
>
mem
(
n
+
1
,
INT_MAX
);
mem
[
0
]
=
0
;
for
(
int
i
=
1
;
i
<=
n
;
i
++
)
for
(
int
j
=
1
;
i
-
j
*
j
>=
0
;
j
++
)
mem
[
i
]
=
min
(
mem
[
i
],
1
+
mem
[
i
-
j
*
j
]);
return
mem
[
n
];
}
};
int
main
()
{
cout
<<
Solution
().
numSquares
(
12
)
<<
endl
;
cout
<<
Solution
().
numSquares
(
13
)
<<
endl
;
return
0
;
}
\ No newline at end of file
0279-Perfect-Squares/java-0279/src/Solution1.java
0 → 100755
View file @
e6726c84
/// Source : https://leetcode.com/problems/perfect-squares/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
import
java.util.LinkedList
;
import
javafx.util.Pair
;
/// BFS
/// Time Complexity: O(n)
/// Space Complexity: O(n)
public
class
Solution1
{
public
int
numSquares
(
int
n
)
{
if
(
n
==
0
)
return
0
;
LinkedList
<
Pair
<
Integer
,
Integer
>>
queue
=
new
LinkedList
<
Pair
<
Integer
,
Integer
>>();
queue
.
addLast
(
new
Pair
<
Integer
,
Integer
>(
n
,
0
));
boolean
[]
visited
=
new
boolean
[
n
+
1
];
visited
[
n
]
=
true
;
while
(!
queue
.
isEmpty
()){
Pair
<
Integer
,
Integer
>
front
=
queue
.
removeFirst
();
int
num
=
front
.
getKey
();
int
step
=
front
.
getValue
();
if
(
num
==
0
)
return
step
;
for
(
int
i
=
1
;
num
-
i
*
i
>=
0
;
i
++){
int
a
=
num
-
i
*
i
;
if
(!
visited
[
a
]){
if
(
a
==
0
)
return
step
+
1
;
queue
.
addLast
(
new
Pair
(
num
-
i
*
i
,
step
+
1
));
visited
[
num
-
i
*
i
]
=
true
;
}
}
}
throw
new
IllegalStateException
(
"No Solution."
);
}
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
((
new
Solution1
()).
numSquares
(
12
));
System
.
out
.
println
((
new
Solution1
()).
numSquares
(
13
));
}
}
0279-Perfect-Squares/java-0279/src/Solution2.java
0 → 100755
View file @
e6726c84
/// Source : https://leetcode.com/problems/perfect-squares/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
import
java.util.Arrays
;
/// Memory Search
/// Time Complexity: O(n)
/// Space Complexity: O(n)
public
class
Solution2
{
public
int
numSquares
(
int
n
)
{
int
[]
mem
=
new
int
[
n
+
1
];
Arrays
.
fill
(
mem
,
-
1
);
return
numSquares
(
n
,
mem
);
}
private
int
numSquares
(
int
n
,
int
[]
mem
){
if
(
n
==
0
)
return
0
;
if
(
mem
[
n
]
!=
-
1
)
return
mem
[
n
];
int
res
=
Integer
.
MAX_VALUE
;
for
(
int
i
=
1
;
n
-
i
*
i
>=
0
;
i
++
)
res
=
Math
.
min
(
res
,
1
+
numSquares
(
n
-
i
*
i
,
mem
));
return
mem
[
n
]
=
res
;
}
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
((
new
Solution2
()).
numSquares
(
12
));
System
.
out
.
println
((
new
Solution2
()).
numSquares
(
13
));
}
}
0279-Perfect-Squares/java-0279/src/Solution3.java
0 → 100755
View file @
e6726c84
/// Source : https://leetcode.com/problems/perfect-squares/description/
/// Author : liuyubobobo
/// Time : 2017-11-17
import
java.util.Arrays
;
/// Dynamic Programming
/// Time Complexity: O(n)
/// Space Complexity: O(n)
public
class
Solution3
{
public
int
numSquares
(
int
n
)
{
int
[]
mem
=
new
int
[
n
+
1
];
Arrays
.
fill
(
mem
,
Integer
.
MAX_VALUE
);
mem
[
0
]
=
0
;
for
(
int
i
=
1
;
i
<=
n
;
i
++)
for
(
int
j
=
1
;
i
-
j
*
j
>=
0
;
j
++)
mem
[
i
]
=
Math
.
min
(
mem
[
i
],
1
+
mem
[
i
-
j
*
j
]);
return
mem
[
n
];
}
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
((
new
Solution3
()).
numSquares
(
12
));
System
.
out
.
println
((
new
Solution3
()).
numSquares
(
13
));
}
}
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