- 博客(140)
- 收藏
- 关注
原创 LeetCode 17. Letter Combinations of a Phone Number
先特判,再创建一个String数组来存储数字和字母之间的对应关系,剩下的就是经典的DFS了。class Solution { public List<String> letterCombinations(String digits) { List<String> res = new ArrayList<>(); if(digits == null || digits.length() == 0){ re
2020-07-30 08:26:15
103
原创 LeetCode 136. Single Number
class Solution { public int singleNumber(int[] nums) { int res = 0; for(int i = 0; i < nums.length; i++){ res ^= nums[i]; } return res; }}
2020-07-26 15:03:31
70
原创 LeetCode 13. Roman to Integer
我们可以每次跟前面的数字比较,如果小于等于前面的数字,先加上当前的数字,比如 "VI",第二个字母 'I' 小于第一个字母 'V',所以要加1。如果大于的前面的数字,加上当前的数字减去二倍前面的数字,这样可以把在上一个循环多加数减掉,比如 "IX",我们在 i=0 时,加上了第一个字母 'I' 的值,此时结果 res 为1。当 i=1 时,字母 'X' 大于前一个字母 'I',这说明前面的1是要减去的,而由于前一步不但没减,还多加了个1,所以此时要减去2倍的1,就是减2,所以才能得到9,整个过程是 res
2020-07-26 14:58:05
75
原创 LeetCode 12. Integer to Roman
class Solution { public String intToRoman(int num) { String[] M = new String[] {"", "M", "MM", "MMM"}; String[] C = new String[] {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}; String[] X = new String[] {"", "X", .
2020-07-26 14:13:53
78
原创 LeetCode 219. Contains Duplicate II
class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { // Corner Case if(nums == null || nums.length == 0){ return false; } Map<Integer, Integer> seen = new HashMap<>(); .
2020-07-26 13:46:49
113
原创 LeetCode 220. Contains Duplicate III
可以用TreeSet来解决问题,但是为了防止溢出的问题,必须将类型强制转化成long(对应的包装类为Long)。https://leetcode-cn.com/problems/contains-duplicate-iii/solution/hua-dong-chuang-kou-er-fen-sou-suo-shu-zhao-shang-/class Solution { public boolean containsNearbyAlmostDuplicate(int[] nums, i
2020-07-26 13:44:29
75
原创 JZ7 斐波那契数列
public class Solution { public int Fibonacci(int n) { if(n == 0){ return 0; }else if(n == 1){ return 1; } int prev = 0, curr = 1; for(int i = 2; i <= n; i++){ curr += prev;.
2020-07-26 09:52:39
93
原创 JZ6 旋转数组的最小数字
这道题和LeetCode 154一样。import java.util.ArrayList;public class Solution { public int minNumberInRotateArray(int [] array) { // Corner Case if(array.length == 0){ return 0; } int left = 0, right = array.length
2020-07-26 09:21:05
163
原创 JZ14 链表中倒数第k个结点
这道题需要用双指针去做:先让fast走k步,再让fast和slow一起走直到fast走过最后一个node。记得要考虑corner case。https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/solution/mian-shi-ti-22-lian-biao-zhong-dao-shu-di-kge-j-11//*public class ListNode { int val;
2020-07-26 09:05:46
102
原创 LeetCode 190. Reverse Bits
public class Solution { // you need treat n as an unsigned value public int reverseBits(int n) { int left = 31, right = 0; while(left > right){ // We must let n take the return value otherwise its value won't change.
2020-07-25 09:33:46
98
原创 LeetCode 217. Contains Duplicate
class Solution { public boolean containsDuplicate(int[] nums) { if(nums == null || nums.length == 0){ return false; } Set<Integer> seen = new HashSet<>(); for(int num: nums){ // If th.
2020-07-25 08:48:38
177
原创 LeetCode 191. Number of 1 Bits
public class Solution { // you need to treat n as an unsigned value public int hammingWeight(int n) { int count = 0; // We should use n != 9 rather than n > 0 while(n != 0){ // Extract the least significant b.
2020-07-25 07:54:44
98
原创 LeetCode 231. Power of Two
class Solution { public boolean isPowerOfTwo(int n) { if(n <= 0){ return false; } while(n % 2 == 0){ n /= 2; } return n == 1; }}
2020-07-25 07:38:02
142
原创 JZ2 替换空格
public class Solution { public String replaceSpace(StringBuffer str) { int count = 0; int oldLength = str.length(); for(int i = 0; i < oldLength; i++){ if(str.charAt(i) == ' '){ count++; .
2020-07-21 17:16:26
92
原创 LeetCode 49. Group Anagrams
首先初始化 key = "0#0#0#0#0#",数字分别代表 abcde 出现的次数,# 用来分割。这样的话,"abb" 就映射到了 "1#2#0#0#0"。"cdc" 就映射到了 "0#0#2#1#0"。"dcc" 就映射到了 "0#0#2#1#0"。代码如下:class Solution { public List<List<String>> groupAnagrams(String[] strs) { // Corner case...
2020-07-19 15:19:54
97
原创 LeetCode 415. Add Strings
从后往前遍历两个String,把对应位的和放入StringBuilder。循环结束后检查是否还有进位,有的话再添加一位1。将StringBuilder反转后再转化成String输出。代码如下:class Solution { public String addStrings(String num1, String num2) { int i = num1.length() - 1, j = num2.length() - 1; StringBuilder res
2020-07-19 14:47:49
91
原创 LeetCode 91. Decode Ways
Approach 1 Recursionclass Solution { public int numDecodings(String s) { return helper(s, 0); } private int helper(String s, int start){ if(start == s.length()){ return 1; } if(s.charAt(start) == '
2020-07-19 13:00:02
100
原创 LeetCode 205. Isomorphic Strings
其实我们不需要将字符串完全转换,我们可以用两个 map 分别记录两个字符串每个字母的映射。将所有字母初始都映射到 0。记录过程中,如果发现了当前映射不一致,就可以立即返回 false 了。这里我们可以直接用char来做索引,使用的是它们对应的ASCII码的值。代码如下:class Solution { public boolean isIsomorphic(String s, String t) { int[] map1 = new int[128]; int[
2020-07-19 09:00:51
78
原创 LeetCode 5. Longest Palindromic Substring
第 1 步:定义状态dp[i][j] 表示子串 s[i..j] 是否为回文子串,这里子串 s[i..j] 定义为左闭右闭区间,可以取到 s[i] 和 s[j]。第 2 步:思考状态转移方程在这一步分类讨论(根据头尾字符是否相等),根据上面的分析得到:dp[i][j] = (s[i] == s[j]) and dp[i + 1][j - 1]说明:「动态规划」事实上是在填一张二维表格,由于构成子串,因此 i 和 j 的关系是 i <= j ,因此,只需要填这张表格对角线以上的部分。看到 dp
2020-07-19 07:07:59
250
原创 LeetCode 28. Implement strStr()
暴力法,双循环。外循环代表substring在haystack的起始位置,内循环代表我们正在比较needle的index。代码如下:class Solution { public int strStr(String haystack, String needle) { // Corner case if(needle.isEmpty()){ return 0; } for(int i = 0; i <
2020-07-19 06:12:37
74
原创 LeetCode 9. Palindrome Number
取出后半段数字进行翻转。这里需要注意的一个点就是由于回文数的位数可奇可偶,所以当它的长度是偶数时,它对折过来应该是相等的;当它的长度是奇数时,那么它对折过来后,有一个的长度需要去掉一位数(除以 10 并取整)。具体做法如下:每次进行取余操作 ( %10),取出最低的数字:y = x % 10将最低的数字加到取出数的末尾:revertNum = revertNum * 10 + y每取一个最低位数字,x 都要自除以 10判断 x 是不是大于revertNum ,当它不大于的时候,说明数字已.
2020-07-18 08:46:38
88
原创 LeetCode 125. Valid Palindrome
这道题就是用left和right两个指针去遍历String就好了。还有就是记住Character的几个API:isLetterOrDigit() 和toLowerCase()。代码如下:class Solution { public boolean isPalindrome(String s) { int left = 0, right = s.length() - 1; while(left < right){ while(le.
2020-07-18 07:51:38
84
原创 LeetCode 3. Longest Substring Without Repeating Characters
这道题是一道滑动窗口的典型题目。class Solution { public int lengthOfLongestSubstring(String s) { Set<Character> window = new HashSet<>(); int slow = 0, fast = 0, ans = 0, n = s.length(); while(fast < n){ if(!window
2020-07-18 07:01:31
82
原创 LaiCode 397. Right Shift By N Characters
public class Solution { public String rightShift(String input, int n) { // Write your solution here if(input.length() == 0){ return input; } char[] array = input.toCharArray(); int k = n % array.length; reverse(array, 0, ar.
2020-07-12 09:08:30
233
原创 LeetCode 15. 3Sum
首先对数组进行排序,排序后固定一个数 nums[i],再使用左右指针指向 nums[i]后面的两端,数字分别为 nums[L] 和 nums[R],计算三个数的和 sum 判断是否满足为 0,满足则添加进结果集。如果 nums[i]大于 0,则三数之和必然无法等于 0,结束循环如果 nums[i] == nums[i-1],则说明该数字重复,会导致结果重复,所以应该跳过当 sum == 0 时,nums[L] == nums[L+1] 则会导致结果重复,应该跳过,L++当 sum == 0时,nu.
2020-07-12 08:43:44
111
原创 LaiCode 197. ReOrder Array
public class Solution { public int[] reorder(int[] array) { // Write your solution here if (array.length % 2 == 1) { reorder(array, 0, array.length - 2); } else { reorder(array, 0, array.length - 1); } return array; } private void reo.
2020-07-09 09:10:22
246
原创 LeetCode 186. Reverse Words in a String II
这道题其实是151的基础。代码如下:class Solution { public void reverseWords(char[] s) { reverse(s, 0, s.length - 1); int head = 0, tail = 0; for(int i = 0; i < s.length; i++){ if(s[i] != ' ' && (i == 0 || s[i - 1] == '
2020-07-08 14:07:11
112
原创 LeetCode 151. Reverse Words in a String
class Solution { public String reverseWords(String s) { // Remove the spaces String str = removeSpace(s); char[] words = str.toCharArray(); // Reverse the whole sentence reverse(words, 0, words.length - 1); ...
2020-07-08 10:49:13
142
原创 LeetCode 344. Reverse String
Iterative Approach class Solution { public void reverseString(char[] s) { int left = 0, right = s.length - 1; while(left < right){ char temp = s[left]; s[left] = s[right]; s[right] = temp;
2020-07-08 09:37:25
117
原创 LaiCode 281. Remove Spaces
public class Solution { public String removeSpaces(String input) { // Write your solution here if(input.length() == 0){ return input; } char[] array = input.toCharArray(); int slow = 0; for(int fast = 0; fast < array.len.
2020-07-08 09:32:47
188
原创 LeetCode 67. Add Binary
StringBuilder的append()方法可以直接使用int作为参数:Thejava.lang.StringBuilder.append(int i)method appends the string representation of theintargument to this sequence.class Solution { public String addBinary(String a, String b) { StringBuilder sb = n...
2020-07-08 08:44:54
85
原创 LeetCode 242. Valid Anagram
我觉得这道题有点类似于比较两个字符串的字母出现频率直方图https://leetcode-cn.com/problems/valid-anagram/solution/hua-jie-suan-fa-242-you-xiao-de-zi-mu-yi-wei-ci-by/class Solution { public boolean isAnagram(String s, String t) { // First check their lengths if(
2020-07-07 14:09:55
148
原创 String类题目会用到的API(持续更新)
String类:toCharArray() 方法将字符串转换为字符数组。经常出现在for循环中 e.g., for(char c : s.toCharArray())。 length() 方法用于返回字符串的长度。 charAt() 方法用于返回指定索引处的字符,索引范围为从 0 到 length() - 1。 String() 构造方法可以将char array转换为字符串。StringBuilder类:toString() 方法返回此序列中数据的字符串表示形式。 append()
2020-07-07 09:31:55
147
原创 LeetCode 1119. Remove Vowels from a String
class Solution { public String removeVowels(String S) { StringBuilder sb = new StringBuilder(); for(char c: S.toCharArray()){ if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'){ continue; .
2020-07-07 09:13:51
100
原创 LeetCode 82. Remove Duplicates from Sorted List II
首先加一个假头,然后双指针prev和curr。主要考虑两种情况:1 -> 2, 1 -> 1 -> 2。https://leetcode.wang/leetCode-82-Remove-Duplicates-from-Sorted-ListII.html/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * L
2020-07-07 08:24:37
77
原创 LeetCode 80. Remove Duplicates from Sorted Array II
https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii/solution/shan-chu-pai-xu-shu-zu-zhong-de-zhong-fu-xiang-i-2/class Solution { public int removeDuplicates(int[] nums) { int slow = 0; for(int fast = 0; fast <
2020-07-07 07:35:30
97
原创 LeetCode 26. Remove Duplicates from Sorted Array
class Solution { public int removeDuplicates(int[] nums) { int slow = 0; for(int fast = 0; fast < nums.length; fast++){ if(slow == 0 || nums[fast] > nums[slow - 1]){ nums[slow++] = nums[fast]; .
2020-07-06 16:26:18
66
原创 LeetCode 216. Combination Sum III
class Solution { public List<List<Integer>> combinationSum3(int k, int n) { List<List<Integer>> res = new ArrayList<>(); backtrack(k, n, 1, new ArrayList<>(), res); return res; } priv.
2020-07-06 10:08:52
83
原创 LeetCode 90. Subsets II
Approach 1如果当前的数字等于path的最后一个数字,那么我们就不考虑不添加这个数的情况了。注意条件是path.size() == 0 而不是 index == 0, 这是因为index != 0时仍然存在path.size() == 0的情况,导致path.size() - 1 < 0,path.get()方法报错。class Solution { public List<List<Integer>> subsetsWithDup(int[] num
2020-07-06 08:29:39
148
原创 LeetCode 47. Permutations II
https://leetcode-cn.com/problems/permutations-ii/solution/hui-su-suan-fa-python-dai-ma-java-dai-ma-by-liwe-2/对于这个解法,我还有另一个视角的理解。在nums排序以后,考虑几个相同的数,如果排在某个数前面的数还未被使用(used[i - 1] == false),那么我们不得使用该数。其实这里所做的工作就是强行给这几个相同的数做了出现次序规定:一定要前面的加入到path以后,才能再添加后面的数。所
2020-07-04 08:41:43
102
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人
RSS订阅