A robot is located atthe top-left corner ofa m x n grid (marked 'Start'inthe diagram below).
The robot can only move either down orrightatany point intime. The robot is trying to reach the bottom-right corner ofthe grid(marked 'Finish'inthe diagram below).
How many possible unique paths are there?
Example
1,1
1,2
1,3
1,4
1,5
1,6
1,7
2,1
3,1
3,7
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Java:
// space O(n) public int uniquePaths(int m, int n) { if (m < 1 || n < 1) return0; if (m == 1 || n == 1) return1; int[] result = new int[n]; for (int i = 0; i < n; i++) { result[i] = 1; } for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) result[j] += result[j - 1]; } returnresult[n - 1]; }
习题地址 Unique Paths
Trailing Zeros - easy
题目
1 2 3 4
Write an algorithm which computes thenumberof trailing zeros in n factorial.
Example 11! = 39916800, so the out should be 2
代码
1 2 3 4 5 6 7 8 9 10 11 12
Java:
publiclong trailingZeros(long n) { if (n <= 0) return0; longcount = 0; while(n > 0) { count += n / 5; n /= 5; } returncount; }
习题地址 Trailing Zeros
Update Bits - medium
题目
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits between i and j in N equal to M (e g , M becomes a substring of N located at i and starting atj) Clarification You can assume that the bits j through i have enough space to fit all of M. That is, if M=10011, you can assume that there are at least 5bits between j and i. You would not, for example, have j=3 and i=2, because M could not fully fit between bit 3and bit 2.
Example Given N=(10000000000)2, M=(10101)2, i=2, j=6 return N=(10001010100)2
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Java:
publicintupdateBits(int n, int m, int i, int j){ int max = ~0; /* All 1’s */ // 1’s through position j, then 0’s if (j == 31) j = max; else j = (1 << (j + 1)) - 1; int left = max - j; // 1’s after position i int right = ((1 << i) - 1); // 1’s, with 0s between i and j int mask = left | right; // Clear i through j, then put m in there return ((n & mask) | (m << i)); }
习题地址 Update Bits
Unique Binary Search Trees - medium
题目
1 2 3 4 5 6 7 8 9 10 11
Given n, how many structurally unique BSTs (binary search trees) that store values 1...n?
Example Given n = 3, there are a total of 5 unique BST's.
1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Java:
publicintnumTrees(int n){ if (n == 0) return1; int[] dp = newint[n + 1]; dp[0] = 1; for (int i = 0; i <= n; i++) { for (int j = 0; j < i; j++) { dp[i] += dp[j] * dp[i - j - 1]; } } return dp[n]; }
习题地址 Unique Binary Search Trees
Fast Power - medium
题目
1 2 3 4 5 6
Calculate the power(a, n) % b where a, b and n are all32bit integers.
publicintfastPower(int a, int b, int n){ long ret = getPower(a, b, n); return (int)ret; }
publiclonggetPower(int a, int b, int n){ if(a == 0) return0; if(n == 0) return1 % b; if(n == 1) return a % b;
long ret = getPower(a, b, n/2); ret *= ret; ret %= b; if(n % 2 == 1){ ret = ret * (a % b); } return ret % b; }
习题地址 Fast Power
Binary Representation - hard
题目
1 2 3 4 5 6 7 8 9
Given a (decimal - e.g. 3.72) number that is passed inasastring, returnthe binary representation that is passed inasastring. If the fractional part of thenumber can not be represented accurately in binary withat most 32characters, return ERROR.
publicStringbinaryRepresentation(String n) { // write your code here if (n.indexOf('.') == -1) { returnparseInteger(n); } String[] params = n.split("\\."); String flt = parseFloat(params[1]); if (flt == "ERROR") { return flt; } if (flt.equals("0") || flt.equals("")) { returnparseInteger(params[0]); } returnparseInteger(params[0]) "." flt; }
privateStringparseInteger(Stringstr) { int n = Integer.parseInt(str); if (str.equals("") || str.equals("0")) { return"0"; } Stringbinary = ""; while (n != 0) { binary = Integer.toString(n % 2) binary; n = n / 2; } returnbinary; }
privateStringparseFloat(Stringstr) { double d = Double.parseDouble("0."str); StringBuilder binary = new StringBuilder(); HashSet<Double> set = new HashSet<Double>(); while (d > 0) { if (binary.length() > 32 || set.contains(d)) { return"ERROR"; } set.add(d); d = d * 2; if (d >= 1) { binary.append(1); d = d - 1; } else { binary.append(0); } } returnbinary.toString(); }