摘要:
Given a number represented as an array of digits, plus one to the number.进位和加法。 1 class Solution { 2 public: 3 vector<int> plusOne(vector<int> &digits) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 vector<int> ret(digits); 7 ... 阅读全文
摘要:
Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2]have the following unique permutations:[1,1,2],[1,2,1], and[2,1,1].先对数组进行排序,这样在DFS的时候,可以先判断前面的一个数是否和自己相等,相等的时候则前面的数必须使用了,自己才能使用,这样就不会产生重复的排列了。 1 class Solution { 2 private: 3 b... 阅读全文
摘要:
Given a collection of numbers, return all possible permutations.For example,[1,2,3]have the following permutations:[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2], and[3,2,1].DFS 1 class Solution { 2 private: 3 vector<vector<int> > ret; 4 bool canUse[100]; 5 int a[100]; 6 public: 7 void dfs(... 阅读全文
摘要:
The set[1,2,3,…,n]contains a total ofn! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, forn= 3):"123""132""213""231""312""321"Givennandk, return thekthpermutation sequence.Not 阅读全文
摘要:
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree andsum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1return[ [5,4,11,2]... 阅读全文
摘要:
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree andsum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ ... 阅读全文
摘要:
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... 阅读全文