杨辉三角
public class Demo01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入杨辉三角的行数:");
int n =sc.nextInt();
List<List<Integer>> ret = YH(n);
for (;n>0;n--){
System.out.println(ret.get(5-n));
}
}
public static List<List<Integer>> YH(int n) {
if (n==0) return null;
List<List<Integer>> ret =new ArrayList<>();
for (int i = 0;i<n;i++){//一直写到n-1行 此时就为n行
List<Integer> cur =new ArrayList<>();
if (i == 0){
cur.add(1);
ret.add(cur);
continue;
}
cur.add(1);
for (int j = 1 ;j < i;j++){
cur.add(ret.get(i-1).get(j-1)+ret.get(i-1).get(j));
}
cur.add(1);
ret.add(cur);
}
return ret;
}
}
栈
先进后出的数据结构,类型为Stack。
如题
一个栈的入栈序列为abc123,则这个栈的不可能出栈序列为:
A abc123 每进一个出一个
B acb321 进1出1 进2出2 进3出3
C 321bac 因为3先出,所以进了6个数据,而c最后出,则不可能,故选C
D 321cba 进6出6
进栈出栈
进栈:push()
出栈:pop()
获取栈顶元素:peek()
栈的实现
中缀表达式(前、后缀表达式)(选)
中缀表达式、前缀表达式及后缀表达式的概念及转换过程
而在栈中输入后缀表达式时,遇到变量/常量先入栈,遇到运算符号则出栈顶两个变量/常量。第一个数字为这个运算符的右操作数,第二个数字就为运算符的左操作数。
队列
先进先出的数据结构,类型为Queue
进队出队
进队:offer()
出队:poll()
获取队头元素:peek()