Skip to content

Commit 2f23aa4

Browse files
author
herongwei
committed
leetcode new ac 560 and 724
1 parent c5486ba commit 2f23aa4

7 files changed

+157
-32
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
**思路:前缀和 + 哈希表优化**
2+
3+
这个题目和两数之和原理是一致的,只不过是将所有的前缀和该前缀和出现的次数存到了 map 里。
4+
5+
**复杂度分析**
6+
7+
时间复杂度:O(n),其中 n 为数组的长度。我们遍历数组的时间复杂度为 O(n),中间利用哈希表查询删除的复杂度均为 O(1),因此总时间复杂度为 O(n)。
8+
9+
空间复杂度:O(n),其中 n 为数组的长度。哈希表在最坏情况下可能有 n 个不同的键值,因此需要 O(n) 的空间复杂度。
10+
11+
```c++
12+
class Solution {
13+
public:
14+
int subarraySum(std::vector<int>& nums, int k) {
15+
int sum = 0, res = 0;
16+
std::unordered_map<int,int> mp{{0,1}};
17+
for (int &e : nums) {
18+
sum += e;
19+
res += mp[sum - k];
20+
mp[sum]++;
21+
}
22+
return res;
23+
}
24+
};
25+
```
26+
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
**思路**
2+
3+
先遍历一遍求出数组的和,然后第二次遍历时,直接进行对比左半部分和右半部分是否相同,如果相同则返回 true,不同则继续遍历。
4+
5+
**复杂度分析**
6+
7+
时间复杂度:O(n),其中 n 为数组的长度。我们遍历数组的时间复杂度为 O(n)
8+
9+
空间复杂度:O(1)
10+
11+
```c++
12+
class Solution {
13+
public:
14+
int pivotIndex(vector<int>& nums) {
15+
int len = nums.size();
16+
int sum = 0;
17+
for (auto &e : nums) {
18+
sum += e;
19+
}
20+
int leftSum = 0;
21+
for(int i=0; i<len; ++i) {
22+
if (leftSum == (sum - leftSum - nums[i])) {
23+
return i;
24+
}
25+
leftSum += nums[i];
26+
}
27+
return -1;
28+
}
29+
};
30+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
**思路**
2+
3+
前缀和 + 哈希表优化
4+
5+
这个题目和两数之和原理是一致的,只不过是将所有的前缀和该前缀和出现的次数存到了 map 里。
6+
7+
**复杂度分析**
8+
9+
时间复杂度:O(n),其中 n 为数组的长度。我们遍历数组的时间复杂度为 O(n),中间利用哈希表查询删除的复杂度均为 O(1),因此总时间复杂度为 O(n)。
10+
11+
空间复杂度:O(n),其中 n 为数组的长度。哈希表在最坏情况下可能有 n 个不同的键值,因此需要 O(n) 的空间复杂度。
12+
13+
```go
14+
func subarraySum(nums []int, k int) int {
15+
sum, res := 0, 0
16+
mp := map[int]int{}
17+
mp[0] = 1;
18+
for i := 0; i < len(nums); i++ {
19+
sum += nums[i]
20+
if _, ok := mp[sum - k]; ok {
21+
res += mp[sum - k]
22+
}
23+
mp[sum]++
24+
}
25+
return res
26+
}
27+
```
28+
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
**思路**
2+
3+
先遍历一遍求出数组的和,然后第二次遍历时,直接进行对比左半部分和右半部分是否相同,如果相同则返回 true,不同则继续遍历。
4+
5+
**复杂度分析**
6+
7+
时间复杂度:O(n),其中 n 为数组的长度。我们遍历数组的时间复杂度为 O(n)
8+
9+
空间复杂度:O(1)
10+
11+
```go
12+
func pivotIndex(nums []int) int{
13+
sum := 0
14+
for i, _ := range nums {
15+
sum += nums[i]
16+
}
17+
leftSum := 0
18+
for i:=0; i< len(nums); i++ {
19+
if leftSum == (sum - leftSum - nums[i]) {
20+
return i
21+
}
22+
leftSum += nums[i]
23+
}
24+
return -1
25+
}
26+
```

huffman renamed to huffman.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,38 @@ int main()
7575
}
7676
return 0;
7777
}
78+
79+
80+
81+
/* ***********************************************
82+
Author :herongwei
83+
Created Time :Fri 07 Jul 2017 05:37:18 PM CST
84+
File Name :readme.txt
85+
************************************************ */
86+
87+
#include <stdio.h>
88+
#include <string.h>
89+
#include <iostream>
90+
#include <algorithm>
91+
#include <vector>
92+
#include <queue>
93+
#include <set>
94+
#include <map>
95+
#include <string>
96+
#include <math.h>
97+
#include <stdlib.h>
98+
#include <time.h>
99+
using namespace std;
100+
typedef long long LL;
101+
const int maxn = 1e5+233;
102+
const int MOD = 1e9+7;
103+
const double eps= 1e-6;
104+
const double pi = acos(-1.0);
105+
106+
int main()
107+
{
108+
//freopen("in.txt","r",stdin);
109+
//freopen("out.txt","w",stdout);
110+
return 0;
111+
112+
}

readme.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
### LeetCode Algorithm
2+
3+
Algorithms are the soul of programs.
4+
5+
| # | Title | Solution | Difficulty |
6+
| ---- | ------------------------------------------------------------ | ------------------------------------------------------------ | ---------- |
7+
| 1605 | [Find Valid Matrix Given Row and Column Sums](https://leetcode.com/problems/find-valid-matrix-given-row-and-column-sums/) | [C++](https://github.com/haoel/leetcode/blob/master/algorithms/cpp/FindValidMatrixGivenRowAndColumnSums/FindValidMatrixGivenRowAndColumnSums.cpp) | Medium |
8+
| 1573 | [Number of Ways to Split a String](https://leetcode.com/problems/number-of-ways-to-split-a-string/) | [C++](https://github.com/haoel/leetcode/blob/master/algorithms/cpp/NumberOfWaysToSplitString/NumberOfWaysToSplitString.cpp) | Medium |
9+
| 1556 | [Thousand Separator](https://leetcode.com/problems/thousand-separator/) | [C++](https://github.com/haoel/leetcode/blob/master/algorithms/cpp/thousandSeparator/ThousandSeparator.cpp) | Easy |
10+
| 560 | [subarray-sum-equals-k](https://leetcode-cn.com/problems/subarray-sum-equals-k/) | [C++]() | Medium |
11+
| 724 | [Find-pivot-index](https://leetcode-cn.com/problems/find-pivot-index/) | [C++]() | Easy |
12+

readme.txt

-32
This file was deleted.

0 commit comments

Comments
 (0)