算法刷题笔记

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

  1. 可以采用计数排序,统计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;
}
}
  1. 三路快排方法实现,只需要执行一次三路快排,遍历一遍把输入序列分成以下三块: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:

  1. (二分查找)从起始点开始遍历,在除了遍历元素$num[i]$外的有序数组结构中采用二分搜索的方法查找$target-nums[i]$可以使时间复杂度控制在$O(nlogn)$.
  2. (对撞指针)前后各一个位置的指针,计算

    $ 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的更多相关文章

  1. 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 ...

  2. LeetCode - Word Subsets

    We are given two arrays A and B of words. Each word is a string of lowercase letters. Now, say that ...

  3. [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 ...

  4. LeetCode 916. Word Subsets

    原题链接在这里:https://leetcode.com/problems/word-subsets/ 题目: We are given two arrays A and B of words.  E ...

  5. 【LeetCode】916. Word Subsets 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/word-sub ...

  6. [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 ...

  7. 916. Word Subsets

    We are given two arrays A and B of words.  Each word is a string of lowercase letters. Now, say that ...

  8. [LeetCode] Ransom Note 赎金条

    
Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 all 
th ...

  9. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

随机推荐

  1. MongonDB

    目录 1.下载MongoDB 2.启动MongoDB 3.添加环境变量,添加启动服务 1.下载MongoDB MongoDB的官网 简单下载方法 win32/mongodb-win32-x86_64- ...

  2. JS继承2

    一.原型链继承 关键步骤: 让子类的原型对象成为父类的实例 矫正子类构造器属性 function Animal(name,age){ this.name = name; this.age = age; ...

  3. swoolefy PHP的异步、并行、高性能网络通信引擎内置了Http/WebSocket服务器端/客户端

    近半年来努力付出,项目终于要正式结项了,团队4人经历了很多困难,加班加点,最终完成了!剩下的时间将总结一下在该项目中用到知识和遇到问题.今天就从swoole说起!项目中实现异步大文件传输的功能,在服务 ...

  4. Java接口、lambda的学习

    接口的实现  :  使用interface定义:形式如下 interface Printable{ final int MAX = 100; void add(); float sum(float x ...

  5. 【POJ2676】Sudoku

    本题传送门 本题知识点:深度优先搜索 + 回溯 问题就是要让我们解决一个数独问题.如果你懂得怎么玩数独的话,那就很自然想到用暴力搜索去做题.(比如我就不会,所以先WA了一发quq) 数独符合三个条件 ...

  6. CentOS7.4下安装部署HAProxy高可用群集

    目录第一部分 实验环境第二部分 搭建配置web服务器第三部分 安装配置haproxy服务器第四部分 测试验证第五部分 haproxy配置相关详细解释 第一部分 实验环境1.一台harpoxy调度服务器 ...

  7. cocos执行tolua/genbindings.py文件,错误搜集:

    1.PYTHON_BIN not defined, use current python.这个不是错误 2.llvm toolchain not found!path: /Users/staff/Do ...

  8. 帝国cms伪静态设置方法

    众所周知,动态页面不利于收录和排名.伪静态可以完美的解决这问题,配合百度云加速CDN,可以让动态页面有静态页面一样快的访问速度. 今天开拓族给大家带来帝国CMS伪静态的详细设置方法. 1.栏目设置为动 ...

  9. DICOM中的UID

    UID形式上是一个字符串,用于唯一标识DICOM标准中各种不同信息对象,如数据元素的值表示类型.DICOM抽象语法名.传输语法.应用程序上下文名字等,以保证在各个不同的国家.地区.生产商.设备使用时的 ...

  10. python 使用夜神模拟器

    安装版本为6.2.8.0 1.模拟器安装证书 打开模拟器,点击浏览器 在浏览器里输入:mitm.it 出现如下: 选择安卓进行安装 比如:sks123 2.设置代理 输入密码:sks123 上面刚才设 ...