- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
这个现在非常常见的算法问题是在白板考试期间由监考人员提出的。我的工作是观察、倾听和客观判断给出的答案,但我无法控制这个问题,也无法与回答者互动。
给了五分钟的时间分析问题,考生可以写项目符号,伪代码(这在实际编写代码时是允许的,只要明确指出,以及将伪代码作为注释或TODO任务的人在弄清楚算法之前得到了加分)。
public class Staircase {
public static int stairs;
public Staircase() {
int a = counting(stairs);
Hệ thống.out.println(a);
}
static int counting(int n) {
if (n < 0)
trả về 0;
else if (n == 0)
trả về 1;
khác
return counting(n - 1) + counting(n - 2) + counting(n - 3);
}
public static void main(String[] args) {
Staircase child;
long t1 = System.nanoTime();
for (int i = 0; i < 30; i++) {
stairs = i;
child = new Staircase();
}
System.out.println("Time:" + ((System.nanoTime() - t1)/1000000));
}
}
//
public class Steps {
public static int stairs;
int c2 = 0;
public Steps() {
int a = step2(0);
Hệ thống.out.println(a);
}
public static void main(String[] args) {
Steps steps;
long t1 = System.nanoTime();
for (int i = 0; i < 30; i++) {
stairs = i;
steps = new Steps();
}
System.out.println("Time:" + ((System.nanoTime() - t1) / 1000000));
}
public int step2(int c) {
if (c + 1 < stairs) {
if (c + 2 <= stairs) {
if (c + 3 <= stairs) {
step2(c + 3);
}
step2(c + 2);
}
step2(c + 1);
} khác {
c2++;
}
return c2;
}
}
câu trả lời hay nhất
前言:您可以在不到一毫秒的时间内轻松执行此计算。详情如下...
哪一个更好”?
哪种算法“更好”的问题可能涉及执行时间,但也涉及其他方面,例如实现风格。Staircase
实现更短,更简洁,恕我直言,更具可读性。更重要的是:它不涉及 tình trạng 。您在那里引入的 c2
变量破坏了纯函数递归实现的优点(和美感)。这可能很容易修复,尽管实现已经变得更类似于 Staircase
了。
衡量绩效
关于执行时间的问题:在 Java 中正确测量执行时间很棘手。
Bài đọc liên quan:
Staircase
实现和
Steps
实现没有太大区别。
Staircase
实现是从
向下计数 ,而
Steps
实现是在
向上计数 。
nếu như
语句。
Staircase
实现对基本情况使用了一种非常通用的处理方式,只检查是否
n < 0
。
class Staircase2
{
public static int counting(int n)
{
int result = 0;
if (n >= 1)
{
result += counting(n-1);
if (n >= 2)
{
result += counting(n-2);
if (n >= 3)
{
result += counting(n-3);
}
}
}
khác
{
result += 1;
}
trả về kết quả;
}
}
nếu như
查询避免了许多“无用”的调用。它已经明显快于原始
Staircase
实现,但仍然比
Steps
实现慢一点。
nếu như
语句和一些附加内容组成。这里最昂贵的事情实际上是递归本身,以及深度嵌套的调用树。
compute(5)
compute(4)
compute(3)
compute(2)
compute(1)
compute(0)
compute(0)
compute(1)
compute(0)
compute(0)
compute(2)
compute(1)
compute(0)
compute(0)
compute(1)
compute(0)
compute(3)
compute(2)
compute(1)
compute(0)
compute(0)
compute(1)
compute(0)
compute(0)
compute(2)
compute(1)
compute(0)
compute(0)
nhập java.util.Arrays;
public class StaircaseSteps
{
public static void main(String[] args)
{
for (int i = 5; i < 33; i++)
{
runStaircase(i);
runSteps(i);
runDynamic(i);
}
}
private static void runStaircase(int max)
{
long before = System.nanoTime();
long sum = 0;
for (int i = 0; i < max; i++)
{
sum += Staircase.counting(i);
}
long after = System.nanoTime();
System.out.println("Staircase up to "+max+" gives "+sum+" time "+(after-before)/1e6);
}
private static void runSteps(int max)
{
long before = System.nanoTime();
long sum = 0;
for (int i = 0; i < max; i++)
{
sum += Steps.step(i);
}
long after = System.nanoTime();
System.out.println("Steps up to "+max+" gives "+sum+" time "+(after-before)/1e6);
}
private static void runDynamic(int max)
{
long before = System.nanoTime();
long sum = 0;
for (int i = 0; i < max; i++)
{
sum += StaircaseDynamicProgramming.counting(i);
}
long after = System.nanoTime();
System.out.println("Dynamic up to "+max+" gives "+sum+" time "+(after-before)/1e6);
}
}
class Staircase
{
public static int counting(int n)
{
if (n < 0)
trả về 0;
else if (n == 0)
trả về 1;
khác
return counting(n - 1) + counting(n - 2) + counting(n - 3);
}
}
class Steps
{
static int c2 = 0;
static int stairs;
public static int step(int c)
{
c2 = 0;
stairs = c;
return step2(0);
}
private static int step2(int c)
{
if (c + 1 < stairs)
{
if (c + 2 <= stairs)
{
if (c + 3 <= stairs)
{
step2(c + 3);
}
step2(c + 2);
}
step2(c + 1);
}
khác
{
c2++;
}
return c2;
}
}
class StaircaseDynamicProgramming
{
public static int counting(int n)
{
int results[] = new int[n+1];
Arrays.fill(results, -1);
return counting(n, results);
}
private static int counting(int n, int results[])
{
int result = results[n];
if (result == -1)
{
result = 0;
if (n >= 1)
{
result += counting(n-1, results);
if (n >= 2)
{
result += counting(n-2, results);
if (n >= 3)
{
result += counting(n-3, results);
}
}
}
khác
{
result += 1;
}
}
results[n] = result;
trả về kết quả;
}
}
...
Staircase up to 29 gives 34850335 time 310.672814
Steps up to 29 gives 34850335 time 112.237711
Dynamic up to 29 gives 34850335 time 0.089785
Staircase up to 30 gives 64099760 time 578.072582
Steps up to 30 gives 64099760 time 204.264142
Dynamic up to 30 gives 64099760 time 0.091524
Staircase up to 31 gives 117897840 time 1050.152703
Steps up to 31 gives 117897840 time 381.293274
Dynamic up to 31 gives 117897840 time 0.084565
Staircase up to 32 gives 216847936 time 1929.43348
Steps up to 32 gives 216847936 time 699.066728
Dynamic up to 32 gives 216847936 time 0.089089
关于递归爬楼梯拼图的 Java 基准测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25635713/
đóng cửa. Câu hỏi này không tuân thủ các nguyên tắc của Stack Overflow. Hiện tại nó không chấp nhận câu trả lời. Các câu hỏi yêu cầu chúng tôi giới thiệu hoặc tìm các công cụ, thư viện hoặc tài nguyên yêu thích bên ngoài trang web đều không có chủ đề đối với Stack Overflow.
Tôi là một lập trình viên xuất sắc, rất giỏi!