0%

Description

Given a string containing just the characters ‘(‘ and ‘)’, find the length of the longest valid (well-formed) parentheses substring.

Example 1:

1
2
3
Input: "(()"
Output: 2
Explanation: The longest valid parentheses substring is "()"

Example 2:

1
2
3
Input: ")()())"
Output: 4
Explanation: The longest valid parentheses substring is "()()"

Difficulty: Hard

Code:

1
2
3
4
5
class Solution {
public int longestValidParentheses(String s) {

}
}

题意

给定一个只包含()的字符串,求最长有效括号的子串的长度。

阅读全文 »

Description

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).

The replacement must be in-place and use only constant extra memory.

Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.

1,2,3 → 1,3,2

3,2,1 → 1,2,3

1,1,5 → 1,5,1

Difficulty: Medium

Code:

1
2
3
4
5
class Solution {
public void nextPermutation(int[] nums) {

}
}

题意

求全排列下一个排列顺序,参考如下。

1
2
3
4
5
6
7
8
9
10
Input: [1,2,3]
Output:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
阅读全文 »

Description

You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in words exactly once and without any intervening characters.

Example 1:

1
2
3
4
5
6
Input:
s = "barfoothefoobarman",
words = ["foo","bar"]
Output: [0,9]
Explanation: Substrings starting at index 0 and 9 are "barfoor" and "foobar" respectively.
The output order does not matter, returning [9,0] is fine too.

Example 2:

1
2
3
4
Input:
s = "wordgoodgoodgoodbestword",
words = ["word","good","best","word"]
Output: []

Difficulty: Hard

Code:

1
2
3
4
5
class Solution {
public List<Integer> findSubstring(String s, String[] words) {

}
}

题意

给定字符串s和一系列单词words,单词的长度都相等。找出所有匹配的子串的起始位置,匹配的字符子串需是由所有这些单词串联一次而成。

阅读全文 »

Description

Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator.

Return the quotient after dividing dividend by divisor.

The integer division should truncate toward zero.

Example 1:

1
2
Input: dividend = 10, divisor = 3
Output: 3

Example 2:

1
2
Input: dividend = 7, divisor = -3
Output: -2

Note:

  • Both dividend and divisor will be 32-bit signed integers.
  • The divisor will never be 0.
  • Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 231 − 1 when the division result overflows.

Difficulty: Medium

Code:

1
2
3
4
5
class Solution {
public int divide(int dividend, int divisor) {

}
}

题意

实现整数的除法,禁止使用乘法、除法、取余。整数范围为有符号int,除数不会是0,结果溢出后返回231 − 1。

阅读全文 »

Description

Implement strStr().

Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Example 1:

1
2
Input: haystack = "hello", needle = "ll"
Output: 2

Example 2:

1
2
Input: haystack = "aaaaa", needle = "bba"
Output: -1

Clarification:

What should we return when needle is an empty string? This is a great question to ask during an interview.

For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C’s strstr() and Java’s indexOf().

Difficulty: Easy

Code:

1
2
3
4
5
class Solution {
public int strStr(String haystack, String needle) {

}
}

题意

实现String.indexOf()的功能,返回needle在haystack第一次出现的下标,若不存在则返回-1。若needle是空字符串,则返回0。

阅读全文 »