Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 201 additions & 0 deletions articles/design-browser-history.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,53 @@ impl BrowserHistory {
}
```

```typescript
class BrowserHistory {
private backHistory: string[];
private frontHistory: string[];

/**
* @constructor
* @param {string} homepage
*/
constructor(homepage: string) {
this.backHistory = [homepage];
this.frontHistory = [];
}

/**
* @param {string} url
* @return {void}
*/
visit(url: string): void {
this.backHistory.push(url);
this.frontHistory = [];
}

/**
* @param {number} steps
* @return {string}
*/
back(steps: number): string {
while (steps-- > 0 && this.backHistory.length > 1) {
this.frontHistory.push(this.backHistory.pop()!);
}
return this.backHistory[this.backHistory.length - 1];
}

/**
* @param {number} steps
* @return {string}
*/
forward(steps: number): string {
while (steps-- > 0 && this.frontHistory.length > 0) {
this.backHistory.push(this.frontHistory.pop()!);
}
return this.backHistory[this.backHistory.length - 1];
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -640,6 +687,50 @@ impl BrowserHistory {
}
```

```typescript
class BrowserHistory {
private history: string[];
private cur: number;

/**
* @constructor
* @param {string} homepage
*/
constructor(homepage: string) {
this.history = [homepage];
this.cur = 0;
}

/**
* @param {string} url
* @return {void}
*/
visit(url: string): void {
this.cur++;
this.history = this.history.slice(0, this.cur);
this.history.push(url);
}

/**
* @param {number} steps
* @return {string}
*/
back(steps: number): string {
this.cur = Math.max(0, this.cur - steps);
return this.history[this.cur];
}

/**
* @param {number} steps
* @return {string}
*/
forward(steps: number): string {
this.cur = Math.min(this.history.length - 1, this.cur + steps);
return this.history[this.cur];
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -1002,6 +1093,57 @@ impl BrowserHistory {
}
```

```typescript
class BrowserHistory {
private history: string[];
private cur: number;
private n: number;

/**
* @constructor
* @param {string} homepage
*/
constructor(homepage: string) {
this.history = [homepage];
this.cur = 0;
this.n = 1;
}

/**
* @param {string} url
* @return {void}
*/
visit(url: string): void {
this.cur++;
if (this.cur === this.history.length) {
this.history.push(url);
this.n++;
} else {
this.history[this.cur] = url;
this.n = this.cur + 1;
}
}

/**
* @param {number} steps
* @return {string}
*/
back(steps: number): string {
this.cur = Math.max(0, this.cur - steps);
return this.history[this.cur];
}

/**
* @param {number} steps
* @return {string}
*/
forward(steps: number): string {
this.cur = Math.min(this.n - 1, this.cur + steps);
return this.history[this.cur];
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -1408,6 +1550,65 @@ impl BrowserHistory {
}
```

```typescript
class ListNode {
val: string;
prev: ListNode | null;
next: ListNode | null;

constructor(val: string, prev: ListNode | null = null, next: ListNode | null = null) {
this.val = val;
this.prev = prev;
this.next = next;
}
}

class BrowserHistory {
private cur: ListNode;

/**
* @constructor
* @param {string} homepage
*/
constructor(homepage: string) {
this.cur = new ListNode(homepage);
}

/**
* @param {string} url
* @return {void}
*/
visit(url: string): void {
this.cur.next = new ListNode(url, this.cur, null);
this.cur = this.cur.next;
}

/**
* @param {number} steps
* @return {string}
*/
back(steps: number): string {
while (this.cur.prev !== null && steps > 0) {
this.cur = this.cur.prev;
steps--;
}
return this.cur.val;
}

/**
* @param {number} steps
* @return {string}
*/
forward(steps: number): string {
while (this.cur.next !== null && steps > 0) {
this.cur = this.cur.next;
steps--;
}
return this.cur.val;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down
103 changes: 103 additions & 0 deletions articles/remove-duplicates-from-sorted-array-ii.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,39 @@ impl Solution {
}
```

```typescript
class Solution {
/**
* @param {number[]} nums
* @return {number}
*/
removeDuplicates(nums: number[]): number {
let n = nums.length;
if (n <= 2) return n;
let i = 0;
while (i < n - 1) {
if (nums[i] === nums[i + 1]) {
let j = i + 2,
cnt = 0;
while (j < n && nums[i] === nums[j]) {
j++;
cnt++;
}
for (let k = i + 2; k < n; k++) {
if (j >= n) break;
nums[k] = nums[j++];
}
n -= cnt;
i += 2;
} else {
i++;
}
}
return n;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -550,6 +583,31 @@ impl Solution {
}
```

```typescript
class Solution {
/**
* @param {number[]} nums
* @return {number}
*/
removeDuplicates(nums: number[]): number {
const count = new Map<number, number>();
for (const num of nums) {
count.set(num, (count.get(num) || 0) + 1);
}
let i = 0;
for (const [num, cnt] of count) {
nums[i++] = num;
count.set(num, cnt - 1);
if (count.get(num)! >= 1) {
nums[i++] = num;
count.set(num, cnt - 1);
}
}
return i;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -799,6 +857,32 @@ impl Solution {
}
```

```typescript
class Solution {
/**
* @param {number[]} nums
* @return {number}
*/
removeDuplicates(nums: number[]): number {
let l = 0,
r = 0;
while (r < nums.length) {
let count = 1;
while (r + 1 < nums.length && nums[r] === nums[r + 1]) {
r++;
count++;
}
for (let i = 0; i < Math.min(2, count); i++) {
nums[l] = nums[r];
l++;
}
r++;
}
return l;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down Expand Up @@ -957,6 +1041,25 @@ impl Solution {
}
```

```typescript
class Solution {
/**
* @param {number[]} nums
* @return {number}
*/
removeDuplicates(nums: number[]): number {
let l = 0;
for (const num of nums) {
if (l < 2 || num !== nums[l - 2]) {
nums[l] = num;
l++;
}
}
return l;
}
}
```

::tabs-end

### Time & Space Complexity
Expand Down