Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
youngyangyang04 committed Jul 29, 2021
2 parents 57eba78 + ff3875d commit 5d7e028
Show file tree
Hide file tree
Showing 10 changed files with 407 additions and 6 deletions.
86 changes: 86 additions & 0 deletions problems/0028.实现strStr.md
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,93 @@ func strStr(haystack string, needle string) int {
}
```

JavaScript版本

> 前缀表统一减一
```javascript
/**
* @param {string} haystack
* @param {string} needle
* @return {number}
*/
var strStr = function (haystack, needle) {
if (needle.length === 0)
return 0;

const getNext = (needle) => {
let next = [];
let j = -1;
next.push(j);

for (let i = 1; i < needle.length; ++i) {
while (j >= 0 && needle[i] !== needle[j + 1])
j = next[j];
if (needle[i] === needle[j + 1])
j++;
next.push(j);
}

return next;
}

let next = getNext(needle);
let j = -1;
for (let i = 0; i < haystack.length; ++i) {
while (j >= 0 && haystack[i] !== needle[j + 1])
j = next[j];
if (haystack[i] === needle[j + 1])
j++;
if (j === needle.length - 1)
return (i - needle.length + 1);
}

return -1;
};
```

> 前缀表统一不减一
```javascript
/**
* @param {string} haystack
* @param {string} needle
* @return {number}
*/
var strStr = function (haystack, needle) {
if (needle.length === 0)
return 0;

const getNext = (needle) => {
let next = [];
let j = 0;
next.push(j);

for (let i = 1; i < needle.length; ++i) {
while (j > 0 && needle[i] !== needle[j])
j = next[j - 1];
if (needle[i] === needle[j])
j++;
next.push(j);
}

return next;
}

let next = getNext(needle);
let j = 0;
for (let i = 0; i < haystack.length; ++i) {
while (j > 0 && haystack[i] !== needle[j])
j = next[j - 1];
if (haystack[i] === needle[j])
j++;
if (j === needle.length)
return (i - needle.length + 1);
}

return -1;
};
```



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
如果数组中不存在目标值 target,返回 [-1, -1]

进阶:你可以设计并实现时间复杂度为 O(log n) 的算法解决此问题吗?
 


示例 1:
* 输入:nums = [5,7,7,8,8,10], target = 8
Expand Down Expand Up @@ -173,8 +173,105 @@ private:
## Java
```java
class Solution {
int[] searchRange(int[] nums, int target) {
int leftBorder = getLeftBorder(nums, target);
int rightBorder = getRightBorder(nums, target);
// 情况一
if (leftBorder == -2 || rightBorder == -2) return new int[]{-1, -1};
// 情况三
if (rightBorder - leftBorder > 1) return new int[]{leftBorder + 1, rightBorder - 1};
// 情况二
return new int[]{-1, -1};
}
int getRightBorder(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
int rightBorder = -2; // 记录一下rightBorder没有被赋值的情况
while (left <= right) {
int middle = left + ((right - left) / 2);
if (nums[middle] > target) {
right = middle - 1;
} else { // 寻找右边界,nums[middle] == target的时候更新left
left = middle + 1;
rightBorder = left;
}
}
return rightBorder;
}
int getLeftBorder(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
int leftBorder = -2; // 记录一下leftBorder没有被赋值的情况
while (left <= right) {
int middle = left + ((right - left) / 2);
if (nums[middle] >= target) { // 寻找左边界,nums[middle] == target的时候更新right
right = middle - 1;
leftBorder = right;
} else {
left = middle + 1;
}
}
return leftBorder;
}
}
```

```java
// 解法2
// 1、首先,在 nums 数组中二分查找 target;
// 2、如果二分查找失败,则 binarySearch 返回 -1,表明 nums 中没有 target。此时,searchRange 直接返回 {-1, -1};
// 3、如果二分查找失败,则 binarySearch 返回 nums 中 为 target 的一个下标。然后,通过左右滑动指针,来找到符合题意的区间

