Java AC
import java.io.StreamTokenizer; public class Main { /* * 1388 */ public static void main(String[] args) throws Exception { StreamTokenizer st = new StreamTokenizer(System.in); long array[] = new long[80]; array[0] = 1; array[1] = 2; for (int i = 2; i < array.length; i++) { array[i] = array[i-1] + array[i-2]; } while (st.nextToken() != StreamTokenizer.TT_EOF) { int n = (int) st.nval; System.out.println(array[n-1]); } } } /************************************************************** Problem: 1388 User: wzqwsrf Language: Java Result: Accepted Time:70 ms Memory:14532 kb ****************************************************************/
C++ AC
#include <stdio.h> const int maxn = 72; long array[maxn]; int n; int main(){ while(scanf("%d",&n) != EOF){ if(n == 1){ printf("1\n"); continue; } array[1] = 1; array[2] = 2; for(int i = 3; i < n + 1; i++){ array[i] = array[i-1] + array[i-2]; } printf("%ld\n", array[n]); } return 0; } /************************************************************** Problem: 1388 User: wangzhenqing Language: C++ Result: Accepted Time:0 ms Memory:1020 kb ****************************************************************/
Java AC 336ms
public class Solution { public int climbStairs(int n) { if (n == 1) { return 1; } int array[] = new int[n+1]; array[1] = 1; array[2] = 2; for(int i = 3; i < n+1; i++){ array[i] = array[i-1] + array[i-2]; } return array[n]; } }Python AC 120ms
class Solution: # @param n, an integer # @return an integer def climbStairs(self, n): if n == 1: return 1 array = [0 for i in range(n+1)] array[1] = 1 array[2] = 2 for i in range(3, n+1): array[i] = array[i-1] + array[i-2] return array[n]