This commit is contained in:
chefyuan 2021-04-27 19:09:13 +08:00
commit 69877f9029
9 changed files with 272 additions and 2 deletions

View File

@ -43,6 +43,8 @@
我们来解析一下哈希表key 代表的是含有 1 个奇数的前缀区间value 代表这种子区间的个数含有两个也就是nums[0],nums[0,1].后面含义相同那我们下面直接看代码吧一下就能读懂
Java Code:
```java
class Solution {
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下面我们直接看代码吧
Java Code:
```java
class Solution {
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
class Solution {
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
class Solution {
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不同则继续遍历
Java Code:
```java
class Solution {
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 的数组来模拟哈希表
Java Code:
```java
class Solution {
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
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/)去实现一下下面我们看代码
Java Code:
```java
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
public class Solution {
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
class Solution {
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
class Solution {
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;
};
```