Count Up Down(上下计数)

简介:

这个题目是 Kayak 发布的代码挑战题目。

最简单的描述就是不使用循环,输出 0 到 5,然后同样不是会用循环的方式再次输出 5 到 0。

英文描述

Part 1

Write a program that counts in sequential order when given a start and end value - without using any iterative programing loops, i.e. while, for, do, for-each, etc.

You can assume that both the start and end values will always be positive and that the start value will always be less then the end value. There should only be one method with the following signature:

void countUp(int start, int end) {
// All code exercise code should go here
}

Here is example output with start=0 and end=5:

[ 0,1,2,3,4,5]

Part 2

Continuing with part 1 change the output of the test, so that it now prints out in sequential order to the end value (only once), but then also counts down to the start value.

Again, using no iterative loops, and assuming that both the start and end values will always be positive and that start value will always be less then the end value. There should only be one method with the following signature:

void countUpAndDown(int start, int end) {
// All code exercise code should go here
}

Here is example output with start=0 and end=5:

[0,1,2,3,4,5,4,3,2,1,0]

中文描述

这里我不按照原文一字一字的翻译,但是尽量按照题目的要求把题目解释清楚。

最简单的描述就是不使用循环,输出 0 到 5,然后同样不是会用循环的方式再次输出 5 到 0。

本题目分 2 部分,第一部分是不使用循环的方式输出 0 到 5,第二部分是不使用循环的方式输出 0 到 5 以后,再输出 5 到 0。

其中需要注意的是 5 只能输出一次。

思路和点评

不使用 For 循环的方式输出 0 到 5 ,我们可以想到有几个方法。

第一个方法可能比较容易想到的就是递归调用,你可以根据输入的值,递归调用需要的次数就可以输出值了。你还可以采用计算机时钟的方式进行输出。

在这里我们采用递归调用的方式进行输出。

源代码

源代码和有关代码的更新请访问 GitHub:

https://github.com/cwiki-us/codebank-algorithm/blob/master/src/main/java/com/ossez/codebank/interview/KayakCountUpDown.java

测试类请参考:

https://github.com/cwiki-us/codebank-algorithm/blob/master/src/test/java/com/ossez/codebank/interview/tests/KayakTest.java

代码思路请参考:



package com.ossez.codebank.interview;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * https://www.cwiki.us/display/ITCLASSIFICATION/Count+Up+Down
 * 
 * @author YuCheng
 *
 */
public class KayakCountUpDown {
	private final static Logger logger = LoggerFactory.getLogger(KayakCountUpDown.class);

	static int minNumber = 0;
	static int maxNumber = 0;
	int tmpN = 0;
	List<Integer> retList = new ArrayList<Integer>();

	/**
	 * 
	 * @param start
	 * @param end
	 * @return
	 */
	public List<Integer> countUp(int start, int end) {
		logger.debug("BEGIN");
		maxNumber = end;
		tmpN = start;
		moveUp(0);
		retList.add(end);
		return retList;

	}

	/**
	 * 
	 * @param start
	 * @param end
	 * @return
	 */
	public List<Integer> countUpDown(int start, int end) {
		logger.debug("BEGIN");
		minNumber = start;
		maxNumber = end;
		tmpN = start;

		moveUp(0);
		retList.add(end);

		moveDown(1);
		return retList;

	}

	/**
	 * 
	 * @param n
	 */
	private void moveUp(int n) {
		retList.add(tmpN);
		tmpN++;
		if (tmpN != maxNumber) {
			moveUp(tmpN + 1);
		}

	}

	/**
	 * 
	 * @param n
	 */
	private void moveDown(int n) {
		tmpN = (maxNumber - n);
		retList.add(tmpN);

		if (tmpN != minNumber) {
			moveDown(n + 1);
		}
	}

}


测试结果

上面程序的测试结果如下:

