Compare commits

..

13 Commits

Author SHA1 Message Date
chefyuan
6d96954aa7 chefyuan 2021-04-27 20:20:10 +08:00
chefyuan
e7919c2bbd chefyuan 2021-04-27 19:13:10 +08:00
chefyuan
6fd6d9917a chefyuan 2021-04-27 19:11:41 +08:00
chefyuan
69877f9029 Merge branch 'main' of https://github.com/chefyuan/algorithm-base into main 2021-04-27 19:09:13 +08:00
chefyuan
1a0aa4c5ef chefyuan 2021-04-27 19:06:20 +08:00
算法基地
5bae992b9a Merge pull request #20 from daluozha/main
leetcode 141、206、225,剑指offer 09 补充js版代码
2021-04-27 19:00:29 +08:00
daluozha
46b5f5eb7d leetcode 141 补充js版解法 2021-04-27 18:14:29 +08:00
daluozha
0b5744e2fa 剑指offer09 补充js版解法 2021-04-27 18:13:37 +08:00
daluozha
4e53da9e3e leetcode 225 补充js版解法 2021-04-27 18:13:08 +08:00
daluozha
39683f1794 leetcode 206 补充js版解法 2021-04-27 18:12:18 +08:00
算法基地
5c0f7ca21b Merge pull request #19 from coderhare/main
前缀和专题补充代码
2021-04-27 17:44:23 +08:00
MoriSummer
376bec88ef Update leetcode560和为K的子数组.md 2021-04-27 15:13:00 +08:00
3119005212
8ef63207ac 前缀和代码 2021-04-27 15:11:05 +08:00
10 changed files with 276 additions and 6 deletions

View File

