leetcode刷题记录

1.二分法

cpp 复制代码
class Solution {
public:
    int search(vector<int> &nums, int target) 
	{
        int left = 0, right = nums.size() - 1;
		while(left <= right) {
			int mid = left + ((right - left) >> 1);
			if(nums[mid] > target) {
				right = mid -1;
			} else if(nums[mid] < target) {
				left = mid + 1;
			} else {
				return mid;
			}
		} 
		return -1;
    }
};

2.搜索插入位置

cpp 复制代码
class Solution {
public:
    int searchInsert(vector<int>& nums, int target) 
	{
		int left = 0, right = nums.size() - 1;
		while(left <= right) {
			int mid = left + ((right - left) >> 1);
			if(nums[mid] >= target) {
				right = mid -1;
			} else if(nums[mid] < target) {
				left = mid + 1;
			} 
		} 
		return left;
	}
};

3.搜索第一和最后一个位置

cpp 复制代码
class Solution {
public:
	int binarySearch(vector<int>& nums, int target, bool equ)
	{
		int left = 0, right = nums.size() - 1;
		while(left <= right) {
			int mid = left + ((right - left) >> 1);
			if(nums[mid] > target || (equ && nums[mid] >= target)) {
				right = mid -1;
			} else {
				left = mid + 1;
			} 
		} 
		return left;
	}
    vector<int> searchRange(vector<int>& nums, int target) {
		int leftIndex = binarySearch(nums, target, true);
		int rightIndex = binarySearch(nums, target, false) - 1;
		if(leftIndex <= rightIndex && rightIndex < nums.size() && nums[leftIndex] == target && nums[rightIndex] == target) {
			return vector<int>{leftIndex, rightIndex};
		}
		return vector<int> {-1, -1};
    }
};

4.移除元素

cpp 复制代码
// 快慢指针 
class Solution {
public:
    int removeElement(vector<int>& nums, int val) 
	{
		int slow = 0, fast = 0;
		for( ; fast < nums.size(); ++fast) {
			if(nums[fast] != val) {
				nums[slow++] = nums[fast];
			}
		}
		return slow;
    }
};

// 相向双指针法
class Solution {
public:
	int removeElement(vector<int>& nums, int val) 
	{
 		int left = 0, right = nums.size() - 1;
		 while(left <= right) {
		 	while (left <= right && nums[left] != val) {
			 	++left;
			 }
			 while (left <= right && nums[right] == val) {
			 	--right;
			 }
			 if(left < right) {
			 	nums[left++] = nums[right--];
			 }
		 } 
		 return left;
    }
};

5.有序数组的平方

cpp 复制代码
class Solution {
public:
    vector<int> sortedSquares(vector<int>& nums) {
		int left = 0, right = nums.size() -1;
		vector<int> result;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
		while(left <= right) {
			long long a = nums[left] * nums[left];
			long long b = nums[right] * nums[right];
			if (a < b) {
				result.push_back(a);
				left++;
			} else {
				result.push_back(b);
				right--;
			}
		}
		return result;
    }
};

6.长度最小的子数组

cpp 复制代码
class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
		int result = INT32_MAX;
		int start = 0;
		int sublength = 0;
		int sum = 0;
		for(int end = 0; end < nums.size(); end++) {
			sum += nums[end];
			while(sum >= target) {
				sublength = end - start + 1;
				result = sublength < result ? sublength :  result;
				sum -= nums[start++];
			}
		}
		return result == INT32_MAX ? 0 : result;
    }
};

7.水果成篮

cpp 复制代码
class Solution {
public:
    int totalFruit(vector<int>& fruits) 
	{
		int start = 0, end = 0, subLength = 0, result = 0;
		unordered_map<int, int> um;
		for (; end < fruits.size(); end++) {
			++um[fruits[end]];
			while (um.size() > 2) {
				auto it = um.find(fruits[start]);
				--it->second;
				if (it->second == 0) {
					um.erase(it);
				}
				++start;
			}
			subLength = end - start + 1;
			result = max(result, subLength);
		}
		return result;
    }
};
// 缩小窗口时也可以直接用下标进行减少,通过键值进行删除,
// 需要注意的是,需要减少的是um[fruits[start]]而不是fruits[start]
class Solution {
public:
    int totalFruit(vector<int>& fruits) {
		int start = 0, end = 0, subLength = 0, result = 0;
		unordered_map<int, int> um;
		for(; end < fruits.size(); end++) {
			++um[fruits[end]];
			while(um.size() > 2) {
				--um[fruits[start]];
				if(um[fruits[start]] == 0) {
					um.erase(fruits[start]);
				}
                ++start;
			}
			subLength = end - start + 1;
			result = max(result, subLength);
		}
		return result;
    }
};

8.最长无重复子串

cpp 复制代码
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
		unordered_map<char, int> win;
		int left = 0,right = 0;
		int len = 0;
		while(right < s.size()) {
			char c = s[right++];
			win[c]++;
			while(win[c] > 1) {
				char d = s[left--];
				win[d]--;
			}
			len = max(len, right - left);
		}
		return len;
    }
};

