摘要:
Given an indexk, return thekthrow of the Pascal's triangle.For example, givenk= 3,Return[1,3,3,1].Note:Could you optimize your algorithm to use onlyO(k) extra space?对于产生一个新的行用从后往前的方法来更新,这样就只需一个O(k)的空间。 1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 // Start typing yo.. 阅读全文
摘要:
GivennumRows, generate the firstnumRowsof Pascal's triangle.For example, givennumRows= 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]模拟题。 1 class Solution { 2 private: 3 vector<vector<int> > ret; 4 public: 5 vector<vector<int> > generate(int numRows) { 6 // Start 阅读全文
摘要:
Determine whether an integer is a palindrome. Do this without extra space.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of converting the integer to string, note the restriction of using extra space.You could also try reversing an integer. However, if you have solved 阅读全文
摘要:
Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.DFS 1 class Solution { 2 private: 3 int ret; 4 int a[100]; 5 bool canUse[100]; 6 public: 7 bool check(int y, int n) 8 { 9 for(int i = 0; i < n; i++)1... 阅读全文
摘要:
Then-queens puzzle is the problem of placingnqueens on ann×nchessboard such that no two queens attack each other.Given an integern, return all distinct solutions to then-queens puzzle.Each solution contains a distinct board configuration of then-queens' placement, where'Q'and'.& 阅读全文
摘要:
Given amxngrid filled with non-negative numbers, find a path from top left to bottom right whichminimizesthe sum of all numbers along its path.Note:You can only move either down or right at any point in time.DP,f[i][j] = min(f[i-1][j], f[i][j-1]) + a[i][j] 1 class Solution { 2 private: 3 int f[1... 阅读全文