0%

Description

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
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]

Difficulty: Medium

Code:

1
2
3
4
5
class Solution {
public List<String> generateParenthesis(int n) {

}
}

题意

给定n对括弧,生成所有正确形式组合的字符串。

阅读全文 »

Description

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

Difficulty: Easy

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {

}
}

题意

把K个有序链表合并成一个有序链表。

阅读全文 »

Description

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

An input string is valid if:

  1. Open brackets must be closed by the same type of brackets.
  2. 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

Difficulty: Easy

Code:

1
2
3
4
5
class Solution {
public boolean isValid(String s) {

}
}

题意

给定一个字符串,只包含”(){}[]”,判断输入到字符串是否有效。开括号和必须以同样到类型到闭括号关闭,开括号必须以正确到顺序关闭。

阅读全文 »

Description

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?

Difficulty: Medium

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {

}
}

题意

给定一个链表,移除链表的倒数第N个节点,并返回链表的头节点。限定N总是有效的不会超出链表长度,最好遍历一次。

阅读全文 »

Description

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]
]

Difficulty: Medium

Code:

1
2
3
4
5
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {

}
}

题意

给定n个整数和一个目标整数,从n个整数中找出所有不重复的4个数字组合,使得它们之和等于目标整数。

阅读全文 »