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:
测试类请参考:
代码思路请参考:
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] - BEGIN2018/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] - BEGIN2018/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] - BEGIN2018/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] - BEGIN2018/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] - BEGIN2018/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] - BEGIN2018/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]
Count Up Down(上下计数)的更多相关文章
- LeetCode 811. Subdomain Visit Count (子域名访问计数)
题目标签:HashMap 题目给了我们一组域名,让我们把每一个域名,包括它的子域名,计数. 遍历每一个域名,取得它的计数,然后把它的所有子域名和它自己,存入hashmap,域名作为key,计数作为va ...
- oracle 使用count()函数进行分组计数时所踩的坑!
1.情景展示 需要对id_card字段按字符长度进行分组统计并进行计数. 2.错误方式 第一步:统计出id_card字段共存在几种情况. 第一种方式:distinct 第二种方式:group by ...
- LeetCode OJ:Count Primes(质数计数)
Count the number of prime numbers less than a non-negative number, n. 计算小于n的质数的个数,当然就要用到大名鼎鼎的筛法了,代码如 ...
- [LeetCode] Count Different Palindromic Subsequences 计数不同的回文子序列的个数
Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...
- HDU 5901 Count primes 大素数计数
题意:计算1~N间素数的个数(N<=1e11) 题解:题目要求很简单,作为论文题,模板有两种 \(O(n^\frac{3}{4} )\),另一种lehmer\(O(n^\frac{2}{3})\ ...
- [LeetCode] 730. Count Different Palindromic Subsequences 计数不同的回文子序列的个数
Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...
- Leetcode811.Subdomain Visit Count子域名访问计数
一个网站域名,如"discuss.leetcode.com",包含了多个子域名.作为顶级域名,常用的有"com",下一级则有"leetcode.com ...
- [Swift]LeetCode38. 报数 | Count and Say
The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 ...
- Storm 实现滑动窗口计数和TopN排序
计算top N words的topology, 用于比如trending topics or trending images on Twitter. 实现了滑动窗口计数和TopN排序, 比较有意思, ...
- SQL-基础学习4--聚集函数:AVG(),COUNT(),MAX(),MIN(),SUM();聚集不同值:DISTINCT
第九课 9.1 聚集函数(对某些行运行的函数,计算并返回一个值) 我们经常需要汇总数据而不用把它们实际检索出来,为此SQL提供了专门的函数.使用这些函数,SQL查询可用于检索数据,以便分析和报表生成. ...
随机推荐
- ODAC(V9.5.15) 学习笔记(四)TCustomDADataSet(2)
2.连接相关 名称 类型 说明 Connection 指向一个数据库连接对象 Disconnected 设置为True将在数据库关闭后继续保持数据集的开启状态. 3. 数据获取 名称 类型 说明 Fe ...
- Firemonkey的几个特色属性(一)
基于FireMonkey的程序开发与VCL确实有些不同,很多属性发生了变化,尤其是外观方面. 1.Margins.Padding.Position Margins:指定了当前控件在父控件(Parent ...
- 【做题】arc080_f-Prime Flip——转换、数论及匹配
题意:有一个无限序列,其中有\(n\)个位置上的数为\(1\),其余都是\(0\).你可以进行若干次操作,每次选取序列上的一个区间\([l,r)\),满足\(r-l\)为奇质数,将在这个区间上的数都异 ...
- FancyBox的使用技巧 (汇总)
http://note.youdao.com/share/?id=1c8373249f523529a6b6dcde60777400&type=note#/
- 比赛总结——atcoder beginner contest 109
第一次AK的ABC 虽然题非常简单 但是值得纪念一下 T1 一道很水的题 不存在做法 纯粹乱跑 但是我把Yes打成YES了,哭唧唧 #include <cstdio> #include & ...
- 给Ubuntu添加清华的软件源
找到 sources.list 文件 cd /etc/apt/ 编辑 vim sources.list 在最后面加上下面这几条语句 # 默认注释了源码镜像以提高 apt update 速度,如有需要可 ...
- Linux安装Broadcom无线驱动
参考https://blog.csdn.net/u012833250/article/details/52493806 首先查看自己的网卡型号,然后先执行sudo apt-get update 再根据 ...
- 【译】第17节---数据注解-Column
原文:http://www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-first. ...
- C语言: 从 CodeBlocks 到 Microsoft Visual Studio 2017
开学到现在寒假,学习了一个学期的C语言,同时也已然用了大半年的 CodeBlocks 来写 C/C++ 程序.CodeBlocks 是写 C/C++ 语言的程序最轻量的IDE(集成开发环境),在 C ...
- JMeter中关于动态切换不同CSV文件解决方案
最近写case,需要当前播放节目的数据作为输入数据,所以每个时刻所用的数据只能是当前时刻附件的数据,尝试用CSV Data Set Config动态加载不同的文件,没有成功,好像CSV Data Se ...