@@ -46,15 +46,15 @@
> 另外我和几位老哥给刚开始刷题但是不知道从哪里开始刷的同学整理了一份 刷题大纲 可以先按这个顺序刷刷完之后应该就能入门当然该仓库的大部分题解也是来自那个大纲 > 另外我和几位老哥给刚开始刷题但是不知道从哪里开始刷的同学整理了一份 刷题大纲 可以先按这个顺序刷刷完之后应该就能入门当然该仓库的大部分题解也是来自那个大纲
> >
> 需要的同学可以去刷题群里看一下 > 需要的同学可以扫描下方二维码回复刷题大纲获取
我把我之前学习算法时**对我帮助很大的算法书籍谷歌大神的刷题笔记面经等整理在了云盘**大家如果需要可以自己下载比较适合有一定基础的同学 我把我之前学习算法时**对我帮助很大的算法书籍谷歌大神的刷题笔记面经等整理在了云盘**需要进阶的同学可以自己下载比较适合有一定基础的同学
> **下载地址[点我](https://pan.baidu.com/s/1xiCHAbX7HSGyE9yX5KPvJA ) 提取码jrnb** > **下载地址[点我](https://pan.baidu.com/s/1xiCHAbX7HSGyE9yX5KPvJA ) 提取码jrnb**
**想要和题友一起刷题互相监督**的同学扫描下方二维码进入如果二维码过期或者满 200 不能扫描进入时可以添加我的微信 [**tan45du_one**](https://cdn.jsdelivr.net/gh/tan45du/tan45du.github.io@master/个人微信.15egrcgqd94w.jpg) 备注 **GitHub 进群**,我会尽快拉你,群里大佬挺多,可以学习下他们的学习方法,拜了个拜 想要手机阅读的大佬可以来我的[公众号袁厨的算法小屋](https://cdn.jsdelivr.net/gh/tan45du/test@master/微信图片_20210320152235.wp1ysdbibsw.png)进行阅读,两个平台同步更新,另外想要和题友们一起刷题**的同学可以来我的小屋,**点击**刷题小队**进入,另外群里老哥还会不定期发布内推消息,面经等,需要的可以进一下,不过来的时候**记得备注**,希望这个群能对你们有一丢丢帮助吧,一起加油
<div align="center"> <img src="https://cdn.jsdelivr.net/gh/tan45du/photobed@master/微信图片编辑_20210427140351.21ladtlz7p4w.jpg" width = "150px" hight = "150px"/> </div> <div align="center"> <img src="https://cdn.jsdelivr.net/gh/tan45du/test@master/微信图片_20210320152235.wp1ysdbibsw.png" width = "150px" hight = "150px"/> </div>

View File

@@ -43,6 +43,8 @@
我们来解析一下哈希表key 代表的是含有 1 个奇数的前缀区间value 代表这种子区间的个数含有两个也就是nums[0],nums[0,1].后面含义相同那我们下面直接看代码吧一下就能读懂 我们来解析一下哈希表key 代表的是含有 1 个奇数的前缀区间value 代表这种子区间的个数含有两个也就是nums[0],nums[0,1].后面含义相同那我们下面直接看代码吧一下就能读懂
Java Code:
```java ```java
class Solution { class Solution {
public int numberOfSubarrays(int[] nums, int k) { public int numberOfSubarrays(int[] nums, int k) {
@@ -70,8 +72,40 @@ class Solution {
} }
``` ```
C++ Code:
```cpp
class Solution {
public:
int numberOfSubarrays(vector<int>& nums, int k) {
if (nums.size() == 0) {
return 0;
}
map <int, int> m;
//统计奇数个数,相当于我们的 presum
int oddnum = 0;
int count = 0;
m.insert({0,1});
for (int & x : nums) {
// 统计奇数个数
oddnum += x & 1;
// 发现存在,则 count增加
if (m.find(oddnum - k) != m.end()) {
count += m[oddnum - k];
}
//存入
if(m.find(oddnum) != m.end()) m[oddnum]++;
else m[oddnum] = 1;
}
return count;
}
};
```
但是也有一点不同就是我们是统计奇数的个数数组中的奇数个数肯定不会超过原数组的长度所以这个题目中我们可以用数组来模拟 HashMap 用数组的索引来模拟 HashMap key用值来模拟哈希表的 value下面我们直接看代码吧 但是也有一点不同就是我们是统计奇数的个数数组中的奇数个数肯定不会超过原数组的长度所以这个题目中我们可以用数组来模拟 HashMap 用数组的索引来模拟 HashMap key用值来模拟哈希表的 value下面我们直接看代码吧
Java Code:
```java ```java
class Solution { class Solution {
public int numberOfSubarrays(int[] nums, int k) { public int numberOfSubarrays(int[] nums, int k) {
@@ -93,4 +127,27 @@ class Solution {
} }
``` ```
### C++ Code:
```cpp
class Solution {
public:
int numberOfSubarrays(vector<int>& nums, int k) {
int len = nums.size();
vector <int> map(len + 1, 0);
map[0] = 1;
int oddnum = 0;
int count = 0;
for (int i = 0; i < len; ++i) {
//如果是奇数则加一偶数加0相当于没加
oddnum += nums[i] & 1;
if (oddnum - k >= 0) {
count += map[oddnum-k];
}
map[oddnum]++;
}
return count;
}
};
```

View File

@@ -46,6 +46,8 @@
**题目代码** **题目代码**
Java Code:
```java ```java
class Solution { class Solution {
public boolean checkSubarraySum(int[] nums, int k) { public boolean checkSubarraySum(int[] nums, int k) {
@@ -71,5 +73,31 @@ class Solution {
} }
``` ```
C++ Code:
```cpp
class Solution {
public:
bool checkSubarraySum(vector<int>& nums, int k) {
map <int, int> m;
//细节2
m.insert({0,-1});
int presum = 0;
for (int i = 0; i < nums.size(); ++i) {
presum += nums[i];
//细节1防止 k 为 0 的情况
int key = k == 0 ? presum : presum % k;
if (m.find(key) != m.end()) {
if (i - m[key] >= 2) {
return true;
}
//因为我们需要保存最小索引,当已经存在时则不用再次存入,不然会更新索引值
continue;
}
m.insert({key, i});
}
return false;
}
};
```

View File

@@ -122,6 +122,8 @@ class Solution {
**题目代码** **题目代码**
Java Code
```java ```java
class Solution { class Solution {
public int subarraySum(int[] nums, int k) { public int subarraySum(int[] nums, int k) {
@@ -150,3 +152,34 @@ class Solution {
} }
``` ```
C++ Code
```cpp
public:
int subarraySum(vector<int>& nums, int k) {
if (nums.size() == 0) {
return 0;
}
map <int, int> m;
//细节,这里需要预存前缀和为 0 的情况,会漏掉前几位就满足的情况
//例如输入[1,1,0]k = 2 如果没有这行代码则会返回0,漏掉了1+1=2和1+1+0=2的情况
//输入:[3,1,1,0] k = 2时则不会漏掉
//因为presum[3] - presum[0]表示前面 3 位的和所以需要m.insert({0,1}),垫下底
m.insert({0, 1});
int count = 0;
int presum = 0;
for (int x : nums) {
presum += x;
//当前前缀和已知,判断是否含有 presum - k的前缀和那么我们就知道某一区间的和为 k 了。
if (m.find(presum - k) != m.end()) {
count += m[presum - k];//获取presum-k前缀和出现次数
}
//更新
if(m.find(presum) != m.end()) m[presum]++;
else m[presum] = 1;
}
return count;
}
};
```

View File

@@ -61,6 +61,8 @@
理解了我们前缀和的概念不知道好像也可以做这个题太简单了哈哈我们可以一下就能把这个题目做出来先遍历一遍求出数组的和然后第二次遍历时直接进行对比左半部分和右半部分是否相同如果相同则返回 true不同则继续遍历 理解了我们前缀和的概念不知道好像也可以做这个题太简单了哈哈我们可以一下就能把这个题目做出来先遍历一遍求出数组的和然后第二次遍历时直接进行对比左半部分和右半部分是否相同如果相同则返回 true不同则继续遍历
Java Code:
```java ```java
class Solution { class Solution {
public int pivotIndex(int[] nums) { public int pivotIndex(int[] nums) {
@@ -82,4 +84,27 @@ class Solution {
} }
``` ```
### C++ Code:
```cpp
class Solution {
public:
int pivotIndex(vector<int>& nums) {
int presum = 0;
//数组的和
for (int x : nums) {
presum += x;
}
int leftsum = 0;
for (int i = 0; i < nums.size(); ++i) {
//发现相同情况
if (leftsum == presum - nums[i] - leftsum) {
return i;
}
leftsum += nums[i];
}
return -1;
}
};
```

View File

@@ -87,6 +87,8 @@ int key = (presum % K + K) % K;
那么这个题目我们可不可以用数组代替 map 当然也是可以的因为此时我们的哈希表存的是余数余数最大也只不过是 K-1所以我们可以用固定长度 K 的数组来模拟哈希表 那么这个题目我们可不可以用数组代替 map 当然也是可以的因为此时我们的哈希表存的是余数余数最大也只不过是 K-1所以我们可以用固定长度 K 的数组来模拟哈希表
Java Code:
```java ```java
class Solution { class Solution {
public int subarraysDivByK(int[] A, int K) { public int subarraysDivByK(int[] A, int K) {
@@ -107,3 +109,26 @@ class Solution {
} }
``` ```
C++ Code:
```cpp
class Solution {
public:
int subarraysDivByK(vector<int>& A, int K) {
vector <int> map (K, 0);
int len = A.size();
int count = 0;
int presum = 0;
map[0] = 1;
for (int i = 0; i < len; ++i) {
presum += A[i];
//求key
int key = (presum % K + K) % K;
//count添加次数并将当前的map[key]++;
count += (map[key]++);
}
return count;
}
};
```

View File

@@ -18,6 +18,7 @@
下面我们来看一下题目代码也是很容易理解 下面我们来看一下题目代码也是很容易理解
Java Code:
```java ```java
class MyStack { class MyStack {
//初始化队列 //初始化队列
@@ -55,3 +56,34 @@ class MyStack {
``` ```
JS Code:
```javascript
var MyStack = function() {
this.queue = [];
};
MyStack.prototype.push = function(x) {
this.queue.push(x);
if (this.queue.length > 1) {
let i = this.queue.length - 1;
while (i) {
this.queue.push(this.queue.shift());
i--;
}
}
};
MyStack.prototype.pop = function() {
return this.queue.shift();
};
MyStack.prototype.top = function() {
return this.empty() ? null : this.queue[0];
};
MyStack.prototype.empty = function() {
return !this.queue.length;
};
```

View File

@@ -58,6 +58,7 @@ class CQueue {
大家可以点击该链接[剑指 Offer 09. 用两个栈实现队列](https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/)去实现一下,下面我们看代码。 大家可以点击该链接[剑指 Offer 09. 用两个栈实现队列](https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/)去实现一下,下面我们看代码。
Java Code:
```java ```java
class CQueue { class CQueue {
//初始化两个栈 //初始化两个栈
@@ -89,3 +90,24 @@ class CQueue {
} }
``` ```
JS Code:
```javascript
var CQueue = function() {
this.stack1 = [];
this.stack2 = [];
};
CQueue.prototype.appendTail = function(value) {
this.stack1.push(value);
};
CQueue.prototype.deleteHead = function() {
if (!this.stack2.length) {
while(this.stack1.length) {
this.stack2.push(this.stack1.pop());
}
}
return this.stack2.pop() || -1;
};
```

View File

@@ -38,6 +38,7 @@
**题目代码** **题目代码**
Java Code:
```java ```java
public class Solution { public class Solution {
public boolean hasCycle(ListNode head) { public boolean hasCycle(ListNode head) {
@@ -56,3 +57,18 @@ public class Solution {
} }
``` ```
JS Code:
```javascript
var hasCycle = function(head) {
let fast = head;
let slow = head;
while (fast && fast.next) {
fast = fast.next.next;
slow = slow.next;
if (fast === slow) {
return true;
}
}
return false;
};
```

View File

@@ -33,6 +33,7 @@ low = temp 即可。然后重复执行上诉操作直至最后,这样则完成
我会对每个关键点进行注释大家可以参考动图理解 我会对每个关键点进行注释大家可以参考动图理解
Java Code:
```java ```java
class Solution { class Solution {
public ListNode reverseList(ListNode head) { public ListNode reverseList(ListNode head) {
@@ -62,9 +63,27 @@ class Solution {
} }
``` ```
JS Code:
```javascript
var reverseList = function(head) {
if(!head || !head.next) {
return head;
}
let low = null;
let pro = head;
while (pro) {
let temp = pro;
pro = pro.next;
temp.next = low;
low = temp;
}
return low;
};
```
上面的迭代写法是不是搞懂啦现在还有一种递归写法不是特别容易理解刚开始刷题的同学可以只看迭代解法 上面的迭代写法是不是搞懂啦现在还有一种递归写法不是特别容易理解刚开始刷题的同学可以只看迭代解法
Java Code:
```java ```java
class Solution { class Solution {
public ListNode reverseList(ListNode head) { public ListNode reverseList(ListNode head) {
@@ -86,3 +105,16 @@ class Solution {
``` ```
JS Code:
```javascript
var reverseList = function(head) {
if (!head || !head.next) {
return head;
}
let pro = reverseList(head.next);
head.next.next = head;
head.next = null;
return pro;
};
```