James Blog


  • Home

  • Archives

  • Tags

  • Search

[LeetCode] 26. Remove Duplicates from Sorted Array

Posted on 05-27-2019 | In LeetCode

Problem

Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

Example 1:

1
2
3
4
5
Given nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.

It doesn't matter what you leave beyond the returned length.

Example 2:

1
2
3
4
5
Given nums = [0,0,1,1,1,2,2,3,3,4],

Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.

It doesn't matter what values are set beyond the returned length.

Clarification:

Confused why the returned value is an integer but your answer is an array?

Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.

Internally you can think of this:

1
2
3
4
5
6
7
8
// nums is passed in by reference. (i.e., without making a copy)
int len = removeDuplicates(nums);

// any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
    print(nums[i]);
}
Read more »

[LeetCode] 25. Reverse Nodes in k-Group

Posted on 05-26-2019 | In LeetCode

Problem

Given a linked list, reverse the nodes of a linked list $k$ at a time and return its modified list.

$k$ is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of $k$ then left-out nodes in the end should remain as it is.

Example:

Given this linked list: 1->2->3->4->5

For $k$ = 2, you should return: 2->1->4->3->5

For $k$ = 3, you should return: 3->2->1->4->5

Note:

  • Only constant extra memory is allowed.
  • You may not alter the values in the list’s nodes, only nodes itself may be changed.
Read more »

[LeetCode] 24. Swap Nodes in Pairs

Posted on 05-25-2019 | In LeetCode

Problem

Given a linked list, swap every two adjacent nodes and return its head.

You may not modify the values in the list’s nodes, only nodes itself may be changed.

Example:

1
Given 1->2->3->4, you should return the list as 2->1->4->3.
Read more »

[LeetCode] 23. Merge k Sorted Lists

Posted on 05-24-2019 | In LeetCode

Problem

Merge $k$ sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

Example:

1
2
3
4
5
6
7
Input:
[
  1->4->5,
  1->3->4,
  2->6
]
Output: 1->1->2->3->4->4->5->6
Read more »

[LeetCode] 22. Generate Parentheses

Posted on 05-23-2019 | In LeetCode

Problem

Given $n$ pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

For example, given $n$ = 3, a solution set is:

1
2
3
4
5
6
7
[
  "((()))",
  "(()())",
  "(())()",
  "()(())",
  "()()()"
]
Read more »

[LeetCode] 21. Merge Two Sorted Lists

Posted on 05-22-2019 | In LeetCode

Problem

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

Example:

1
2
Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
Read more »

[LeetCode] 20. Valid Parentheses

Posted on 05-21-2019 | In LeetCode

Problem

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

An input string is valid if:

Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Note that an empty string is also considered valid.

Example 1:

1
2
Input: "()"
Output: true

Example 2:

1
2
Input: "()[]{}"
Output: true

Example 3:

1
2
Input: "(]"
Output: false

Example 4:

1
2
Input: "([)]"
Output: false

Example 5:

1
2
Input: "{[]}"
Output: true
Read more »

[LeetCode] 19. Remove Nth Node From End of List

Posted on 05-20-2019 | In LeetCode

Problem

Given a linked list, remove the $n$-th node from the end of list and return its head.

Example:

1
2
3
Given linked list: 1->2->3->4->5, and n = 2.

After removing the second node from the end, the linked list becomes 1->2->3->5.

Note:

Given $n$ will always be valid.

Follow up:

Could you do this in one pass?

Read more »

[LeetCode] 18. 4Sum

Posted on 05-19-2019 | In LeetCode

Problem

Given an array nums of $n$ integers and an integer target, are there elements $a$, $b$, $c$, and $d$ in nums such that $a + b + c + d =$ target? Find all unique quadruplets in the array which gives the sum of target.

Note:

The solution set must not contain duplicate quadruplets.

Example:

1
2
3
4
5
6
7
8
Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.

A solution set is:
[
  [-1,  0, 0, 1],
  [-2, -1, 1, 2],
  [-2,  0, 0, 2]
]
Read more »

[LeetCode] 17. Letter Combinations of a Phone Number

Posted on 05-18-2019 | In LeetCode

Problem

Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.

A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.

17. Letter Combinations of a Phone Number

Example:

1
2
Input: "23"
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

Note:

Although the above answer is in lexicographical order, your answer could be in any order you want.

Read more »
1 … 19 20 21 … 23
James Huang

James Huang

226 posts
4 categories
48 tags
GitHub LinkedIn Twitter Portfolio
© 2025 James Huang
Powered by Jekyll
Theme - NexT.Muse