# LintCode十道题

LintCode十道题：

1.买卖股票的最佳时机

class Solution {
/**
* @param prices: Given an integer array
* @return: Maximum profit
*/
public int maxProfit(int[] prices) {
if (0 == prices.length || null == prices) return 0;
int minPrice = prices[0];
int sumProfit = 0;
for (int i = 1; i < prices.length; i++) {
if (minPrice < prices[i]) {
sumProfit += prices[i] - minPrice;
minPrice = prices[i];
} else {
minPrice = prices[i];
}
}
return sumProfit;
}

};

2.N皇后问题

class Solution {
/**
* Get all distinct N-Queen solutions
* @param n: The number of queens
* @return: All distinct solutions
* For example, A string '...Q' shows a queen on forth position
*/
ArrayList<ArrayList<String>> solveNQueens(int n) {
ArrayList<String> path = new ArrayList<String>();
ArrayList<ArrayList<String>> result = new ArrayList<ArrayList<String>>();
if (n < 0){
return result;
int cols[] = new int[n];
DFS_helper(n,result,0,cols);
return result;
}
public void DFS_helper(int nQueens,ArrayList<ArrayList<String>> result,int row,int[] cols){
if(row == nQueens ){
ArrayList<String> path = new ArrayList<String>();
for (int i = 0; i < nQueens; i++)
{
String s = "";
for(int j = 0;j< nQueens ;j++){
if(j == cols[i])
s = s + "Q";
else
s = s + ".";
}
}
}else{
for(int i = 0;i<nQueens ;i++){
cols[row] = i;
if(isValid(row,cols))
DFS_helper(nQueens,result,row + 1 ,cols);
}
}
}
public boolean isValid(int row ,int[] cols){
for(int i=0;i<row;i++){
if(cols[row] == cols[i] || Math.abs(cols[row] - cols[i]) == row - i)
return false;
}
return true;
}
};

3.全排列

class Solution {
/**
* @param nums: A list of integers.
* @return: A list of permutations.
*/
public List<List<Integer>> permute(int[] nums) {
if nums == None || len(nums) == 0:
return[]
m = len(nums) - 1
self.ans = []
self.res = []
self.fun(nums, 0, m)
return self.res

def fun(self, L, k, m):
if k == m:
self.ans = []
for i in range(m+1):
self.ans.append(L[i])
self.res.append(self.ans)
else:
for i in range(k, m+1):
L[i], L[k] = L[k], L[i]
self.fun(L, k+1, m)
L[i], L[k] = L[k], L[i]
}
};

4.两个链表的交叉

/**
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
/**
* @param headA: the first list
* @param headB: the second list
* @return: a ListNode
*/
return NULL;
int a = 1, b = 1;
while (p -> next != NULL) {
p = p -> next;
a += 1;
}
while (q -> next != NULL) {
q = q->next;
b += 1;
}
if (q != p)
return NULL;
if (a > b) {
for (int i = 0; i < a-b; i++) {
}
} else if (a < b) {
for (int j = 0; j < b-a; j++) {
}
}
}
}
};

5.二叉树的最大深度

/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param root: The root of binary tree.
* @return: An integer
*/
int maxDepth(TreeNode *root) {
if (root == NULL) {
return 0;
}
int left = maxDepth(root -> left);
int right = maxDepth(root -> right);
return (left > right) ? left + 1 : right + 1;
}
};

6.交叉字符串

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) {
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()];
}
};

7.翻转字符串

class Solution {
public:
/**
* @param s : A string
* @return : A string
*/
string reverseWords(string s) {
stringstream in;
stringstream out;
in << s;
char str[101][101];
char tempStr[1001];
in.getline(tempStr, 1001);
int num = 0;
char *t = NULL;
for (t = strtok(tempStr, " "); t != NULL; t = strtok(NULL, " ")) {
strcpy(str[num++], t);
}
for (int i = num - 1; i >= 0; i--) {
out << str[i] << " ";
}
return out.str();
}
};

8.排列序号

class Solution {
public:
/**
* @param A an integer array
* @return a long integer
*/
// 计算阶乘的函数
long long fac(int a) {
long long res = 1;
while (a != 1)
res *= a--;
return res;
}

long long permutationIndex(vector<int>& A) {
if (A.empty()) return 0;
if (A.size() == 1) return 1;

long long res = 1;
vector<int> B(A);

sort(B.begin(), B.end());
// 记录位数
int dig = A.size()-1;

int j = 0;

for (int i = 0; i < A.size()-1; ++i) {
// 找出A中每一位数字在B中的位置
while (A[i] != B[j])
j++;
res += j*fac(dig--);
B.erase(B.begin()+j);
j = 0;
}
return res;
}
};

9.爬楼梯

class Solution {
public:
/**
* @param n: An integer
* @return: An integer
*/
int climbStairs(int n) {
int one = 0;
int two = 1;
while(n>0) {
two=one+two;
one=two-one;
n--;
}
return two;

}
};

10.最小路径和

class Solution {
public:
/**
* @param grid: a list of lists of integers.
* @return: An integer, minimizes the sum of all numbers along its path
*/
int minPathSum(vector<vector<int> > &grid) {
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];
}
};

posted @ 2017-08-16 13:53  M橘子小姐  阅读(221)  评论(0编辑  收藏  举报