Skip to content

Commit

Permalink
update cheatsheet
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Oct 31, 2024
1 parent 293c797 commit f8e1500
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
24 changes: 22 additions & 2 deletions doc/cheatsheet/2_pointers.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
### 0-2-0) Remove Duplicates from Sorted Array
```java
// java
// LC 26
// LC 26 (LC 83)
// https://labuladong.online/algo/essential-technique/array-two-pointers-summary/#%E5%8E%9F%E5%9C%B0%E4%BF%AE%E6%94%B9
class Solution {
public int removeDuplicates(int[] nums) {
Expand All @@ -68,8 +68,28 @@ class Solution {
}
}
```
### 0-2-1) Remove Element
```java
// java
// LC 27
// https://labuladong.online/algo/essential-technique/array-two-pointers-summary/#%E5%8E%9F%E5%9C%B0%E4%BF%AE%E6%94%B9
class Solution {
public int removeElement(int[] nums, int val) {
int fast = 0, slow = 0;
while (fast < nums.length) {
if (nums[fast] != val) {
nums[slow] = nums[fast];
slow++;
}
fast++;
}
return slow;
}
}
```


#### 0-2-1) for loop + "expand `left`, `right` from center"
#### 0-2-3) for loop + "expand `left`, `right` from center"
```python
# LC 005 Longest Palindromic Substring
# LC 647 Palindromic Substrings
Expand Down
28 changes: 27 additions & 1 deletion doc/cheatsheet/2_pointers_linkedlist.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,30 @@ while (fast != null){
return slow;
```

## 2) LC Example
## 2) LC Example


### 2-1) Remove Duplicates from Sorted List
```java
// LC 83 (LC 26)
// https://labuladong.online/algo/essential-technique/array-two-pointers-summary/#%E5%8E%9F%E5%9C%B0%E4%BF%AE%E6%94%B9
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head == null) return null;
ListNode slow = head, fast = head;
while (fast != null) {
if (fast.val != slow.val) {
// nums[slow] = nums[fast];
slow.next = fast;
// slow++;
slow = slow.next;
}
// fast++
fast = fast.next;
}
// 断开与后面重复元素的连接
slow.next = null;
return head;
}
}
```

0 comments on commit f8e1500

Please sign in to comment.