Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return [1,3,3,1]
.
提前开好空间,将vector从后往前更新
vector<int> getRow(int rowIndex) {
vector<int> A(rowIndex+1, 0);
A[0] = 1;
for(int i=1; i<rowIndex+1; i++)
for(int j=i; j>=1; j--)
A[j] += A[j-1];
return A;
}
Maximum Product Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest product.
For example, given the array [2,3,-2,4]
,
the contiguous subarray [2,3]
has the largest product = 6
.
需要考虑两个点:1. 中间某个为0 2.中间的负数的处理
某个点的最大值/最小值 = min*num 或者 max*num 或者 num(比如max或min为0时)
int maxProduct (vector<int>& nums) {
int lastmaxhere = nums[0];
int lastminhere = nums[0];
int maxres = nums[0];
int maxhere, minhere;
for(int i = 1;i < nums.size(); i++){
maxhere = max(max(lastmaxhere * nums[i], lastminhere * nums[i]), nums[i]);
minhere = min(min(lastmaxhere * nums[i], lastminhere * nums[i]), nums[i]);
maxres = max(maxres, maxhere);
lastmaxhere = maxhere;
lastminhere = minhere;
}
return maxres;
}
Word Search
Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
For example,
Given board =
[ ['A','B','C','E'], ['S','F','C','S'], ['A','D','E','E'] ]word =
"ABCCED"
, -> returns
true
,
word =
"SEE"
, -> returns
true
,
word =
"ABCB"
, -> returns
false
.
将已经走过的格子使用一个特殊符号进行标记,进行递归搜索
public boolean exist(char[][] board, String word) {
for(int i = 0; i < board.length; i++)
for(int j = 0; j < board[0].length; j++){
if(exist(board, i, j, word, 0))
return true;
}
return false;
}
private boolean exist(char[][] board, int i, int j, String word, int ind){
if(ind == word.length()) return true;
if(i > board.length-1 || i <0 || j<0 || j >board[0].length-1 || board[i][j]!=word.charAt(ind))
return false;
board[i][j]='*';
boolean result = exist(board, i-1, j, word, ind+1) ||
exist(board, i, j-1, word, ind+1) ||
exist(board, i, j+1, word, ind+1) ||
exist(board, i+1, j, word, ind+1);
board[i][j] = word.charAt(ind);
return result;
}
Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋
times. The algorithm should run in linear time and in O(1) space.
点1:数组中最多出现两个
点2:Boyer-Moore Majority Vote algorithm O(1)
Majority 指超过 n/2 向下取整次
vector<int> majorityElement(vector<int>& nums) {
vector<int> rst;
int cand1 = 0,cand2 = 1,count1 = 0,count2 = 0;
for(int i=0; i < nums.size(); i++){
if(nums[i] == cand1)
count1++;
else if(nums[i] == cand2)
count2 ++;
else if(count1 == 0){
cand1 = nums[i];
count1++;
}else if(count2 == 0){
cand2 = nums[i];
count2++;
}
else{
count1--;
count2--;
}
}
count1 = count2 = 0;
for(int i = 0; i < nums.size(); i++){
if(nums[i] == cand1)
count1++;
else if(nums[i] == cand2)
count2++;
}
if(count1 > nums.size()/3)
rst.push_back(cand1);
if(count2 > nums.size()/3)
rst.push_back(cand2);
return rst;
}
Given a sorted integer array without duplicates, return the summary of its ranges.
For example, given [0,1,2,4,5,7]
, return ["0->2","4->5","7"].
Given a sorted integer array without duplicates, return the summary of its ranges.
使用两个指针,减少条件判断的代码量
vector<string> summaryRanges(vector<int>& nums) {
const int size_n = nums.size();
vector<string> res;
if ( 0 == size_n) return res;
for (int i = 0; i < size_n;) {
int start = i, end = i;
while (end + 1 < size_n && nums[end+1] == nums[end] + 1) end++;
if (end > start) res.push_back(to_string(nums[start]) + "->" + to_string(nums[end]));
else res.push_back(to_string(nums[start]));
i = end+1;
}
return res;
}