本文共 1088 字,大约阅读时间需要 3 分钟。
public class Demo01 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入杨辉三角的行数:"); int n =sc.nextInt(); List
> ret = YH(n); for (;n>0;n--){ System.out.println(ret.get(5-n)); } } public static List
> YH(int n) { if (n==0) return null; List
> ret =new ArrayList<>(); for (int i = 0;i 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出3C 321bac 因为3先出,所以进了6个数据,而c最后出,则不可能,故选CD 321cba 进6出6
进栈:push()
出栈:pop() 获取栈顶元素:peek()先进先出的数据结构,类型为Queue
进队:offer()
出队:poll() 获取队头元素:peek()转载地址:http://ejse.baihongyu.com/