Kayak 添加数组排列算法

This commit is contained in:
YuCheng Hu 2018-12-25 16:00:26 -05:00
parent 145596a71e
commit a35c713322
1 changed files with 58 additions and 0 deletions

View File

@ -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<Integer> retList = new ArrayList<Integer>();
public List<Integer> countUp(int start, int end) {
maxNumber = end;
tmpN = start;
moveUp(0);
retList.add(end);
return retList;
}
public List<Integer> 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);
}
}
}