Skip to content

Commit 3dd9395

Browse files
committed
update 代码随想录
1 parent ab6e775 commit 3dd9395

File tree

289 files changed

+34308
-4300
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

289 files changed

+34308
-4300
lines changed

面试和算法/代码随想录/README.md

Lines changed: 73 additions & 96 deletions
Large diffs are not rendered by default.
-129 KB
Loading

面试和算法/代码随想录/problems/0001.两数之和.md

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<p align="center">
2-
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
2+
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
33
<img src="../pics/训练营.png" width="1000"/>
44
</a>
5-
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
5+
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
66

77

88
# 1. 两数之和
@@ -133,6 +133,7 @@ public:
133133
### Java:
134134
135135
```java
136+
//使用哈希表
136137
public int[] twoSum(int[] nums, int target) {
137138
int[] res = new int[2];
138139
if(nums == null || nums.length == 0){
@@ -152,6 +153,61 @@ public int[] twoSum(int[] nums, int target) {
152153
}
153154
```
154155

156+
```java
157+
//使用哈希表方法2
158+
public int[] twoSum(int[] nums, int target) {
159+
Map<Integer, Integer> indexMap = new HashMap<>();
160+
161+
for(int i = 0; i < nums.length; i++){
162+
int balance = target - nums[i]; // 记录当前的目标值的余数
163+
if(indexMap.containsKey(balance)){ // 查找当前的map中是否有满足要求的值
164+
return new int []{i, indexMap.get(balance)}; // 如果有,返回目标值
165+
} else{
166+
indexMap.put(nums[i], i); // 如果没有,把访问过的元素和下标加入map中
167+
}
168+
}
169+
return null;
170+
}
171+
```
172+
173+
```java
174+
//使用双指针
175+
public int[] twoSum(int[] nums, int target) {
176+
int m=0,n=0,k,board=0;
177+
int[] res=new int[2];
178+
int[] tmp1=new int[nums.length];
179+
//备份原本下标的nums数组
180+
System.arraycopy(nums,0,tmp1,0,nums.length);
181+
//将nums排序
182+
Arrays.sort(nums);
183+
//双指针
184+
for(int i=0,j=nums.length-1;i<j;){
185+
if(nums[i]+nums[j]<target)
186+
i++;
187+
else if(nums[i]+nums[j]>target)
188+
j--;
189+
else if(nums[i]+nums[j]==target){
190+
m=i;
191+
n=j;
192+
break;
193+
}
194+
}
195+
//找到nums[m]在tmp1数组中的下标
196+
for(k=0;k<nums.length;k++){
197+
if(tmp1[k]==nums[m]){
198+
res[0]=k;
199+
break;
200+
}
201+
}
202+
//找到nums[n]在tmp1数组中的下标
203+
for(int i=0;i<nums.length;i++){
204+
if(tmp1[i]==nums[n]&&i!=k)
205+
res[1]=i;
206+
}
207+
return res;
208+
}
209+
```
210+
155211
### Python:
156212
(版本一) 使用字典
157213

@@ -285,7 +341,7 @@ impl Solution {
285341
}
286342
```
287343

288-
### Javascript:
344+
### JavaScript:
289345

290346
```javascript
291347
var twoSum = function (nums, target) {
@@ -311,6 +367,7 @@ function twoSum(nums: number[], target: number): number[] {
311367
index = helperMap.get(target - nums[i]);
312368
if (index !== undefined) {
313369
resArr = [i, index];
370+
break;
314371
}
315372
helperMap.set(nums[i], i);
316373
}
@@ -499,8 +556,8 @@ int* twoSum(int* nums, int numsSize, int target, int* returnSize){
499556
return NULL;
500557
}
501558
```
559+
502560
<p align="center">
503561
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
504562
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
505563
</a>
506-

面试和算法/代码随想录/problems/0005.最长回文子串.md

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<p align="center">
2-
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
2+
<a href="https://www.programmercarl.com/xunlian/xunlianying.html" target="_blank">
33
<img src="../pics/训练营.png" width="1000"/>
44
</a>
5-
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
5+
<p align="center"><strong><a href="./qita/join.md">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们受益!</strong></p>
66

77

88

@@ -256,7 +256,60 @@ public:
256256
* 时间复杂度:O(n^2)
257257
* 空间复杂度:O(1)
258258

259+
### Manacher 算法
259260

261+
Manacher 算法的关键在于高效利用回文的对称性,通过插入分隔符和维护中心、边界等信息,在线性时间内找到最长回文子串。这种方法避免了重复计算,是处理回文问题的最优解。
262+
263+
```c++
264+
//Manacher 算法
265+
class Solution {
266+
public:
267+
string longestPalindrome(string s) {
268+
// 预处理字符串,在每个字符之间插入 '#'
269+
string t = "#";
270+
for (char c : s) {
271+
t += c; // 添加字符
272+
t += '#';// 添加分隔符
273+
}
274+
int n = t.size();// 新字符串的长度
275+
vector<int> p(n, 0);// p[i] 表示以 t[i] 为中心的回文半径
276+
int center = 0, right = 0;// 当前回文的中心和右边界
277+
278+
279+
// 遍历预处理后的字符串
280+
for (int i = 0; i < n; i++) {
281+
// 如果当前索引在右边界内,利用对称性初始化 p[i]
282+
if (i < right) {
283+
p[i] = min(right - i, p[2 * center - i]);
284+
}
285+
// 尝试扩展回文
286+
while (i - p[i] - 1 >= 0 && i + p[i] + 1 < n && t[i - p[i] - 1] == t[i + p[i] + 1]) {
287+
p[i]++;// 增加回文半径
288+
}
289+
// 如果当前回文扩展超出右边界,更新中心和右边界
290+
if (i + p[i] > right) {
291+
center = i;// 更新中心
292+
right = i + p[i];// 更新右边界
293+
}
294+
}
295+
// 找到最大回文半径和对应的中心
296+
int maxLen = 0, centerIndex = 0;
297+
for (int i = 0; i < n; i++) {
298+
if (p[i] > maxLen) {
299+
maxLen = p[i];// 更新最大回文长度
300+
centerIndex = i;// 更新中心索引
301+
}
302+
}
303+
// 计算原字符串中回文子串的起始位置并返回
304+
return s.substr((centerIndex - maxLen) / 2, maxLen);
305+
}
306+
};
307+
```
308+
309+
310+
311+
* 时间复杂度:O(n)
312+
* 空间复杂度:O(n)
260313

261314
## 其他语言版本
262315

@@ -618,7 +671,7 @@ char * longestPalindrome(char * s){
618671
### C#:
619672

620673
動態規則:
621-
```c#
674+
```csharp
622675
public class Solution {
623676

624677
public string LongestPalindrome(string s) {
@@ -648,7 +701,7 @@ public class Solution {
648701
```
649702

650703
雙指針:
651-
```C#
704+
```csharp
652705
public class Solution {
653706
int maxlenth = 0;
654707
int left = 0;
@@ -677,6 +730,7 @@ public class Solution {
677730
}
678731
```
679732

733+
680734
<p align="center">
681735
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
682736
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>

0 commit comments

Comments
 (0)