不使用循环的方式输出循环的值,具体问题,请参考: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
1 changed files with 32 additions and 4 deletions

View File

@ -3,19 +3,32 @@ 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);
@ -24,19 +37,30 @@ public class KayakCountUpDown {
}
/**
*
* @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++;
@ -46,8 +70,12 @@ public class KayakCountUpDown {
}
/**
*
* @param n
*/
private void moveDown(int n) {
tmpN = (maxNumber - n );
tmpN = (maxNumber - n);
retList.add(tmpN);
if (tmpN != minNumber) {