文章 224
评论 6
浏览 200762
LinkedList - 25. Reverse Nodes in k-Group

LinkedList - 25. Reverse Nodes in k-Group

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

LinkedList - 24. Swap Nodes in Pairs

LinkedList - 24. Swap Nodes in Pairs

24. Swap Nodes in Pairs 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: Given 1->2->3->4, you should return the list as 2->1->4->3. 思路: 链表的翻转实现题,题目意思是翻转相邻的两个节点,题目难点在于容易产生断链的情况。所有链表的题目,基本都是先做一个dummy节点去指向头结点,然后再去用其他指针操作链表的时候,就很方便。题目可以扩展为每隔k个节点翻转一次。 代码: java: /** * Definition for singly-linked list. * public class ListNode { * int val; ....

LinkedList - 141. Linked List Cycle

LinkedList - 141. Linked List Cycle

141. Linked List Cycle Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list. Example 1: Input: head = [3,2,0,-4], pos = 1 Output: true Explanation: There is a cycle in the linked list, where tail connects to the second node. Follow up: Can you solve ....

LinkedList - 206. Reverse Linked List

LinkedList - 206. Reverse Linked List

Reverse a singly linked list.

Subsequence - 187. Repeated DNA Sequences

Subsequence - 187. Repeated DNA Sequences

187. Repeated DNA Sequences All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: “ACGAATTCCG”. When studying DNA, it is sometimes useful to identify repeated sequences within the DNA. Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule. 思路: 题目意思是找出重复的序列,采用滑动窗口和hashset不可以重复的特性来做。 代码: java: class Solution { public List<String> findRepeatedDnaSequences(String s) { Set curr = new HashSet....

parentheses - 241. Different Ways to Add Parentheses

parentheses - 241. Different Ways to Add Parentheses

Different Ways to Add Parentheses Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +, - and *. Example 1: Input: “2-1-1” Output: [0, 2] Explanation: ((2-1)-1) = 0 (2-(1-1)) = 2 Example 2: Input: "2*3-4*5" Output: [-34, -14, -10, -10, 10] Explanation: (2*(3-(4*5))) = -34 ((2*3)-(4*5)) = -14 ((2*(3-4))*5) = -10 (2*((3-4)*5)) = -10 (((2*3)-4)*5) = 10 思路:....

parentheses - 32. Longest Valid Parentheses

parentheses - 32. Longest Valid Parentheses

Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. Example 1: Input: “(()” Output: 2 Explanation: The longest valid parentheses substring is “()” Example 2: Input: “)()())” Output: 4 Explanation: The longest valid parentheses substring is “()()” 思路: 括号问题常规解法都是使用栈。 代码: java: class Solution { public int longestValidParentheses(String s) { Stack<Integer> stack = ne....

parentheses - 22. Generate Parentheses

parentheses - 22. Generate Parentheses

22. Generate Parentheses 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: [ “((()))”, “(()())”, “(())()”, “()(())”, “()()()” ] 思路: 题目意思是给一个整形数字,返回合法左右括号这种组合的所有组合,典型的使用回溯法来做。 代码: java: class Solution { public List<String> generateParenthesis(int n) { List<String> res = new ArrayList<String>(); backtrack(res, "", 0, 0, n); return res; } private void ba....

palindrome - 132. Palindrome Partitioning II

palindrome - 132. Palindrome Partitioning II

132. Palindrome Partitioning II Given a string s, partition s such that every substring of the partition is a palindrome. Return the minimum cuts needed for a palindrome partitioning of s. Example: Input: “aab” Output: 1 Explanation: The palindrome partitioning [“aa”,“b”] could be produced using 1 cut. 思路: 这一题和131题的条件增加了只要找出切割字符串最少的一种分割方法。用dp来做,状态转移方程就是i到j是否是文字符串。 代码: java: class Solution { public int minCut(String s) { char[] c = s.toCharArray(); int n = c.leng....

palindrome - 131. Palindrome Partitioning

palindrome - 131. Palindrome Partitioning

131. Palindrome Partitioning Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. Example: Input: “aab” Output: [ [“aa”,“b”], [“a”,“a”,“b”] ] 思路: 题目意思是找出字符串中子串是回文字符串的所有组合,使用回溯来做。 代码: java: public class Solution { List<List<String>> resultLst; ArrayList<String> currLst; public List<List<String>> partition(String s) { resultLst = new ArrayList<Li....

Parentheses - 20. Valid Parentheses

Parentheses - 20. Valid Parentheses

20. Valid Parentheses 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: Input: “()” Output: true 思路: 使用栈来做,遇到左括号入栈,遇到右括号出栈,如果不匹配就不合法。 代码: java: class Solution { /*public boolean ....

Palindrome  - 9. Palindrome Number

Palindrome - 9. Palindrome Number

Palindrome Number Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: Input: 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome. Follow up: Coud you solve it wi....

Palindrome - 5. Longest Palindromic Substring

Palindrome - 5. Longest Palindromic Substring

Longest Palindromic Substring Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example 1: Input: “babad” Output: “bab” Note: “aba” is also a valid answer. Example 2: Input: “cbbd” Output: “bb” 思路: 找出字符串中的回文字符串,解法有很多,比如dp,这里采用中容易想到的中心扩散法。主要区分一下偶回文和奇回文的处理,时间复杂度O(n^2),空间复杂度O(1) 代码: java: class Solution { public String longestPalindrome(String s) { if (s == null || s.length() == 0) return ""; int start = 0,....

Palindrome - 125. Valid Palindrome

Palindrome - 125. Valid Palindrome

Given a string s, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.

Sliding Window - 395. Longest Substring with At Least K Repeating Characters

Sliding Window - 395. Longest Substring with At Least K Repeating Characters

395. Longest Substring with At Least K Repeating Characters Find the length of the longest substring T of a given string (consists of lowercase letters only) such that every character in T appears no less than k times. Example 1: Input: s = "aaabb", k = 3 Output: 3 The longest substring is "aaa", as 'a' is repeated 3 times. Example 2: Input: s = "ababbc", k = 2 Output: 5 The longest substring is "ababb", as 'a' is repeated 2 times and 'b' is repeated 3 times. ....

Sliding Window - 340. Longest Substring with At Most K Distinct Characters

Sliding Window - 340. Longest Substring with At Most K Distinct Characters

Longest Substring with At Most K Distinct Characters Given a string S, find the length of the longest substring T that contains at most k distinct characters. Example 1: Input: S = “eceba” and k = 3 Output: 4 Explanation: T = “eceb” Example 2: Input: S = “WORLD” and k = 4 Output: 4 Explanation: T = “WORL” or “ORLD” 思路: 题目意思是说给定一个k,找出一个连续子串,子串里面最多只允许出现k个不同的字符。所以,使用两个指针来限定窗口大小,当超过k个不同的字符的时候,就调整窗口大小,而调整窗口的时候,需要用字符出现的频率来区分去除的字符是重复的还是不重复的。 代码: java: public class Solution { /** *....

Sliding Window - 3. Longest Substring Without Repeating Characters

Sliding Window - 3. Longest Substring Without Repeating Characters

3. Longest Substring Without Repeating Characters Given a string, find the length of the longest substring without repeating characters. Example 1: Input: “abcabcbb” Output: 3 Explanation: The answer is “abc”, with the length of 3. Example 2: Input: “bbbbb” Output: 1 Explanation: The answer is “b”, with the length of 1. Example 3: Input: “pwwkew” Output: 3 Explanation: The answer is “wke”, with the length of 3. Note that the answer must be a substring, “pwke” is a subsequence....

Sliding Window - 30. Substring with Concatenation of All Words

Sliding Window - 30. Substring with Concatenation of All Words

30. Substring with Concatenation of All Words 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: Input: s = “barfoothefoobarman”, words = [“foo”,“bar”] Output: [0,9] Explanation: Substrings starting at index 0 and 9 are “barfoor” and “foobar” respectively. The output or....

Sliding Window - 76. Minimum Window Substring

Sliding Window - 76. Minimum Window Substring

76. Minimum Window Substring Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). Example: Input: S = “ADOBECODEBANC”, T = “ABC” Output: “BANC” Note: If there is no such window in S that covers all characters in T, return the empty string "". If there is such window, you are guaranteed that there will always be only one unique minimum window in S. 思路: 这是一道经典的sliding window的题目,题目意思是给定两个字符串,字符串S如果包含另一个字符串T的所有字符,....

Nothing just happens, it's all part of a plan.