2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.tests.KayakTest] - TEST Count Up and Down 
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.tests.KayakTest] - [2 -> 5]
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.KayakCountUpDown] - BEGIN
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.tests.KayakTest] - UP - [2, 3, 4, 5]
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.KayakCountUpDown] - BEGIN
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.tests.KayakTest] - UP & DOWN - [2, 3, 4, 5, 4, 3, 2]
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.tests.KayakTest] - [0 -> 5]
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.KayakCountUpDown] - BEGIN
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.tests.KayakTest] - UP - [0, 1, 2, 3, 4, 5]
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.KayakCountUpDown] - BEGIN
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.tests.KayakTest] - UP & DOWN - [0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0]
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.tests.KayakTest] - [-1 -> 5]
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.KayakCountUpDown] - BEGIN
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.tests.KayakTest] - UP - [-1, 0, 1, 2, 3, 4, 5]
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.KayakCountUpDown] - BEGIN
2018/12/25 21:23:54 DEBUG [com.ossez.codebank.interview.tests.KayakTest] - UP & DOWN - [-1, 0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0, -1]

https://www.cwiki.us/display/ITCLASSIFICATION/Count+Up+Down

目录
相关文章
|
Java
Mac 下安装jdk1.7(国内镜像)
Mac 下安装jdk1.7(国内镜像)
2875 0
|
Java 编译器
java“变量 x 可能未被初始化”解决
在Java中,如果编译器检测到变量可能在使用前未被初始化,会报“变量 x 可能未被初始化”的错误。解决方法包括:1. 在声明变量时直接初始化;2. 确保所有可能的执行路径都能对变量进行初始化。
1074 2
|
开发框架 前端开发 JavaScript
在Winform开发中,使用Async-Awati异步任务处理代替BackgroundWorker
在Winform开发中,使用Async-Awati异步任务处理代替BackgroundWorker
|
开发框架 搜索推荐 数据中心
KDD2024最佳学生论文解读,中科大、华为诺亚:序列推荐新范式DR4SR
【9月更文挑战第25天】近年来,随着人工智能技术的发展,序列推荐系统(SR)因能捕捉用户动态偏好而在日常生活中愈发重要。然而,数据质量问题常被忽视。为解决此问题,中国科学技术大学与华为诺亚方舟实验室联合提出DR4SR,一种通过数据集再生提升序列推荐系统性能的新范式。DR4SR采用模型无关的数据再生方法,增强数据集的多样性和泛化能力,且可通过DR4SR+进行个性化调整以适应不同模型需求。实验表明,DR4SR和DR4SR+在多个数据集上显著提升了推荐系统性能。尽管面临计算资源和过拟合风险等挑战,该范式仍展现出巨大潜力。
393 7
|
存储 Java
HashMap之链表转红黑树(树化 )-treefyBin方法源码解读(所有涉及到的方法均有详细解读,欢迎指正)
本文详细解析了Java HashMap中链表转红黑树的机制,包括树化条件(链表长度达8且数组长度≥64)及转换流程,确保高效处理大量数据。
688 1
|
存储 安全 物联网
Android经典实战之跳转到系统设置页面或其他系统应用页面大全
本文首发于公众号“AntDream”,关注获取更多技巧。文章总结了Android开发中跳转至系统设置页面的方法,包括设备信息、Wi-Fi、显示与声音设置等,并涉及应用详情与电池优化页面。通过简单的Intent动作即可实现,需注意权限与版本兼容性。每日进步,尽在“AntDream”。
1664 2
|
存储 Prometheus 监控
服务搭建篇(一) 搭建基于prometheus + node_exporter + grafana + Alertmanager 的监控报警系统 , 保姆级教程
Alertmanager处理客户端应用程序(如Prometheus服务器)发送的警报。它负责重复数据删除、分组,并将它们路由到正确的接收器集成,如电子邮件、PagerDuty或OpsGenie。它还负责静音和抑制警报
756 0
|
分布式计算 Hadoop Serverless
数据处理的艺术:EMR Serverless Spark实践及应用体验
阿里云EMR Serverless Spark是基于Spark的全托管大数据处理平台,融合云原生弹性与自动化,提供任务全生命周期管理,让数据工程师专注数据分析。它内置高性能Fusion Engine,性能比开源Spark提升200%,并有成本优化的Celeborn服务。支持计算存储分离、OSS-HDFS兼容、DLF元数据管理,实现一站式的开发体验和Serverless资源管理。适用于数据报表、科学项目等场景,简化开发与运维流程。用户可通过阿里云控制台快速配置和体验EMR Serverless Spark服务。
|
机器学习/深度学习 人工智能
【机器学习】有哪些指标,可以检查回归模型是否良好地拟合了数据?
【5月更文挑战第16天】【机器学习】有哪些指标,可以检查回归模型是否良好地拟合了数据?
|
Cloud Native 安全 数据中心