My code:
import java.util.ArrayList;
import java.util.List;
public class Solution {
public List<Integer> getRow(int rowIndex) {
List<Integer> result = new ArrayList<Integer>();
int row = rowIndex + 1;
if (row <= 0)
return result;
else if (row == 1) {
result.add(1);
return result;
}
else if (row == 2) {
result.add(1);
result.add(1);
return result;
}
int[] arrayK = new int[row];
arrayK[0] = 1;
arrayK[1] = 1;
for (int i = 3; i <= row; i++) {
int[] temp = new int[row];
for (int j = 0; j < i; j++) {
if (j == 0)
temp[j] = 1;
else if (j == i - 1)
temp[j] = 1;
else
temp[j] = arrayK[j - 1] + arrayK[j];
}
arrayK = temp;
}
for(int i = 0; i < row; i++)
result.add(arrayK[i]);
return result;
}
public static void main(String[] args) {
Solution test = new Solution();
System.out.println(test.getRow(5));
}
}
My test result:
這次作業(yè)也比較簡單吧烧给。
**
總結:Today is my birthday. Hope everything will fine in the next year.
**
Anyway, Good luck, Richardo!
My code:
public class Solution {
public List<Integer> getRow(int rowIndex) {
ArrayList<Integer> ret = new ArrayList<Integer>();
if (rowIndex < 0)
return ret;
int[] level = new int[rowIndex + 1];
level[0] = 1;
for (int i = 1; i < rowIndex + 1; i++) {
for (int j = i - 2; j >= 0; j--) {
level[j + 1] += level[j];
}
level[i] = 1;
}
for (int i = 0; i <= rowIndex; i++)
ret.add(level[i]);
return ret;
}
}
用一個數組從右往左模擬子刮。還是很快的。代碼也比以前的簡潔生宛。
Anyway, Good luck, Richardo!