From a35c713322e813c3ffd5722d8bfa448e739e51b9 Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Tue, 25 Dec 2018 16:00:26 -0500 Subject: [PATCH] =?UTF-8?q?Kayak=20=E6=B7=BB=E5=8A=A0=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E6=8E=92=E5=88=97=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../codebank/interview/KayakCountUpDown.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/main/java/com/ossez/codebank/interview/KayakCountUpDown.java diff --git a/src/main/java/com/ossez/codebank/interview/KayakCountUpDown.java b/src/main/java/com/ossez/codebank/interview/KayakCountUpDown.java new file mode 100644 index 0000000000..8b696881a3 --- /dev/null +++ b/src/main/java/com/ossez/codebank/interview/KayakCountUpDown.java @@ -0,0 +1,58 @@ +package com.ossez.codebank.interview; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author YuCheng + * + */ +public class KayakCountUpDown { + + static int minNumber = 0; + static int maxNumber = 0; + int tmpN = 0; + List retList = new ArrayList(); + + public List countUp(int start, int end) { + maxNumber = end; + tmpN = start; + moveUp(0); + retList.add(end); + return retList; + + } + + public List countUpDown(int start, int end) { + minNumber = start; + maxNumber = end; + tmpN = start; + + moveUp(0); + retList.add(end); + + moveDown(1); + return retList; + + } + + private void moveUp(int n) { + retList.add(tmpN); + tmpN++; + if (tmpN != maxNumber) { + moveUp(tmpN + 1); + } + + } + + private void moveDown(int n) { + tmpN = (maxNumber - n ); + retList.add(tmpN); + + if (tmpN != minNumber) { + moveDown(n + 1); + } + } + +}