mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -332,7 +332,43 @@ var threeSum = function(nums) {
|
||||
return res;
|
||||
};
|
||||
```
|
||||
TypeScript:
|
||||
|
||||
```typescript
|
||||
function threeSum(nums: number[]): number[][] {
|
||||
nums.sort((a, b) => a - b);
|
||||
let length = nums.length;
|
||||
let left: number = 0,
|
||||
right: number = length - 1;
|
||||
let resArr: number[][] = [];
|
||||
for (let i = 0; i < length; i++) {
|
||||
if (i > 0 && nums[i] === nums[i - 1]) {
|
||||
continue;
|
||||
}
|
||||
left = i + 1;
|
||||
right = length - 1;
|
||||
while (left < right) {
|
||||
let total: number = nums[i] + nums[left] + nums[right];
|
||||
if (total === 0) {
|
||||
resArr.push([nums[i], nums[left], nums[right]]);
|
||||
left++;
|
||||
right--;
|
||||
while (nums[right] === nums[right + 1]) {
|
||||
right--;
|
||||
}
|
||||
while (nums[left] === nums[left - 1]) {
|
||||
left++;
|
||||
}
|
||||
} else if (total < 0) {
|
||||
left++;
|
||||
} else {
|
||||
right--;
|
||||
}
|
||||
}
|
||||
}
|
||||
return resArr;
|
||||
};
|
||||
```
|
||||
|
||||
ruby:
|
||||
```ruby
|
||||
|
@ -311,7 +311,49 @@ var fourSum = function(nums, target) {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
|
||||
```typescript
|
||||
function fourSum(nums: number[], target: number): number[][] {
|
||||
nums.sort((a, b) => a - b);
|
||||
let first: number = 0,
|
||||
second: number,
|
||||
third: number,
|
||||
fourth: number;
|
||||
let length: number = nums.length;
|
||||
let resArr: number[][] = [];
|
||||
for (; first < length; first++) {
|
||||
if (first > 0 && nums[first] === nums[first - 1]) {
|
||||
continue;
|
||||
}
|
||||
for (second = first + 1; second < length; second++) {
|
||||
if ((second - first) > 1 && nums[second] === nums[second - 1]) {
|
||||
continue;
|
||||
}
|
||||
third = second + 1;
|
||||
fourth = length - 1;
|
||||
while (third < fourth) {
|
||||
let total: number = nums[first] + nums[second] + nums[third] + nums[fourth];
|
||||
if (total === target) {
|
||||
resArr.push([nums[first], nums[second], nums[third], nums[fourth]]);
|
||||
third++;
|
||||
fourth--;
|
||||
while (nums[third] === nums[third - 1]) third++;
|
||||
while (nums[fourth] === nums[fourth + 1]) fourth--;
|
||||
} else if (total < target) {
|
||||
third++;
|
||||
} else {
|
||||
fourth--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return resArr;
|
||||
};
|
||||
```
|
||||
|
||||
PHP:
|
||||
|
||||
```php
|
||||
class Solution {
|
||||
/**
|
||||
|
@ -42,7 +42,7 @@
|
||||
|
||||

|
||||
|
||||
很明显暴力解法的时间复杂度是O(n^2),这道题目暴力解法在leetcode上是可以过的。
|
||||
很明显暴力解法的时间复杂度是$O(n^2)$,这道题目暴力解法在leetcode上是可以过的。
|
||||
|
||||
代码如下:
|
||||
|
||||
|
@ -259,7 +259,7 @@ void getNext(int* next, const string& s)
|
||||
|
||||
然后还要对next数组进行初始化赋值,如下:
|
||||
|
||||
```
|
||||
```cpp
|
||||
int j = -1;
|
||||
next[0] = j;
|
||||
```
|
||||
@ -278,7 +278,7 @@ next[i] 表示 i(包括i)之前最长相等的前后缀长度(其实就是
|
||||
|
||||
所以遍历模式串s的循环下标i 要从 1开始,代码如下:
|
||||
|
||||
```
|
||||
```cpp
|
||||
for (int i = 1; i < s.size(); i++) {
|
||||
```
|
||||
|
||||
@ -292,7 +292,7 @@ next[j]就是记录着j(包括j)之前的子串的相同前后缀的长度
|
||||
|
||||
所以,处理前后缀不相同的情况代码如下:
|
||||
|
||||
```
|
||||
```cpp
|
||||
while (j >= 0 && s[i] != s[j + 1]) { // 前后缀不相同了
|
||||
j = next[j]; // 向前回退
|
||||
}
|
||||
@ -346,7 +346,7 @@ void getNext(int* next, const string& s){
|
||||
|
||||
i就从0开始,遍历文本串,代码如下:
|
||||
|
||||
```
|
||||
```cpp
|
||||
for (int i = 0; i < s.size(); i++)
|
||||
```
|
||||
|
||||
@ -356,7 +356,7 @@ for (int i = 0; i < s.size(); i++)
|
||||
|
||||
代码如下:
|
||||
|
||||
```
|
||||
```cpp
|
||||
while(j >= 0 && s[i] != t[j + 1]) {
|
||||
j = next[j];
|
||||
}
|
||||
@ -364,7 +364,7 @@ while(j >= 0 && s[i] != t[j + 1]) {
|
||||
|
||||
如果 s[i] 与 t[j + 1] 相同,那么i 和 j 同时向后移动, 代码如下:
|
||||
|
||||
```
|
||||
```cpp
|
||||
if (s[i] == t[j + 1]) {
|
||||
j++; // i的增加在for循环里
|
||||
}
|
||||
@ -376,7 +376,7 @@ if (s[i] == t[j + 1]) {
|
||||
|
||||
代码如下:
|
||||
|
||||
```
|
||||
```cpp
|
||||
if (j == (t.size() - 1) ) {
|
||||
return (i - t.size() + 1);
|
||||
}
|
||||
|
@ -46,7 +46,7 @@
|
||||
|
||||
模拟的队列执行语句如下:
|
||||
|
||||
```
|
||||
```cpp
|
||||
queue.push(1);
|
||||
queue.push(2);
|
||||
queue.pop(); // 注意弹出的操作
|
||||
|
@ -21,7 +21,7 @@ empty() -- 返回队列是否为空。
|
||||
|
||||
示例:
|
||||
|
||||
```
|
||||
```cpp
|
||||
MyQueue queue = new MyQueue();
|
||||
queue.push(1);
|
||||
queue.push(2);
|
||||
|
@ -264,6 +264,27 @@ var canConstruct = function(ransomNote, magazine) {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
|
||||
```typescript
|
||||
function canConstruct(ransomNote: string, magazine: string): boolean {
|
||||
let helperArr: number[] = new Array(26).fill(0);
|
||||
let base: number = 'a'.charCodeAt(0);
|
||||
let index: number;
|
||||
for (let i = 0, length = magazine.length; i < length; i++) {
|
||||
helperArr[magazine[i].charCodeAt(0) - base]++;
|
||||
}
|
||||
for (let i = 0, length = ransomNote.length; i < length; i++) {
|
||||
index = ransomNote[i].charCodeAt(0) - base;
|
||||
helperArr[index]--;
|
||||
if (helperArr[index] < 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
PHP:
|
||||
```php
|
||||
|
@ -67,7 +67,7 @@ deque是一个双向队列,只要封住一段,只开通另一端就可以实
|
||||
|
||||
我们也可以指定vector为栈的底层实现,初始化语句如下:
|
||||
|
||||
```
|
||||
```cpp
|
||||
std::stack<int, std::vector<int> > third; // 使用vector为底层容器的栈
|
||||
```
|
||||
|
||||
@ -77,7 +77,7 @@ std::stack<int, std::vector<int> > third; // 使用vector为底层容器的栈
|
||||
|
||||
也可以指定list 为起底层实现,初始化queue的语句如下:
|
||||
|
||||
```
|
||||
```cpp
|
||||
std::queue<int, std::list<int>> third; // 定义以list为底层容器的队列
|
||||
```
|
||||
|
||||
|
41
problems/面试题 02.07. 解法更新.md
Normal file
41
problems/面试题 02.07. 解法更新.md
Normal file
@ -0,0 +1,41 @@
|
||||
# 双指针,不计算链表长度
|
||||
设置指向headA和headB的指针pa、pb,分别遍历两个链表,每次循环同时更新pa和pb。
|
||||
* 当链表A遍历完之后,即pa为空时,将pa指向headB;
|
||||
* 当链表B遍历完之后,即pa为空时,将pb指向headA;
|
||||
* 当pa与pb相等时,即指向同一个节点,该节点即为相交起始节点。
|
||||
* 若链表不相交,则pa、pb同时为空时退出循环,即如果链表不相交,pa与pb在遍历过全部节点后同时指向结尾空节点,此时退出循环,返回空。
|
||||
# 证明思路
|
||||
设链表A不相交部分长度为a,链表B不相交部分长度为b,两个链表相交部分长度为c。<br>
|
||||
在pa指向链表A时,即pa为空之前,pa经过链表A不相交部分和相交部分,走过的长度为a+c;<br>
|
||||
pa指向链表B后,在移动相交节点之前经过链表B不相交部分,走过的长度为b,总合为a+c+b。<br>
|
||||
同理,pb走过长度的总合为b+c+a。二者相等,即pa与pb可同时到达相交起始节点。 <br>
|
||||
该方法可避免计算具体链表长度。
|
||||
```cpp
|
||||
class Solution {
|
||||
public:
|
||||
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
|
||||
//链表为空时,返回空指针
|
||||
if(headA == nullptr || headB == nullptr) return nullptr;
|
||||
ListNode* pa = headA;
|
||||
ListNode* pb = headB;
|
||||
//pa与pb在遍历过全部节点后,同时指向结尾空节点时退出循环
|
||||
while(pa != nullptr || pb != nullptr){
|
||||
//pa为空时,将pa指向headB
|
||||
if(pa == nullptr){
|
||||
pa = headB;
|
||||
}
|
||||
//pa为空时,将pb指向headA
|
||||
if(pb == nullptr){
|
||||
pb = headA;
|
||||
}
|
||||
//pa与pb相等时,返回相交起始节点
|
||||
if(pa == pb){
|
||||
return pa;
|
||||
}
|
||||
pa = pa->next;
|
||||
pb = pb->next;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
```
|
Reference in New Issue
Block a user