1. LeetCode 11. 盛最多水的容器
大概思路:
- 双指针l和r指向最左和最右直线,左右指针逐渐往中间移动,一边移动一边更新答案。
- 假设l先指向了最优解左直线,则r指向最优解右直线右边的直线,若
h[r]<h[l],则r指向直线为短板,r左移,若h[r]>=h[l],则此时l指向的直线为短板,此时r就指向了最优解右直线。- 因此移动规则就是哪边低哪边的指针往中间移动,这样能保证两个指针在移动过程中的某个时刻指向最优解两端直线。
class Solution {
public:
int maxArea(vector<int>& height) {
int res = 0;
int l = 0, r = height.size() - 1;
while(l < r)
{
res = max(res, min(height[l], height[r]) * (r - l));
if(height[l] < height[r])l++;
else r--;
}
return res;
}
};
2 .LeetCode 12. 整数转罗马数字
大概思路:
- 一道找规律的题目,抽取I,IV,V,IX来分析,其他可类比。
- 因为罗马数字可以看做是多个部分之和,所以可以建立一个整数到罗马数字的映射,只要整数大于等于该罗马数字,则将该罗马数字放入答案,整数减去该罗马数字(例如整数7,7>=5,则将V放入答案,7-5=2>=1,则答案加I,2-1=1>=1,则答案加I,最终答案为VII)
- 对于一个小于10的正数,只用I来表示只能表示出1~3,用V和I只能5~8和1~3,则还有4和9用IV和IX来表示。
class Solution {
public:
string intToRoman(int num) {
string res;
int a[13] = {
1, 4, 5, 9,
10, 40, 50, 90,
100, 400, 500, 900,
1000
};
string b[13] = {
"I", "IV", "V", "IX",
"X", "XL", "L", "XC",
"C", "CD", "D", "CM",
"M"
};
for(int i = 12; i >= 0; i--)
{
while(num >= a[i])
{
num -= a[i];
res += b[i];
}
}
return res;
}
};
3. LeetCode 15. 三数之和
大概思路:
- 因为题目要求a+b+c=0,则若a固定了,则要求b+c=-a(常数),若将数组从小到大排序,a可以通过一层循环选取(即nums[i]),b和c可以通过对a的右边直到数组末尾这一段区间使用双指针算法求得。
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>>res;
sort(nums.begin(), nums.end());
for(int i = 0; i < nums.size(); i++)
{
if(i && nums[i] == nums[i - 1])continue;//a去重
for(int j = i + 1, k = nums.size() - 1; j < k; j++)
{
if(j > i + 1 && nums[j] == nums[j - 1])continue;//b去重,j>i+1与i>0同理
while(k - 1 > j && nums[i] + nums[j] + nums[k] > 0)k--;//即使c有重复,那也只会选最右边的那个
if(nums[i] + nums[j] + nums[k] == 0)
res.push_back({nums[i], nums[j], nums[k]});
}
}
return res;
}
};
4. LeetCode 23. 合并K个排序链表
大概思路:
- 若合并两个升序链表,可以直接归并,时间复杂度为o(n),n为链表总长度。
- 合并2个以上升序链表,则可以利用小根堆
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
struct Cmp{
bool operator()(ListNode* a, ListNode* b)
{
return a->val > b->val;
}
};
ListNode* dummy = new ListNode(-1);
ListNode* tail = dummy;
priority_queue<ListNode*, vector<ListNode*>, Cmp>heap;
for(ListNode* l : lists)
if(l)heap.push(l);
while(heap.size())
{
ListNode* t = heap.top();
heap.pop();
tail = tail -> next = t;
if(t -> next)heap.push(t -> next);
}
return dummy -> next;
}
};
5. LeetCode 30. 串联所有单词的子串
大概思路:
- 第一个关键点是将给定字符串分组,因为每个单词固定长度,设长度为w,则可以将整个串分为w组,例如第i组子串的所有单词为s[i]~s[i+w-1], s[i+w]~s[i+2*w-1],…m
- 设words数组元素个数为m,类似于滑动窗口,m*w长度的滑动窗口从相应组起点开始滑动,单词滑动步数为w,维护窗口内单词与words中单词一一匹配成功的数目,若成功匹配数目为m,则该窗口代表的子串即为串联所有单词的子串,记录起点。
class Solution {
public:
vector<int> findSubstring(string s, vector<string>& words) {
vector<int>res;
if(words.empty())return res;
int n = s.size(), m = words.size(), w = words[0].size();
unordered_map<string, int>tot;
for(auto word : words)tot[word]++;
for(int i = 0; i < w; i++)//共w组,起点不同
{
int cnt = 0;//匹配成功数
unordered_map<string, int>wd;
for(int j = i; j + w <= n; j += w)
{
//窗口长度达到m*w后进一个单词出一个单词
//出一个单词
if(j >= i + m * w)
{
string word = s.substr(j - m * w, w);
wd[word]--;
if(wd[word] < tot[word])cnt--;//匹配成功数减少
}
//进一个单词
string word = s.substr(j, w);
wd[word]++;
if(wd[word] <= tot[word])cnt++;//匹配成功数增加
if(cnt == m)res.push_back(j - (m - 1) * w);
}
}
return res;
}
};