今天去一家公司面试。面试题中出现这么一道算法题。平时没太多时间去研究面试题的我。只能临场发挥了。于是写上了以下的代码。。。。。。。经验证,没发现什么问题。
public class YHSJ {
/**
* @param args
* @createtime Apr 14, 2010 6:49:41 PM
*/
public static void main(String[] args) {
Integer[] topRow = null;
Integer[] currentRow = null;
for(int i=0;i<10;i++) {
currentRow = new Integer[i+1];
for(int j=0;j<i+1;j++) {
try {
currentRow[j] = (topRow[j-1]+topRow[j]);
} catch (Exception e) {
currentRow[j] = 1;
}
System.out.print(currentRow[j]+" ");
}
System.out.println();
topRow = currentRow;
}
}
}
本文分享了一道面试中遇到的算法题及其解决方案。该题通过Java实现了一个特定的数值模式打印功能,采用双重循环来构建每一行的数据,并巧妙处理了边界条件。

1262

被折叠的 条评论
为什么被折叠?