class Solution {
public int[] searchRange(int[] nums, int target) {
int index = binarySearch(nums, target); // 二分查找

if (index == -1) { // nums 中不存在 target,直接返回 {-1, -1}
return new int[] {-1, -1}; // 匿名数组
}
// nums 中存在 targe,则左右滑动指针,来找到符合题意的区间
int left = index;
int right = index;
// 向左滑动,找左边界
while (left - 1 >= 0 && nums[left - 1] == nums[index]) { // 防止数组越界。逻辑短路,两个条件顺序不能换
left--;
}
// 向左滑动,找右边界
while (right + 1 < nums.length && nums[right + 1] == nums[index]) { // 防止数组越界。
right++;
}
return new int[] {left, right};
}

/**
* 二分查找
* @param nums
* @param target
*/
public int binarySearch(int[] nums, int target) {
int left = 0;
int right = nums.length - 1; // 不变量:左闭右闭区间

while (left <= right) { // 不变量:左闭右闭区间
int mid = left + (right - left) / 2;
if (nums[mid] == target) {
return mid;
} else if (nums[mid] < target) {
left = mid + 1;
} else {
right = mid - 1; // 不变量:左闭右闭区间
}
}
return -1; // 不存在
}
}
```



## Python

```python
Expand All @@ -196,4 +293,3 @@ private:
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>


17 changes: 16 additions & 1 deletion problems/0141.环形链表.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。

如果链表中存在环,则返回 true 。 否则,返回 false 。
 

![](https://code-thinking-1253855093.file.myqcloud.com/pics/20210727173600.png)

# 思路
Expand Down Expand Up @@ -74,6 +74,21 @@ public:
## Java
```java
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode fast = head;
ListNode slow = head;
// 空链表、单节点链表一定不会有环
while (fast != null && fast.next != null) {
fast = fast.next.next; // 快指针,一次移动两步
slow = slow.next; // 慢指针,一次移动一步
if (fast == slow) { // 快慢指针相遇,表明有环
return true;
}
}
return false; // 正常走到链表末尾,表明没有环
}
}
```

## Python
Expand Down
31 changes: 31 additions & 0 deletions problems/0203.移除链表元素.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,39 @@ public:
## 其他语言版本
C:
```c
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* removeElements(struct ListNode* head, int val){
typedef struct ListNode ListNode;
ListNode *shead;
shead = (ListNode *)malloc(sizeof(ListNode));
shead->next = head;
ListNode *cur = shead;
while(cur->next != NULL){
if (cur->next->val == val){
ListNode *tmp = cur->next;
cur->next = cur->next->next;
free(tmp);
}
else{
cur = cur->next;
}
}
head = shead->next;
free(shead);
return head;
}
```

Java:
```java
/**
Expand Down
15 changes: 15 additions & 0 deletions problems/0283.移动零.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ public:
Java:
```java
public void moveZeroes(int[] nums) {
int slow = 0;
for (int fast = 0; fast < nums.length; fast++) {
if (nums[fast] != 0) {
nums[slow++] = nums[fast];
}
}
// 后面的元素全变成 0
for (int j = slow; j < nums.length; j++) {
nums[j] = 0;
}
}
```

Python:

```python
Expand Down
23 changes: 23 additions & 0 deletions problems/0435.无重叠区间.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,29 @@ class Solution {
}
```

Java:
按左边排序,不管右边顺序。相交的时候取最小的右边。
```java
class Solution {
public int eraseOverlapIntervals(int[][] intervals) {

Arrays.sort(intervals,(a,b)->{
return Integer.compare(a[0],b[0]);
});
int remove = 0;
int pre = intervals[0][1];
for(int i=1;i<intervals.length;i++){
if(pre>intervals[i][0]) {
remove++;
pre = Math.min(pre,intervals[i][1]);
}
else pre = intervals[i][1];
}
return remove;
}
}
```

Python:
```python
class Solution:
Expand Down
Loading

0 comments on commit 5d7e028

Please sign in to comment.