lintcode
(1)二叉树的中序遍历:
地址链接:http://www.cnblogs.com/aly15109725486/p/7235756.html
代码:
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> ret;
if(root==NULL)return ret;
inorderHelper(ret,root);
return ret;
}
private:
void inorderHelper(vector<int>& ret,TreeNode* root)
{
if(root==NULL)return;
inorderHelper(ret,root->left);
ret.push_back(root->val);
inorderHelper(ret,root->right);
} ;
(2)最小路径和:
地址链接:http://www.cnblogs.com/aly15109725486/p/7235742.html
代码:
public class Solution {
/**
* @param grid: a list of lists of integers.
* @return: An integer, minimizes the sum of all numbers along its path
*/
int min(int a, int b){
if (a < b) return a;
else return b;
}
public int minPathSum(int[][] grid) {
// write your code here
int m = grid.length;
int n = grid[0].length;
int [][] dp = new int [m][n];
dp[0][0] = grid[0][0];
for (int i = 1; i < m; i++)
dp[i][0] = dp[i-1][0] + grid[i][0];
for (int j = 1; j < n; j++)
dp[0][j] = dp[0][j-1] + grid[0][j];
for (int i = 1; i < m; i++)
for (int j = 1; j < n; j++)
dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + grid[i][j];
return dp[m-1][n-1];
}
}
(3)二叉树的最大深度:
代码链接:http://www.cnblogs.com/aly15109725486/p/7235732.html
代码:
class Solution {
public:
/**
* @param grid: a list of lists of integers.
* @return: An integer, minimizes the sum of all numbers along its path
*/
int min(int a, int b){
if (a < b) return a;
else return b;
}
int minPathSum(vector<vector<int> > &grid) {
// write your code here
int m = grid.size();
int n = grid[0].size();
int dp[m][n];
dp[0][0] = grid[0][0];
for (int i = 1; i < m; i++)
dp[i][0] = dp[i-1][0] + grid[i][0];
for (int j = 1; j < n; j++)
dp[0][j] = dp[0][j-1] + grid[0][j];
for (int i = 1; i < m; i++)
for (int j = 1; j < n; j++)
dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + grid[i][j];
return dp[m-1][n-1];
}
(4)排列序号
代码链接:http://www.cnblogs.com/aly15109725486/p/7235725.html
代码:
public class Solution {
/**
* @param A an integer array
* @return a long integer
*/
public long permutationIndex(int[] A) {
// Write your code here
long R=0;
int low[] = new int[A.length];
long weight[] = new long[A.length];
for(int i=A.length-1;i>=0;--i){
for(int j=i+1;j<A.length;++j)
if(A[j]<A[i])
++low[i];
weight[i] = (i>=A.length-2)?(A.length-1-i):weight[i+1]*(A.length-i-1);
R += weight[i]*low[i];
}
return R+1;
}
}
(5)矩阵归零
代码链接:http://www.cnblogs.com/aly15109725486/p/7235720.html
代码:
class Solution {
public:
/**
* @param matrix: A list of lists of integers
* @return: Void
*/
void setZeroes(vector<vector<int> > &matrix) {
// write your code here
int n = matrix.size();
if(n==0)
return ;
int m = matrix[0].size();
bool firstLine = false;
bool firstColumn = false;
for(int i=0;i<n;i++)
if(matrix[i][0]==0)
firstColumn=true;
for(int i=0;i<m;i++)
if(matrix[0][i]==0)
firstLine = true;
for(int i=1;i<n;i++)
{
for(int j=1;j<m;j++)
{
if(matrix[i][j]==0)
{
matrix[i][0]=0;
matrix[0][j]=0;
}
}
}
for(int i=1;i<n;i++)
{
if(matrix[i][0]==0)
{
for(int j=0;j<m;j++)
matrix[i][j]=0;
}
}
for(int i=1;i<m;i++)
{
if(matrix[0][i]==0)
{
for(int j=0;j<n;j++)
matrix[j][i]=0;
}
}
if(firstLine)
for(int i=0;i<m;i++)
matrix[0][i]=0;
if(firstColumn)
for(int i=0;i<n;i++)
matrix[i][0]=0;
}
};
(6)最接近的三数之和
代码链接:http://www.cnblogs.com/aly15109725486/p/7235707.html
代码:
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
int closest = nums[0] + nums[1] + nums[2];
int diff = abs(closest - target);
sort(nums.begin(), nums.end());
for (int i = 0; i < nums.size() - 2; ++i) {
int left = i + 1, right = nums.size() - 1;
while (left < right) {
int sum = nums[i] + nums[left] + nums[right];
int newDiff = abs(sum - target);
if (diff > newDiff) {
diff = newDiff;
closest = sum;
}
if (sum < target) ++left;
else --right;
}
}
return closest;
}
};
(7)买卖股票的最佳时机
代码链接:http://www.cnblogs.com/aly15109725486/p/7235695.html
代码:
class Solution {
public:
/**
* @param prices: Given an integer array
* @return: Maximum profit
*/
int maxProfit(vector<int> &price) {
int re = 0;
if(price.size()<2)
return re;
int lowest = price[0];
for(int i=1;i<price.size();i++)
{
int cur = price[i];
re = max(re,cur-lowest);
lowest = min(lowest,cur);
}
return re;
}
};
(8)交叉字符串
代码链接:http://www.cnblogs.com/aly15109725486/p/7235686.html
代码:
class Solution {
public:
/**
* Determine whether s3 is formed by interleaving of s1 and s2.
* @param s1, s2, s3: As description.
* @return: true of false.
*/
bool isInterleave(string s1, string s2, string s3) {
// write your code here
if(s3.length()!=s1.length()+s2.length())
return false;
if(s1.length()==0)
return s2==s3;
if(s2.length()==0)
return s1==s3;
vector<vector<bool> > dp(s1.length()+1,vector<bool>(s2.length()+1,false));
dp[0][0] = true;
for(int i=1;i<=s1.length();i++)
dp[i][0] = dp[i-1][0]&&(s3[i-1]==s1[i-1]);
for(int i=1;i<=s2.length();i++)
dp[0][i] = dp[0][i-1]&&(s3[i-1]==s2[i-1]);
for(int i=1;i<=s1.length();i++)
{
for(int j=1;j<=s2.length();j++)
{
int t = i+j;
if(s1[i-1]==s3[t-1])
dp[i][j] = dp[i][j]||dp[i-1][j];
if(s2[j-1]==s3[t-1])
dp[i][j] = dp[i][j]||dp[i][j-1];
}
}
return dp[s1.length()][s2.length()];
}
};
(9)全排列
代码链接:http://www.cnblogs.com/aly15109725486/p/7235678.html
代码:
void per(vector<int> nums,int start,vector<vector<int> > &result){
if(start==nums.size()-1){
result.push_back(nums);
}
else{
for(int i=start;i<nums.size();i++){
swap(nums[start],nums[i]);
per(nums,start+1,result);
swap(nums[start],nums[i]);
}
}
}
class Solution {
public:
/**
* @param nums: A list of integers.
* @return: A list of permutations.
*/
vector<vector<int> > permute(vector<int> nums) {
// write your code here
vector<vector<int> > result;
if(nums.size()==0){
result.push_back(nums);
return result;
}
per(nums,0,result);
return result;
}
};
(10)数字三角形
代码链接:http://www.cnblogs.com/aly15109725486/p/7235672.html
代码:
class Solution {
public:
/**
* @param triangle: a list of lists of integers.
* @return: An integer, minimum path sum.
*/
int minimumTotal(vector<vector<int> > &triangle) {
// write your code here
const int len = triangle.size();
vector<vector<int>> buff(len, vector<int>(len));
buff[0][0] = triangle[0][0];
for (int i = 1; i < len; i++)
{
for (int j = 0; j <= i; j++)
{
// 要取buff[i-1][j-1],和buff[i-1][j],但是要保证不越界。
// 第i-1行的j的取值范围为[0, i-1]
int lo = max(0, j-1);
int hi = min(j, i-1);
buff[i][j] = min(buff[i-1][lo], buff[i-1][hi]) + triangle[i][j];
}
}
int res = buff[len-1][0];
for (int i = 1; i < len; i++)
res = min(res, buff[len-1][i]);
return res;
}
};