Leetcode Note
算法刷题笔记
Leetcode-11. Container With Most Water
Method: (对撞指针)每次保留两指针中最大的那个即可求得最大的面积
Runtime: 16 ms, faster than 95.65% of C++ online submissions for Container With Most Water.
Memory Usage: 9.9 MB, less than 43.30% of C++ online submissions for Container With Most Water.
class Solution {
public:
int maxArea(vector<int>& height) {
int i = 0;
int r = height.size()-1;
long long max = -1;
while (i<r)
{
long long area = (r - i)*min(height[r], height[i]);
if (area > max) max = area;
if (height[r] > height[i]) i++;
else r--;
}
return max;
}
};
Leetcode-26 Remove Duplicates from Sorted Array
Method: temp 作为一个标志表示遍历过的数,用temp来比较是否重复。
Runtime: 16 ms, faster than 99.08% of C++ online submissions for Remove Duplicates from Sorted Array.
Memory Usage: 9.8 MB, less than 97.50% of C++ online submissions for Remove Duplicates from Sorted Array.
int removeDuplicates(vector<int>& nums) {
int length = 0;
int temp = 99999;
for (int i = 0;i < nums.size();i++) {
if (nums[i] != temp) {
nums[length++] = nums[i];
temp = nums[i];
}
}
return length;
}
Leetcode-27 Remove Element
Method: k为非val值的个数, 查询到一个重复值$‘i’$则继续往下遍历,若非val值则进行赋值。
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Remove Element.
Memory Usage: 8.5 MB, less than 97.06% of C++ online submissions for Remove Element.
int removeElement(vector<int>& nums, int val) {
int k = 0;
int m = 0;
for (int i = 0;i < nums.size();i++) {
if (nums[i] == val) {
m++;
}
else {
if (k != m) {
nums[k] = nums[m];
}
k++;m++;
}
}
return k;
}
Leetcode-75 Sort Color
Method:
- 可以采用计数排序,统计0,1,2个数然后按数量插入就好。
Runtime: 4 ms, faster than 68.84% of C++ online submissions for Sort Colors.
Memory Usage: 8.5 MB, less than 98.25% of C++ online submissions for Sort Colors.
void sortColors(vector<int>& nums) {
const int n = 3;
int count[n] = { 0 };
for (int i = 0;i < nums.size();i++) {
count[nums[i]]++;
}
int index = 0;
for (int i = 0;i < n;i++) {
for (int j = 0;j < count[i];j++) nums[index++] = i;
}
}
- 三路快排方法实现,只需要执行一次三路快排,遍历一遍把输入序列分成以下三块:
arr[0 ... zero] == 0 / arr[zero+1 ... i-1] == 1 / arr[two ... n-1] == 2
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Sort Colors.
Memory Usage: 8.5 MB, less than 96.49% of C++ online submissions for Sort Colors
void sortColors(vector<int>& nums) {
int zero = -1; //nums[0 .. zero] == 0
int two = nums.size(); // nums[two..-n-1] == 2
for (int i = 0;i < two;) {
if (nums[i] == 1)
i++;
else if (nums[i] == 2) {
two--;
swap(nums[i], nums[two]);
}
else {// nums[i] == 0
zero++;
swap(nums[zero], nums[i]);
i++;
}
}
}
Leetcode-80 Remove Duplicates from Sorted Array II
Method: @k 为合法数字的数量,@t 判断一类数字是否<2, @temp 用于比较的数字的拷贝, 迭代位置与合法数字的位置做交换,一次遍历即可完成。
Runtime: 8 ms, faster than 99.12% of C++ online submissions for Remove Duplicates from Sorted Array II.
Memory Usage: 8.8 MB, less than 89.47% of C++ online submissions for Remove Duplicates from Sorted Array II.
int removeDuplicates(vector<int>& nums) {
int k = 0;
bool t = true;
int temp = 99999;
for (int i = 0;i < nums.size();i++) {
if (nums[i] == temp) {
if (t == true) continue;
else {
t = true;
if (k != i) {
nums[k] = nums[i];
}
k++;
}
}
else {
t = false;
temp = nums[i];
if (k != i) {
nums[k] = nums[i];
}
k++;
}
}
return k;
}
Leetcode-88 Merge Sorted Array
Method: 归并排序,设置一个外置数组用以空间换时间,时间为遍历一遍+拷贝长数组一遍的时间。
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Merge Sorted Array.
Memory Usage: 8.7 MB, less than 80.43% of C++ online submissions for Merge Sorted Array.、
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) {
int p1 = 0, p2 = 0;
vector<int> temp;
for (int i = 0;i < m;i++) {
temp.push_back(nums1[i]);
}
int index = 0;
while (p1 < m && p2 < n)
{
if (temp[p1] <= nums2[p2]) {
if (index != p1)
nums1[index] = temp[p1];
index++;
p1++;
}
else {
nums1[index++] = nums2[p2++];
}
}
if (p1 != m) for (;p1 < m;p1++) nums1[index++] = temp[p1];
else for (;p2 < n;p2++) nums1[index++] = nums2[p2];
}
Leetcode-125. Valid Palindrome
Method: (对撞指针)先判断字符的有效性,再判断是否要转换大小写,最后在相同类型的文字格式下进行字符的比较。
Runtime: 12 ms, faster than 39.15% of C++ online submissions for Valid Palindrome.
Memory Usage: 9.3 MB, less than 81.63% of C++ online submissions for Valid Palindrome.
bool isPalindrome(string s) {
int p = 0;
int r = s.length() - 1;
while (p<r)
{
if ((s[p] >= 'A'&& s[p] <= 'Z') || (s[p] >= 'a'&&s[p] <= 'z') || (s[p] >= '0'&&s[p] <= '9')) {
if ((s[r] >= 'A'&& s[r] <= 'Z') || (s[r] >= 'a'&&s[r] <= 'z') || (s[r] >= '0'&&s[r] <= '9')) {
char c = s[p]<'A'?s[p]:(s[p] >= 'a' ? s[p] : s[p] - 'A' + 'a');
char k = s[r]<'A' ? s[r] : (s[r] >= 'a' ? s[r] : s[r] - 'A' + 'a');
if (c != k) {
return false;
}
else {
p++;
r--;
}
}
else {
r--;
continue;
}
}
else {
p++;
}
}
return true;
}
Leetcode-167 Two Sum II - Input array is sorted(*)
Method:
- (二分查找)从起始点开始遍历,在除了遍历元素$num[i]$外的有序数组结构中采用二分搜索的方法查找$target-nums[i]$可以使时间复杂度控制在$O(nlogn)$.
- (对撞指针)前后各一个位置的指针,计算
$ nums[i] + nums[j] < target ——> i++ $
$ nums[i] + nums[j] > target ——> j++ $
只需要一次遍历即可找到答案
Runtime: 4 ms, faster than 96.98% of C++ online submissions for Two Sum II - Input array is sorted.
Memory Usage: 9.5 MB, less than 82.35% of C++ online submissions for Two Sum II - Input array is sorted.
vector<int> twoSum(vector<int>& numbers, int target) {
assert(numbers.size() >= 2);
int l = 0, r = numbers.size() - 1;
while (l < r)
{
if (numbers[l] + numbers[r] == target) {
int res[2] = { l + 1, r + 1 };
return vector<int>(res, res + 2);
}
else if (numbers[l] + numbers[r] < target) l++;
else r--; //numbers[l] + numbers[r] > target
}
throw invalid_argument("The input has no solution.");
}
Leetcode-215. Kth Largest Element in an Array(*)
Method: (*快速排序)运用快排partition操作 返回标定点位置,我们能够知道,标定点的位置是每一轮快排过后确定的最终位置因此只要 标定点位置p与想要查找的位置k相重则说明标定点已经排序完成,只需要返回标定点在数组中的值就好了。
Runtime: 28 ms, faster than 29.31% of C++ online submissions for Kth Largest Element in an Array.
Memory Usage: 11 MB, less than 6.06% of C++ online submissions for Kth Largest Element in an Array.
int _partition(vector<int>& arr, int l, int r) {
int temp = arr[l];
int i = l + 1, j = r;
while (true)
{
while (i <= r && arr[i] > temp)i++;
while (j >= l+1 && arr[j] < temp)j--;
if (i > j) break;
swap(arr[i], arr[j]);
i++;
j--;
}
swap(arr[l], arr[j]);
return j;
}
int __quickSort(vector<int>& arr, int l, int r, int k) {
int p = _partition(arr, l, r);
if (p != k-1) {
if(p > k-1)
return __quickSort(arr, l, p - 1, k);
else return __quickSort(arr, p + 1, r, k);
}
else
return arr[p];
}
int findKthLargest(vector<int>& nums, int k) {
return __quickSort(nums, 0, nums.size()-1, k);
}
Leetcode-283 Move Zeroes
Method:主要用了一个标定点** $i$ **来标识一轮遍历过程中的非零数的个数,然后将每个非零数按$i$的位置存放。时间复杂度$O(n)$;空间复杂度$O(1)$。
Others Method: 使用交换思想,只需要一轮遍历.
Runtime: 12 ms, faster than 96.77% of C++ online submissions for Move Zeroes.
Memory Usage: 9.3 MB, less than 100.00% of C++ online submissions for Move Zeroes.
void moveZeroes(vector<int>& nums) {
int i = 0;
for (int j = 0;j < nums.size();j++) {
if (nums[j] != 0) {
nums[i++] = nums[j];
}
}
for (;i < nums.size();i++) {
nums[i] = 0;
}
}
//交换方法
void moveZeroes(vector<int>& nums) {
int i = 0;
for (int j = 0;j < nums.size();j++) {
if (nums[j] != 0) {
if (i != j) {
int temp = nums[i];
nums[i++] = nums[j];
nums[j] = temp;
}
else //i==k
i++;
}
}
}
Leetcode 344. Reverse String
Method:(对撞指针)简单用法。
Runtime: 44 ms, faster than 92.20% of C++ online submissions for Reverse String.
Memory Usage: 15.2 MB, less than 86.59% of C++ online submissions for Reverse String.
void reverseString(vector<char>& s) {
int p = 0;
int r = s.size()-1;
while (p < r)
{
swap(s[p++], s[r--]);
}
}
Leetcode 345. Reverse Vowels of a String
Method:(对撞指针)简单用法。
Runtime: 4 ms, faster than 99.53% of C++ online submissions for Reverse Vowels of a String.
Memory Usage: 10 MB, less than 87.88% of C++ online submissions for Reverse Vowels of a String
class Solution {
public:
string reverseVowels(string s) {
int p = 0;
int r = s.length()-1;
while (p < r)
{
if (s[p] == 'a' || s[p] == 'A' || s[p] == 'e' || s[p] == 'E' || s[p] == 'i' || s[p] == 'I' || s[p] == 'o' || s[p] == 'O'
|| s[p] == 'u' || s[p] == 'U')
if (s[r] == 'a' || s[r] == 'A' || s[r] == 'e' || s[r] == 'E' || s[r] == 'i' || s[r] == 'I' || s[r] == 'o' || s[r] == 'O'
|| s[r] == 'u' || s[r] == 'U')
swap(s[p++], s[r--]);
else
r--;
else
p++;
}
return s;
}
};
Leetcode Note的更多相关文章
- LeetCode Note 1st,practice makes perfect
1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a ...
- LeetCode - Word Subsets
We are given two arrays A and B of words. Each word is a string of lowercase letters. Now, say that ...
- [LeetCode] 916. Word Subsets 单词子集合
We are given two arrays A and B of words. Each word is a string of lowercase letters. Now, say that ...
- LeetCode 916. Word Subsets
原题链接在这里:https://leetcode.com/problems/word-subsets/ 题目: We are given two arrays A and B of words. E ...
- 【LeetCode】916. Word Subsets 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/word-sub ...
- [Swift]LeetCode916.单词子集 | Word Subsets
We are given two arrays A and B of words. Each word is a string of lowercase letters. Now, say that ...
- 916. Word Subsets
We are given two arrays A and B of words. Each word is a string of lowercase letters. Now, say that ...
- [LeetCode] Ransom Note 赎金条
Given an arbitrary ransom note string and another string containing letters from all th ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
随机推荐
- 关于api创建监控项,添加灵活调度的事件间隔
在api文档中没有明确说明,可以查询数据库,得到的是一个字符串,
- Problem 3 基站建设 (station.cpp)———2019.10.6
在此郑重的感激wxyww大佬 wxyww tql [题目描述]小 Z 的爸爸是一位通信工程师,他所在的通信公司最近接到了一个新的通信工程建设任务,他们需要在 C 城建设一批新的基站.C 城的城市规划做 ...
- GoCN每日新闻(2019-11-10)
GoCN每日新闻(2019-11-10) 1. Go Netpoll I/O多路复用构建原生网络模型之源码深度解析 https://taohuawu.club/go-netpoll-io-multip ...
- pytest . class
#在当前测试类的开始与结束执行. setup_class teardown_class #在每个测试方法开始与结束执行. setup teardown #在每个测试方法开始与结束执行,与setup/t ...
- load average 定义(网易面试)
1. load average 定义 linux系统中的Load对当前CPU工作量的度量.简单的说是进程队列的长度. Load Average 就是一段时间 (1 分钟.5分钟.15分钟) 内平均 L ...
- 后台接收参数报错 Required String parameter 'id' is not present
来自:https://blog.csdn.net/qq_15238647/article/details/81539287 关于ajax请求spring后台出现 Required String par ...
- 【重庆师范大学】PHP博客训练-Thinkphp
设计数据库 CREATE TABLE `user` ( `user_id` int unsigned NOT NULL AUTO_INCREMENT, `username` varchar() COM ...
- Win7下msys64安装mingw工具链
1. 安装msys64 安装到指定目录, 例如C:\msys64 2. 命令行更新 运行msys2.exe打开命令行窗口, 执行命令 pacman -Syuu 3. 修改安装源 进入msys64/et ...
- 【基础】Qt SCXML Calculator QML Example
Qt SCXML Calculator QML Example 这个系统自带的例子原本主要是用来说明SCXML机制的,但是由于计算器的经典和简洁,我认为用来练习QML非常合适,原本的例子还有一些问题, ...
- centos 查看版本 及 升级
查看系统版本: cat /etc/redhat-release(/etc/centos-release)// 或者 rpm -q centos-release [root@56 ~]# cat /e ...