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
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]; }
Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to setall bits between i and j in N equal to M (e g , M becomes a substringof N located at i andstartingat j)
Clarification You can assume that the bits j through i have enough spaceto fit allof M. That is, if M=10011, you can assume that there areatleast5 bits between j and i. You would not, for example, have j=3and i=2, because M could not fully fit between bit3andbit2.
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
Java:
publicint updateBits(int n, int m, int i, int j) { intmax = ~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)); }
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.
近期评论