![](https://img-blog.csdnimg.cn/20201014180756926.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
str_818
这个作者很懒,什么都没留下…
展开
-
【LeetCode】443. 压缩字符串(String Compression)
压缩字符串【 英文练习 | 中文练习 】题目描述: 给定一组字符,使用原地算法将其压缩。压缩后的长度必须始终小于或等于原数组长度,数组的每个元素应该是长度为 1 的字符(不是 int 整数类型),在完成原地修改输入数组后,返回数组的新长度。示例 1:输入:["a","a","b","b","c","原创 2018-11-29 13:09:11 · 201 阅读 · 0 评论 -
【LeetCode】345. 反转字符串中的元音字母(Reverse Vowels of a String)
【 英文练习 | 中文练习 】题目描述: 编写一个函数,以字符串作为输入,反转该字符串中的元音字母。示例:输入: "hello"输出: "holle"解题思路: 双指针典型题目,注意元音字母不要只考虑小写的。public String reverseVowels(String s) { if(s == null || s.length() == 0) return s...原创 2018-12-04 12:32:33 · 171 阅读 · 0 评论 -
【LeetCode】80. 删除排序数组中的重复项 II(Remove Duplicates from Sorted Array II)
【 英文练习 | 中文练习 】题目描述: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度。不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。解题思路 : 遍历一遍数组,用一个下标记录替换位置,将所有元素重新赋值一遍。public int removeDuplicates(int[] nums) ...原创 2018-11-28 15:08:18 · 131 阅读 · 0 评论 -
【LeetCode】11. 盛最多水的容器(Container With Most Water)
【 英文练习 | 中文练习 】题目描述: 给定 n 个非负数 a1,a2,…,an,每个数代表坐标中的一个点 (i,ai)。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i,ai) 和 (i,0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。 示例:输入: [1,8,6,2,5,4,8,3,7]输出: 49解题思路: 两线段之间形成的区域总是...原创 2018-11-28 15:11:14 · 121 阅读 · 0 评论 -
【LeetCode】125. 验证回文串(Valid Palindrome)
【 英文练习 | 中文练习 】题目描述: 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,忽略字母的大小写。解题思路: 用双指针的思想,从字符串的头部与尾部向中间移动比较。public boolean isPalindrome(String s) { if (s.isEmpty()) return true; int head = 0, tail = s.len...原创 2018-11-28 15:13:05 · 117 阅读 · 0 评论 -
【LeetCode】151. 翻转字符串里的单词(Reverse Words in a String)
【 英文练习 | 中文练习 】题目描述: 给定一个字符串,逐个翻转字符串中的每个单词。示例:输入: "the sky is blue"输出: "blue is sky the"说明:无空格字符构成一个单词。输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。解法一: 使用 Java API...原创 2018-11-28 15:14:48 · 344 阅读 · 0 评论 -
【LeetCode】680. 验证回文字符串 Ⅱ(Valid Palindrome II)
【 英文练习 | 中文练习 】题目描述: 给定一个非空字符串,最多删除一个字符,判断是否能成为回文字符串。示例:输入: "abca"输出: True解释: 可以删除c字符public boolean validPalindrome(String s) { int left = 0, right = s.length() - 1; char[] ca = s.toC...原创 2018-12-05 13:38:31 · 245 阅读 · 0 评论 -
【LeetCode】167. 两数之和 Ⅱ - 输入有序数组(Two Sum II - Input array is sorted)
【 英文练习 | 中文练习 】题目描述: 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数,函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2。说明:返回的下标值(index1 和 index2)不是从零开始的。你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素。示例:输入: number...原创 2018-12-03 14:19:43 · 118 阅读 · 0 评论 -
【LeetCode】633. 平方数之和(Sum of Square Numbers)
【 英文练习 | 中文练习 】题目描述: 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,c 等于 a 和 b 的平方和。示例:输入: 5输出: True解释: 1 * 1 + 2 * 2 = 5解法一: 利用双指针的思想,需要注意两个坑点,一个是 a 可能等于 b ,第二个是 a 和 b 可能为 0 。b 的起始位置为 Math.sqrt(c)。public b...原创 2018-12-03 15:25:49 · 125 阅读 · 0 评论 -
【LeetCode】905. 按奇偶排序数组(Sort Array By Parity)
【 英文练习 | 中文练习 】题目描述: 将一个数组中所有的偶数放到前半部分,所有的奇数放到后半部分。可以返回满足此条件的任何数组作为答案。解题思路 :可以重新开辟一个数组,遍历两遍元素组,分别将其中的偶数与奇数取出来。还有一种方法是利用双指针的思想,额外空间复杂度为 O(1) 。public int[] sortArrayByParity(int[] A) { if (A ==...原创 2018-12-12 21:29:23 · 151 阅读 · 0 评论 -
【LeetCode】88. 合并两个有序数组(Merge Sorted Array)
【 英文练习 | 中文练习 】题目描述: 给定两个有序数组,合并它们,合并之后的数组依旧有序。解题思路: 从后向前存放。public void merge(int[] nums1, int m, int[] nums2, int n) { if(nums1 == null || nums2 == null) return; while...原创 2018-12-06 16:07:03 · 171 阅读 · 0 评论 -
【LeetCode】141. 环形链表(Linked List Cycle)
【 英文练习 | 中文练习 】题目描述: 给定一个链表,判断链表中是否有环。解题思路: 一种方法可以使用 Hash Table ,判断该结点之前是否遇到过;更优的方法是使用双指针,一个指针每次移动一个结点,一个指针每次移动两个结点,如果存在环,那么这两个指针一定会相遇。public boolean hasCycle(ListNode head) { if (head == nul...原创 2018-12-07 14:29:45 · 138 阅读 · 0 评论 -
【LeetCode】524. 通过删除字母匹配到字典里最长单词(Longest Word in Dictionary through Deleting)
【 英文练习 | 中文练习 】题目描述: 给定一个字符串和一个字符串字典,找到字典里面最长的字符串,该字符串可以通过删除给定字符串的某些字符来得到。如果答案不止一个,返回长度最长且字典顺序最小的字符串。如果答案不存在,则返回空字符串。示例1:输入:s = "abpcplea", d = ["ale","apple","monkey","plea"]输出: "apple"原创 2018-12-10 10:44:18 · 234 阅读 · 0 评论 -
【Leetcode】70. 爬楼梯(Climbing Stairs)
Leetcode - 70 Climbing Stairs (Easy)题目描述:共有 n 个台阶,每次只能往上爬 1 个或者 2 个台阶,求爬 n 个台阶共有几种不同的爬法。Input: 2Output: 2Explanation: There are two ways to climb to the top.1. 1 step + 1 step2. 2 steps解题思路:首...原创 2019-05-05 12:55:42 · 143 阅读 · 0 评论 -
【Leetcode】198. 打家劫舍(House Robber)
Leetcode - 198 House Robber (Easy)题目描述:一个小偷沿着一条街偷窃,给定数组表示每家屋子的金额,如果偷窃连续的两间屋子就会触发警报,求在不触发警报的前提下,偷窃的最大金额。Input: [1,2,3,1]Output: 4Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3...原创 2019-05-05 13:27:48 · 117 阅读 · 0 评论 -
【Leetcode】213. 打家劫舍II(House Robber II)
Leetcode - 213 House Robber II (Medium)题目描述:一个小偷沿着一条环形的街偷窃,给定数组表示每家屋子的金额,如果偷窃连续的两间屋子就会触发警报,求在不触发警报的前提下,偷窃的最大金额。Input: [2,3,2]Output: 3Explanation: You cannot rob house 1 (money = 2) and then rob h...原创 2019-05-05 13:44:50 · 128 阅读 · 0 评论 -
【Leetcode】509.斐波那契数列(Fibonacci Number)
Leetcode - 509 Fibonacci Number (Easy)F(0) = 0, F(1) = 1F(N) = F(N - 1) + F(N - 2), for N > 1.public int fib(int N) { if(N <= 1) return N; int f1 = 0, f2 = 1; for(int i = 2; i ...原创 2019-05-08 10:04:23 · 215 阅读 · 0 评论 -
【Leetcode】91. 解码方法(Decode Ways)
Leetcode - 91 Decode Ways (Medium)题目描述:给定一个字符串,字母 A - Z 可以分别解码成 1 - 26,求字符串共有几种解码方法。Input: "226"Output: 3Explanation: It could be decoded as "BZ" (2 26), "VF" (22 6), or "BBF" (2 2 6).解题思路:要注意 0...原创 2019-05-08 10:11:46 · 210 阅读 · 0 评论 -
【Leetcode】62. 不同的路径(Unique Paths)
Leetcode - 62 Unique Paths (Medium)题目描述:机器人只能向右或向下移动,给定矩阵的长和宽,求从左上角移动到右下角共有几种不同的路径。 Input: m = 3, n = 2Output: 3Explanation:From the top-left corner, there are a total of 3 ways to reach the b...原创 2019-05-06 09:28:24 · 176 阅读 · 0 评论 -
【Leetcode】63. 不同的路径 II(Unique Paths II)
Leetcode - 63 Unique Paths II (Medium)题目描述:和上一道题类似,只不过在路径中添加了障碍物,给定路径数组,1 表示障碍物,0 表示正常方块。Input:[ [0,0,0], [0,1,0], [0,0,0]]Output: 2Explanation:There is one obstacle in the middle of the ...原创 2019-05-06 09:53:51 · 117 阅读 · 0 评论 -
【Leetcode】120. 三角形(Triangle)
Leetcode - 120 Triangle (Medium)题目描述:给定一个三角形形状的集合,从上到下依次向邻接位置移动,求移动的最小路径和。[ [2], [3,4], [6,5,7], [4,1,8,3]]The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11)....原创 2019-05-06 10:16:43 · 200 阅读 · 0 评论 -
【Leetcode】279. 完全平方数(Perfect Squares)
Leetcode - 279 Perfect Squares (Medium)题目描述:给定一个正整数,求最少能由几个完全平方数组成。Input: n = 13Output: 2Explanation: 13 = 4 + 9.解题思路:定义状态 dp[n] 表示 n 的最少完全平方数,状态方程为 dp[n] = Min{ dp[n - i*i] + 1 }。dp[0] = 0 dp...原创 2019-05-06 10:33:17 · 202 阅读 · 0 评论 -
【Leetcode】139. 拆分词句(Word Break)
Leetcode - 139 Word Break (Medium)题目描述:给定一个字符串 s 与字典 wordDict,判断 s 是否能拆分成 wordDict 中的子字符串。Input: s = "leetcode", wordDict = ["leet", "code"]Output: trueExplanation: Return true because "leetcode" ...原创 2019-05-06 15:46:38 · 202 阅读 · 0 评论 -
【Leetcode】300.最长上升子序列(Longest Increasing Subsequence)
Leetcode - 300 Longest Increasing Subsequence (Medium)题目描述:给定无序的整数数组,找到最长上升子序列的长度。Input: [10,9,2,5,3,7,101,18]Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], therefore th...原创 2019-05-09 10:19:09 · 91 阅读 · 0 评论 -
【Leetcode】64. 最小路径和(Minimum Path Sum)
Leetcode - 64 Minimum Path Sum (Medium)题目描述:给定 m × n 的矩阵,只能向下或向右移动,求从左上角到右下角路径的最小路径和。Input:[ [1,3,1], [1,5,1], [4,2,1]]Output: 7Explanation: Because the path 1→3→1→1→1 minimizes the sum....原创 2019-05-06 18:38:39 · 129 阅读 · 0 评论 -
【Leetcode】75. 排序颜色(Sort Colors)
Leetcode - 75 Sort Colors (Medium)题目描述:0、1、2 分别代表三种颜色,给定由这三个数组组成的数组,将数组内的元素按照 0、1、2 的顺序排列。Input: [2,0,2,1,1,0]Output: [0,0,1,1,2,2]public void sortColors(int[] nums) { int zero = -1, one = 0,...原创 2019-05-19 15:05:09 · 277 阅读 · 0 评论 -
【Leetcode】242. 判断两字符串是否由相同字符组成(Valid Anagram)
Leetcode - 242 Valid Anagram (Easy)public boolean isAnagram(String s, String t) { int[] alphabet = new int[26]; for (int i ) for (char c : s.toCharArray()) alphabet[c - 'a']++; for (c...原创 2019-05-19 15:15:05 · 422 阅读 · 0 评论 -
【Leetcode】409. 最长回文字符串(Longest Palindrome)
Leetcode - 409 Longest Palindrome (Easy)题目描述:求给定字符串中的字符能够组成最长字符串的长度。Input:"abccccdd"Output:7Explanation:One longest palindrome that can be built is "dccaccd", whose length is 7.public int l...原创 2019-05-19 15:28:41 · 178 阅读 · 0 评论 -
【Leetcode】205. 同构字符串(Isomorphic Strings)
Leetcode - 205 Isomorphic Strings (Easy)Input: s = "egg", t = "add"Output: trueInput: s = "foo", t = "bar"Output: false解题思路:判断上一次字母出现的位置是否相同。public boolean isIsomorphic(String s, String t) { ...原创 2019-05-19 18:10:24 · 200 阅读 · 0 评论 -
【Leetcode】303. 区间和检索(Range Sum Query)
Leetcode - 303 Range Sum Query - Immutable (Easy)题目描述:给定一个整型数组,计算下标 i 与 j 之间的和。Given nums = [-2, 0, 3, -5, 2, -1]sumRange(0, 2) -> 1sumRange(2, 5) -> -1sumRange(0, 5) -> -3解题思路:定义 dp[...原创 2019-05-07 09:01:34 · 86 阅读 · 0 评论 -
【Leetcode】413. 算数切片(Arithmetic Slices)
Leetcode - 413 Arithmetic Slices (Medium)题目描述:给定一个数组,求数组中长度在 3 个以上(包含三个)的等差数列的数量。A = [1, 2, 3, 4]return: 3, for 3 arithmetic slices in A: [1, 2, 3], [2, 3, 4] and [1, 2, 3, 4] itself.数组等差数列...原创 2019-05-07 09:57:15 · 106 阅读 · 0 评论 -
【Leetcode】647. 回文子串的数量(Palindromic Substrings)
Leetcode - 647 Palindromic Substrings (Medium)Input: "aaa"Output: 6Explanation: Six palindromic strings: "a", "a", "a", "aa", "aa", "aaa".解题思路:依次以每个字符为中心向外扩展,计算回文子串的数量。int count = 0; public ...原创 2019-05-19 19:05:59 · 193 阅读 · 0 评论 -
【Leetcode】646. 链对的最大长度(Maximum Length of Pair Chain)
Leetcode - 646 Maximum Length of Pair Chain (Medium)题目描述:对于 (a, b) 和 (c, d) ,如果 b < c,则它们可以构成一条链,求最大链的长度。解法一:DPpublic int findLongestChain(int[][] pairs) { int n = pairs.length; Arrays...原创 2019-05-10 11:09:06 · 157 阅读 · 0 评论 -
【Leetcode】376. 摆动子序列(Wiggle Subsequence)
Leetcode - 376 Wiggle Subsequence (Medium)题目描述:一大一小就构成了摆动序列,求最长的摆动序列。Input: [1,17,5,10,13,15,10,5,16,8]Output: 7Explanation: There are several subsequences that achieve this length. One is [1,17,1...原创 2019-05-10 12:18:44 · 178 阅读 · 0 评论 -
【Leetcode】343. 整数拆分(Integer Break)
Leetcode - 343 Integer Break (Medium)题目描述:给定一个整数 n,求相加等于 n 并且成绩最大的一组整数的积。Input: 10Output: 36Explanation: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36.解题思路:定义状态 dp[i] 为 i 对应的最大乘积。public int integerBreak(int n...原创 2019-05-07 22:50:56 · 128 阅读 · 0 评论 -
【Leetcode】9. 判断回文数(Palindrome Number)
Leetcode - 9 Palindrome Number (Easy)Input: -121Output: falseExplanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.public boolean i...原创 2019-05-20 19:16:20 · 187 阅读 · 0 评论 -
【Leetcode】696. 计算二进制子字符串(Count Binary Substrings)
Leetcode - 696 Count Binary Substrings (Easy)题目描述:给定一个由 0 和 1 组成的字符串,求有多少连续分组的子串。Input: "00110011"Output: 6Explanation: There are 6 substrings that have equal number of consecutive 1's and 0's: "0...原创 2019-05-20 19:43:17 · 314 阅读 · 0 评论 -
【Leetcode】283. 将数组中的 0 移动到后面(Move Zeros)
Leetcode - 283 Move Zeros (Easy)public void moveZeroes(int[] nums) { int index = 0; for (int num : nums) { if (num != 0) { nums[index++] = num; } } while (...原创 2019-05-20 22:47:07 · 225 阅读 · 0 评论 -
【Leetcode】724. 寻找中枢节点(Find Pivot Index)
Leetcode - 724 Find Pivot Index (Easy)题目描述:以中枢点为界,左右两边元素之和相等。Input: nums = [1, 7, 3, 6, 5, 6]Output: 3Explanation: The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum...原创 2019-05-20 23:11:25 · 230 阅读 · 0 评论 -
【Leetcode】566.改变数组维度(Reshape the Matrix)
Leetcode - 566 Reshape the Matrix (Easy)题目描述:将原数组 reshape 成另一个数组,原数组中的元素按行遍历重新赋到新数组中。Input: nums = [[1,2], [3,4]]r = 1, c = 4Output: [[1,2,3,4]]Explanation:The row-traversing of nums is [1,2...原创 2019-05-21 08:29:11 · 863 阅读 · 0 评论