9.字符串排列

cpp 复制代码
class Solution {
public:
    bool checkInclusion(string s1, string s2) {
		unordered_map<char, int>need, win;
		for(const char& c : s1) need[c]++;
		int left = 0, right = 0;
		int valid = 0;
		while(right < s2.size()) {
			char c = s2[right++];
			if(need.count(c)) {
				win[c]++;
				if(win[c] == need[c]) {
					valid++;
				}
			}
			
			if(right - left == s1.size()) {
				if(valid == need.size()) {
					return true;
				}
				char d = s2[left++];
				if(need.count(d)) {
					if(win[d] == need[d]) {
						valid--;
					}
					win[d]--;
				}
			}
		}
		return false;
    }
};

10.所有字母异位词

cpp 复制代码
class Solution {
public:
    vector<int> findAnagrams(string s, string p) {
		unordered_map<char, int>need, win;
			for(const char& c : p) need[c]++;
			int left = 0, right = 0;
			int valid = 0;
			vector<int> res;
			while(right < s.size()) {
				char c = s[right++];
				if(need.count(c)) {
					win[c]++;
					if(win[c] == need[c]) {
						valid++;
					}
				}

				if(right - left == p.size()) {
					if(valid == need.size()) {
						res.push_back(left);
					}
					char d = s[left++];
					if(need.count(d)) {
						if(win[d] == need[d]) {
							valid--;
						}
						win[d]--;
					}
				}
			}
		return res;
    }
}

11.最小覆盖子串

cpp 复制代码
class Solution {
public:
    string minWindow(string s, string t) {
		unordered_map<char, int> need, win;
	   for(const char& c : t) need[c]++;
	   int left = 0, right = 0;
	   int valid = 0;
	   int start = 0, len = INT32_MAX;
	   while(right < s.size()) {
	   		char c = s[right++];
	   		if(need.count(c)) {
			   	win[c]++;
			   	if(win[c] == need[c]) {
				   	valid++;
				   }
			}
			
			while(left < right && valid == need.size()) {
				if(right - left < len) {
					len = right - left;
					start = left;
				}
				
				char d = s[left++];
				if (need.count(d)) {
					if(win[d] == need[d]) {
						valid--;
					}
					win[d]--;
				}
			}
	   }
	   return len = = INT32_MAX ? "" : s.substr(start, len);
    }
};

12.哈希解法

cpp 复制代码
class Solution {
public:
    string minWindow(string s, string t) {
        vector<int>need(128,0); // 定义哈希数组存储,其中ASCII码共有128个符号
		for(const char& c : t) {
			need[c]++;
		}
		int left = 0, right = 0;
		int valid = t.size();
		int start = 0, len = INT32_MAX;
		while(right < s.size()) {
			char c = s[right++];
			if(need[c] > 0) {    // 只有need中的元素,才可能大于0,因为right遍历是减少,left最多让元素数量恢复原始大小,而只有need中元素初始为大于0
				valid--;
			}
			need[c]--;   // 先把右边的字符加入窗口
			if(valid == 0) {    // 窗口中已经包含所需的全部字符
				while(need[s[left]] < 0) {  // 当左边为无效元素或者多余元素(need中过剩的元素)时,缩减窗口
					need[s[left]]++;
					left++;
				}   // while循环后,刚好到达一个必不可少的元素
				if(right  - left < len) {   // 更新答案
					len = right - left; // 更新最小长度
					start = left;    // 更新起始位置
				}
				char d = s[left++];
				need[d]++;  // 左边界右移之前需要释放need[s[l]]
				valid++;    // 释放了必要元素,valid增加
			}
		}
		return len == INT32_MAX ? "" : s.substr(start,len);
    }
};
相关推荐
shymoy1 分钟前
Radix Sorts
数据结构·算法·排序算法
风影小子10 分钟前
注册登录学生管理系统小项目
算法
黑龙江亿林等保12 分钟前
深入探索哈尔滨二级等保下的负载均衡SLB及其核心算法
运维·算法·负载均衡
起名字真南14 分钟前
【OJ题解】C++实现字符串大数相乘:无BigInteger库的字符串乘积解决方案
开发语言·c++·leetcode
lucy1530275107915 分钟前
【青牛科技】GC5931:工业风扇驱动芯片的卓越替代者
人工智能·科技·单片机·嵌入式硬件·算法·机器学习
杜杜的man31 分钟前
【go从零单排】迭代器(Iterators)
开发语言·算法·golang
小沈熬夜秃头中୧⍤⃝1 小时前
【贪心算法】No.1---贪心算法(1)
算法·贪心算法
木向1 小时前
leetcode92:反转链表||
数据结构·c++·算法·leetcode·链表
阿阿越1 小时前
算法每日练 -- 双指针篇(持续更新中)
数据结构·c++·算法
skaiuijing2 小时前
Sparrow系列拓展篇:对调度层进行抽象并引入IPC机制信号量
c语言·算法·操作系统·调度算法·操作系统内核