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

目录
相关文章
|
5月前
|
存储 SQL 关系型数据库
count(1)、count(具体字段)和count(*)究竟有什么区别?
count(1)、count(具体字段)和count(*)究竟有什么区别?
54 0
|
SQL Oracle 关系型数据库
count函数
count函数
93 0
|
SQL 关系型数据库 MySQL
|
C++
201604-1 折点计数
201604-1 折点计数
56 0
201604-1 折点计数
|
存储 SQL 缓存
count(*)那么慢能用吗,该怎么办呢?
大家好前面我们大概了解了为什么delete from表名,表的大小还是没有变小!以及数据删除流程,数据页空洞,online和inplace。重建表的两种实现方式。今天介绍一下为什么count(*)那么慢。
count(*)那么慢能用吗,该怎么办呢?
|
关系型数据库 数据库
都2020年了,你还不知道count(1)和count(*)谁效率更高吗?
今天公司的一个需求需要统计一个数据库中表的行数有多少,二话不说当然就直接用count()这个聚合函数,以前经常听到一种说法说count(1)的效率比count(*)要高,于是测试了一下count(1)和count(*)的速度差距,发现两者的查询速度很接近,甚至count(*)要更快一些,于是就有了这篇文章。
|
.NET
如何使用 GroupBy 计数-Count()
十年河东,十年河西,莫欺少年穷。 本节探讨的内容很简单,就是如果使用GroupBy计数 提供两种方法:第一:把查询的数据,转化为泛型,然后泛型分组计数。                         第二:Linq语句直接分组计数 有如下范例: SQL如下: create table S_ca...
2747 0
|
人工智能
白细胞计数
总时间限制: 1000ms 内存限制: 65536kB 题目链接:http://noi.openjudge.cn/ch0109/08/ 描述 医院采样了某临床病例治疗期间的白细胞数量样本n份,用于分析某种新抗生素对该病例的治疗效果。
2110 0
|
算法 Java
算法题-Count and Say
题目来自 LeetCode The count-and-say sequence is the sequence of integers with the first five terms as following: 1 11 21 1211 111221 1 is read off as “one 1” or 11.
1032 0
|
SQL 关系型数据库 MySQL
count(*) VS count(X)
背景 在平时的工作中,有些同学对count的用法还是有疑惑的,为此我做个简单的总结和测试,希望对大家有帮助。 count(*)和count(X)是不等价的 表达式 含义 count(*) 返回总行数,包括空和非空值 count(expression) .
1823 0