不使用循环的方式输出循环的值,具体问题,请参考:https://www.cwiki.us/display/ITCLASSIFICATION/Count+Up+Down

This commit is contained in:
YuCheng Hu 2018-12-25 21:17:34 -05:00
parent a35c713322
commit aa0b09e306

View File

@ -3,19 +3,32 @@ package com.ossez.codebank.interview;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
*
* https://www.cwiki.us/display/ITCLASSIFICATION/Count+Up+Down
* *
* @author YuCheng * @author YuCheng
* *
*/ */
public class KayakCountUpDown { public class KayakCountUpDown {
private final static Logger logger = LoggerFactory.getLogger(KayakCountUpDown.class);
static int minNumber = 0; static int minNumber = 0;
static int maxNumber = 0; static int maxNumber = 0;
int tmpN = 0; int tmpN = 0;
List<Integer> retList = new ArrayList<Integer>(); List<Integer> retList = new ArrayList<Integer>();
/**
*
* @param start
* @param end
* @return
*/
public List<Integer> countUp(int start, int end) { public List<Integer> countUp(int start, int end) {
logger.debug("BEGIN");
maxNumber = end; maxNumber = end;
tmpN = start; tmpN = start;
moveUp(0); moveUp(0);
@ -24,7 +37,14 @@ public class KayakCountUpDown {
} }
/**
*
* @param start
* @param end
* @return
*/
public List<Integer> countUpDown(int start, int end) { public List<Integer> countUpDown(int start, int end) {
logger.debug("BEGIN");
minNumber = start; minNumber = start;
maxNumber = end; maxNumber = end;
tmpN = start; tmpN = start;
@ -37,6 +57,10 @@ public class KayakCountUpDown {
} }
/**
*
* @param n
*/
private void moveUp(int n) { private void moveUp(int n) {
retList.add(tmpN); retList.add(tmpN);
tmpN++; tmpN++;
@ -46,8 +70,12 @@ public class KayakCountUpDown {
} }
/**
*
* @param n
*/
private void moveDown(int n) { private void moveDown(int n) {
tmpN = (maxNumber - n ); tmpN = (maxNumber - n);
retList.add(tmpN); retList.add(tmpN);
if (tmpN != minNumber) { if (tmpN != minNumber) {