class Solution {
public:
bool check(char c){return c >='0'&& c <='9'|| c >='A'&& c <='Z'|| c >='a'&& c <='z';}
bool isPalindrome(string s){for(int i =0, j = s.size()-1; i < j; i++, j--){while(i < j &&!check(s[i]))i++;while(i < j &&!check(s[j]))j--;if(s[i]!= s[j]&& s[i]!=(s[j]^32))return false;}return true;}};//使用双指针
LeetCode 88. Merge Sorted Array
class Solution {
public:voidmerge(vector<int>& nums1,int m, vector<int>& nums2,int n){int k = m + n -1;//m,n现在是下标
m--, n--;//从最大的数开始比较while(m >=0&& n >=0){if(nums1[m]> nums2[n])nums1[k--]= nums1[m--];else nums1[k--]= nums2[n--];}while(n >=0)nums1[k--]= nums2[n--];}};
LeetCode 278. First Bad Version
// Forward declaration of isBadVersion API.
bool isBadVersion(int version);
class Solution {
public:intfirstBadVersion(int n){//非常经典的二分问题int l =1, r = n;while(l < r){int mid =(l +0ll+ r)>>1;if(isBadVersion(mid))r = mid;else l = mid +1;}return r;}};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/// BST的中序遍历是按从小到大的顺序排序的
class BSTIterator {
public:
stack<TreeNode*> stk;BSTIterator(TreeNode* root){while(root){
stk.push(root);
root = root->left;}}/** @return the next smallest number */intnext(){auto t = stk.top();
stk.pop();for(auto p = t->right; p; p = p->left){
stk.push(p);}return t->val;}/** @return whether we have a next smallest number */
bool hasNext(){return!stk.empty();}};/**
* Your BSTIterator object will be instantiated and called as such:
* BSTIterator* obj = new BSTIterator(root);
* int param_1 = obj->next();
* bool param_2 = obj->hasNext();
*/
LeetCode 238. Product of Array Except Self
class Solution {
public:
vector<int>productExceptSelf(vector<int>& nums){
vector<int>res(nums.size());//这里用到了前缀积,把乘积分为前半部分和后半部分for(int i =0, t =1; i < nums.size(); i++){
res[i]= t;
t *= nums[i];}for(int i = nums.size()-1, t =1; i >=0; i--){
res[i]*= t;
t *= nums[i];}return res;}};
LeetCode 56. Merge Intervals
class Solution {
public:static bool cmp(vector<int> a, vector<int> b){return a[0]< b[0];}
vector<vector<int>>merge(vector<vector<int>>& intervals){
vector<vector<int>> res;if(intervals.empty())return res;//这里用到了自定义排序sort(intervals.begin(), intervals.end(), cmp);int st = intervals[0][0], ed = intervals[0][1];//有两种方案for(int i =1; i < intervals.size(); i++){//第一种,区间需要新开一个if(ed < intervals[i][0]){
res.push_back({st, ed});
st = intervals[i][0], ed = intervals[i][1];}else{//第二种,是合并同一个区间
ed =max(ed, intervals[i][1]);}}
res.push_back({st, ed});return res;}};
LeetCode 75. Sort Colors
class Solution {
public:voidsortColors(vector<int>& nums){int red =0, blue = nums.size()-1;//方法二:这种方法很巧妙//设置两个边界线,一个代表0和1的分界线,一个代表1和2的分界线//时间复杂度O(n),遇到0,则修改0,1分界线,遇到1继续遍历,遇到2则修改1和2的分界线for(int i =0; i <= blue;){if(nums[i]==0)swap(nums[i++], nums[red++]);elseif(nums[i]==2)swap(nums[i], nums[blue--]);else i++;}// //方法一:直接用桶排序遍历一遍,再输出即可,思路简单// int count[3] = {0};// for(int i = 0; i < nums.size(); i++)// {// count[nums[i]]++;// }// for(int i = 0, t = 0; i < 3; i++)// {// for(int j = 0; j < count[i]; j++)// {// nums[t++] = i;// }// }}};
LeetCode 133. Clone Graph
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> neighbors;
Node() {}
Node(int _val, vector<Node*> _neighbors) {
val = _val;
neighbors = _neighbors;
}
};
*///这题不是很懂//思路//1.先把每一个点复制一遍,维护哈希表,可以从原点得到新点;//2.遍历所有边(a,b),在新点之间,建立新的边(a',b')
class Solution {
public:
unordered_map<int, Node*> hash;
Node*cloneGraph(Node* node){if(!node)return node;auto root = new Node(node->val, vector<Node*>());
hash[node->val]= root;dfs(node);return root;}voiddfs(Node* node){for(auto&neighbor:node->neighbors){if(!hash.count(neighbor->val)){
hash[neighbor->val]= new Node(neighbor->val, vector<Node*>());dfs(neighbor);}
hash[node->val]->neighbors.push_back(hash[neighbor->val]);}}};
LeetCode 76. Minimum Window Substring
class Solution {
public:
string minWindow(string s, string t){
string res;//使用两个哈希表一个是动态的,一个是静态的
unordered_map<char,int> S, T;for(auto c : t)T[c]++;int total = T.size();int satisfy =0;//双指针实现滑动窗口for(int i =0, j =0; i < s.size(); i ++){
S[s[i]]++;if(S[s[i]]== T[s[i]])satisfy++;while(S[s[j]]> T[s[j]])S[s[j++]]--;if(satisfy == total &&(res.empty()|| i - j +1< res.size())){
res = s.substr(j, i-j+1);}}return res;}};