diff --git a/com/zerroi/leetcode/Three15/CanConstruct.java b/com/zerroi/leetcode/Three15/CanConstruct.java new file mode 100644 index 0000000..b168d52 --- /dev/null +++ b/com/zerroi/leetcode/Three15/CanConstruct.java @@ -0,0 +1,9 @@ +package com.zerroi.leetcode.Three15; + +public class CanConstruct { +} +class SolutionThird { + public boolean canConstruct(String ransomNote, String magazine) { + return magazine.contains(ransomNote); + } +} diff --git a/com/zerroi/leetcode/Three15/Convert.java b/com/zerroi/leetcode/Three15/Convert.java new file mode 100644 index 0000000..d412d38 --- /dev/null +++ b/com/zerroi/leetcode/Three15/Convert.java @@ -0,0 +1,43 @@ +package com.zerroi.leetcode.Three15; + +import java.util.ArrayList; +import java.util.List; + +/* +* 将一个给定字符串 s 根据给定的行数 numRows ,以从上往下、从左到右进行 Z 字形排列。 + +比如输入字符串为 "PAYPALISHIRING" 行数为 3 时,排列如下: + +P A H N +A P L S I I G +Y I R +之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"PAHNAPLSIIGYIR"。 + +请你实现这个将字符串进行指定行数变换的函数: + +string convert(string s, int numRows); +* */ +public class Convert { +} + +class SolutionFirst { + public String convert(String s, int numRows) { + if (numRows < 2) return s; + List list = new ArrayList<>(); + for (int i = 0; i < numRows; i++) { + list.add(new StringBuilder()); + } + int flag = -1, i = 0; + for (char c : s.toCharArray()) { + list.get(i).append(c); + if (i == 0 || i == numRows - 1) flag = -flag; + i += flag; + } + StringBuilder res = new StringBuilder(); + for (StringBuilder builder : list) { + res.append(builder); + } + return res.toString(); + } +} + diff --git a/com/zerroi/leetcode/Three15/LengthOfLongestSubstring.java b/com/zerroi/leetcode/Three15/LengthOfLongestSubstring.java new file mode 100644 index 0000000..a9ba2e2 --- /dev/null +++ b/com/zerroi/leetcode/Three15/LengthOfLongestSubstring.java @@ -0,0 +1,34 @@ +package com.zerroi.leetcode.Three15; + +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +/*给定一个字符串 s ,请你找出其中不含有重复字符的 最长 +子串 + 的长度。*/ +public class LengthOfLongestSubstring { + public static void main(String[] args) { + + } +} + +class SolutionSecond { + public int lengthOfLongestSubstring(String s) { + Set set = new HashSet<>(); + int start = 0; + int end = 0; + int res = 0; + while (end < s.length()) { + if (!set.contains(s.charAt(end))) { + set.add(s.charAt(end)); + end++; + } else { + set.remove(s.charAt(start)); + start++; + } + res = Math.max(res, set.size()); + } + return res; + } +} diff --git a/com/zerroi/leetcode/Three16/IsValidSudoku.java b/com/zerroi/leetcode/Three16/IsValidSudoku.java new file mode 100644 index 0000000..9a5064f --- /dev/null +++ b/com/zerroi/leetcode/Three16/IsValidSudoku.java @@ -0,0 +1,89 @@ +package com.zerroi.leetcode.Three16; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class IsValidSudoku { + public static void main(String[] args) { + System.out.println(5 / 3); + } +} + +class SolutionFirst { + public boolean isValidSudoku(char[][] board) { + Set set = new HashSet<>(); + for (int i = 0; i < 9; i++) { + for (int j = 0; j < 9; j++) { + char c = board[i][j]; + if (c != '.'&& set.contains(c)) return false; + set.add(c); + } + set.clear(); + } + + Set setRow = new HashSet<>(); + for (int i = 0; i < 9; i++) { + for (int j = 0; j < 9; j++) { + char c = board[j][i]; + if (c != '.' && setRow.contains(c)) return false; + setRow.add(c); + } + setRow.clear(); + } + + Set setBlock = new HashSet<>(); + for (int i = 0; i < 9; i++) { + int m = i / 3; + int n = i % 3; + setBlock.clear(); + for (int k = m * 3; k < m * 3 + 3; k++) { + for (int j = n * 3; j < n * 3 + 3; j++) { + char c = board[k][j]; + if (c != '.' && setBlock.contains(c)) return false; + setBlock.add(c); + } + } + } + return true; + } + + public boolean isValidSudoku2(char[][] board) { + Set set = new HashSet<>(); + Map map = new HashMap<>(); + for (int i = 0; i < 9; i++) { + for (int j = 0; j < 9; j++) { + char c = board[i][j]; + if (c != '.'&& set.contains(c)) return false; + map.put(c, 1); + } + set.clear(); + } + + Set setRow = new HashSet<>(); + for (int i = 0; i < 9; i++) { + for (int j = 0; j < 9; j++) { + char c = board[j][i]; + if (c != '.' && setRow.contains(c)) return false; + setRow.add(c); + } + setRow.clear(); + } + + Set setBlock = new HashSet<>(); + for (int i = 0; i < 9; i++) { + int m = i / 3; + int n = i % 3; + setBlock.clear(); + for (int k = m * 3; k < m * 3 + 3; k++) { + for (int j = n * 3; j < n * 3 + 3; j++) { + char c = board[k][j]; + if (c != '.' && setBlock.contains(c)) return false; + setBlock.add(c); + } + } + } + return true; + } +} diff --git a/com/zerroi/leetcode/Three16/SpiralOrder.java b/com/zerroi/leetcode/Three16/SpiralOrder.java new file mode 100644 index 0000000..ad10317 --- /dev/null +++ b/com/zerroi/leetcode/Three16/SpiralOrder.java @@ -0,0 +1,46 @@ +package com.zerroi.leetcode.Three16; + +import java.util.ArrayList; +import java.util.List; + +public class SpiralOrder { +} + +class SolutionSecond { + public List spiralOrder(int[][] matrix) { + List res = new ArrayList<>(); + int left = 0, right = matrix.length; + int top = 0, bottom = matrix[0].length; + int len = right * bottom; + int i = 0; + while (i < len) { + for (int j = left; j < right; j++) { + res.add(matrix[top][j]); + ++i; + if (i >= len) return res; + } + ++top; + + for (int j = top; j < bottom; j++) { + res.add(matrix[j][right - 1]); + ++i; + if (i >= len) return res; + } + --right; + for (int j = right - 1; j >= left; j--) { + res.add(matrix[bottom-1][j]); + ++i; + if (i >= len) return res; + } + ++left; + for (int j = bottom - 1; j >= top; j--) { + res.add(matrix[j][left]); + ++i; + if (i >= len) return res; + + } + --bottom; + } + return res; + } +} \ No newline at end of file diff --git a/com/zerroi/leetcode/Three17/GameOfLife.java b/com/zerroi/leetcode/Three17/GameOfLife.java new file mode 100644 index 0000000..a1cf218 --- /dev/null +++ b/com/zerroi/leetcode/Three17/GameOfLife.java @@ -0,0 +1,13 @@ +package com.zerroi.leetcode.Three17; + +public class GameOfLife { + public static void main(String[] args) { + + } +} + +class Solution { + public void gameOfLife(int[][] board) { + + } +} diff --git a/com/zerroi/leetcode/Three17/Rotate.java b/com/zerroi/leetcode/Three17/Rotate.java new file mode 100644 index 0000000..5487768 --- /dev/null +++ b/com/zerroi/leetcode/Three17/Rotate.java @@ -0,0 +1,35 @@ +package com.zerroi.leetcode.Three17; + +import java.util.Arrays; + +public class Rotate { + public static void main(String[] args) { + int[][] matrix = new int[][]{{5,1,9,11},{2,4,8,10},{13,3,6,7},{15,14,12,16}}; + SolutionFirst solutionFirst = new SolutionFirst(); + solutionFirst.rotate(matrix); + } +} +/* +* 给定一个 n × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。 + +* */ +class SolutionFirst { + public void rotate(int[][] matrix) { + int n = matrix.length; + int[][] res = new int[n][n]; + int indexCol; + int indexRow = 0; + for (int i = n - 1; i >= 0; i--) { + indexCol = n - 1; + for (int j = n - 1; j >= 0; j--) { + res[indexCol][indexRow] = matrix[i][j]; + indexCol--; + } + indexRow++; + } + + for (int i = 0; i < n; i++) { + System.arraycopy(res[i], 0, matrix[i], 0, n); + } + } +} diff --git a/com/zerroi/leetcode/Three17/SetZeroes.java b/com/zerroi/leetcode/Three17/SetZeroes.java new file mode 100644 index 0000000..58c856e --- /dev/null +++ b/com/zerroi/leetcode/Three17/SetZeroes.java @@ -0,0 +1,59 @@ +package com.zerroi.leetcode.Three17; + +public class SetZeroes { + public static void main(String[] args) { + SolutionSecond solutionSecond = new SolutionSecond(); + solutionSecond.setZeroes(new int[][]{{0,1,2,0},{3,4,5,2},{1,3,1,5}}); + + } +} + +class SolutionSecond { + public void setZeroes(int[][] matrix) { + int m = matrix.length; + int n = matrix[0].length; + int[][] position = new int[m][n]; + + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (matrix[i][j] == 0) position[i][j] = 1; + } + } + + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (position[i][j] == 1) { + for (int k = 0; k < n; k++) { + if (matrix[i][k] != 0) matrix[i][k] = 0; + } + for (int l = 0; l < n; l++) { + if (matrix[l][j] != 0) matrix[l][j] = 0; + } + } + } + } + } + + + public void setZeroes(int[][] matrix, int a) { + int m = matrix.length; + int n = matrix[0].length; + boolean[] col = new boolean[m]; + boolean[] row = new boolean[n]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (matrix[i][j] == 0) { + col[i] = row[j] = true; + } + } + } + + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (col[i] || row[j] ) { + matrix[i][j] = 0; + } + } + } + } +} \ No newline at end of file diff --git a/com/zerroi/leetcode/Three18/CanConstruct.java b/com/zerroi/leetcode/Three18/CanConstruct.java new file mode 100644 index 0000000..294ab03 --- /dev/null +++ b/com/zerroi/leetcode/Three18/CanConstruct.java @@ -0,0 +1,60 @@ +package com.zerroi.leetcode.Three18; + +import java.util.HashMap; +import java.util.Map; + +public class CanConstruct { + public static void main(String[] args) { + SolutionFirst solutionFirst = new SolutionFirst(); + boolean res = solutionFirst.canConstruct("aa", "aab", 1); + System.out.println("res = " + res); + + } +} + +class SolutionFirst { + public boolean canConstruct(String ransomNote, String magazine) { + if (magazine.length() < ransomNote.length()) return false; + + Map map = new HashMap<>(); + for (char c : magazine.toCharArray()) { + if (map.containsKey(c)) { + map.put(c, map.get(c) + 1); + } else { + map.put(c, 1); + } + } + + for (char c : ransomNote.toCharArray()) { +// 如果magazine中不包涵ransomNote中的某一个字符 + if (!map.containsKey(c)) { + return false; + } else { + Integer i = map.get(c); + if (i <= 0) { + return false; + } + else map.put(c, i-1); + } + + } + + return true; + } + + public boolean canConstruct(String ransomNote, String magazine, int i) { + char[] ransomNoteCharArray = ransomNote.toCharArray(); + char[] magazineCharArray = magazine.toCharArray(); + int[] count = new int[26]; + for (char c : magazineCharArray) { + count[c - 'a']++; + } + + for (char c : ransomNoteCharArray) { + count[c - 'a']--; + if (count[c - 'a'] < 0) return false; + } + + return true; + } +} \ No newline at end of file diff --git a/com/zerroi/leetcode/Three18/IsIsomorphic.java b/com/zerroi/leetcode/Three18/IsIsomorphic.java new file mode 100644 index 0000000..4f7aad2 --- /dev/null +++ b/com/zerroi/leetcode/Three18/IsIsomorphic.java @@ -0,0 +1,52 @@ +package com.zerroi.leetcode.Three18; + +import java.util.HashMap; +import java.util.Map; + +public class IsIsomorphic { + public static void main(String[] args) { + SolutionSecond solutionSecond = new SolutionSecond(); + boolean res = solutionSecond.isIsomorphic("badc", "baba"); + System.out.println("res = " + res); + } +} + +/*给定两个字符串 s 和 t ,判断它们是否是同构的。 + +如果 s 中的字符可以按某种映射关系替换得到 t ,那么这两个字符串是同构的。 + +每个出现的字符都应当映射到另一个字符,同时不改变字符的顺序。不同字符不能映射到同一个字符上,相同字符只能映射到同一个字符上,字符可以映射到自己本身。*/ +class SolutionSecond { + public boolean isIsomorphic(String s, String t) { + if (s.length() != t.length()) return false; + + Map sToT = new HashMap<>(); + Map tToS = new HashMap<>(); + + + for (int i = 0; i < s.length(); i++) { + char charS = s.charAt(i); + char charT = t.charAt(i); + + // Check s to t mapping + if (sToT.containsKey(charS)) { + if (sToT.get(charS) != charT) { + return false; // Conflict in mapping + } + } else { + sToT.put(charS, charT); + } + + // Check t to s mapping + if (tToS.containsKey(charT)) { + if (tToS.get(charT) != charS) { + return false; // Conflict in mapping + } + } else { + tToS.put(charT, charS); + } + } + return true; + } +} + diff --git a/com/zerroi/leetcode/Three18/WordPattern.java b/com/zerroi/leetcode/Three18/WordPattern.java new file mode 100644 index 0000000..fb7a623 --- /dev/null +++ b/com/zerroi/leetcode/Three18/WordPattern.java @@ -0,0 +1,45 @@ +package com.zerroi.leetcode.Three18; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +public class WordPattern { + public static void main(String[] args) { + SolutionThird solutionThird = new SolutionThird(); + boolean res = solutionThird.wordPattern("aaaa", "dog cat cat dog"); + System.out.println("res = " + res); + + } +} + +class SolutionThird { + public boolean wordPattern(String pattern, String s) { + HashMap patternToS = new HashMap<>(); + HashMap SToPattern = new HashMap<>(); + String[] strs = s.split(" "); + if (pattern.length() != strs.length) { + return false; + } + for (int i = 0; i < pattern.length(); i++) { + char c = pattern.charAt(i); + String str = strs[i]; + if (patternToS.containsKey(c)) { + if (!Objects.equals(patternToS.get(c), str)) { + return false; + } + } else { + patternToS.put(c, str); + } + + if (SToPattern.containsKey(str)) { + if (SToPattern.get(str) != c) { + return false; + } + } else { + SToPattern.put(str, c); + } + } + return true; + } +} diff --git a/com/zerroi/leetcode/Three19/GroupAnagrams.java b/com/zerroi/leetcode/Three19/GroupAnagrams.java new file mode 100644 index 0000000..babc596 --- /dev/null +++ b/com/zerroi/leetcode/Three19/GroupAnagrams.java @@ -0,0 +1,60 @@ +package com.zerroi.leetcode.Three19; + +import java.util.*; + +public class GroupAnagrams { + public static void main(String[] args) { + SolutionSecond solutionSecond = new SolutionSecond(); + List> res = solutionSecond.groupAnagrams(new String[]{"eat", "tea", "tan", "ate", "nat", "bat"}); + res.forEach(System.out::println); + } +} + +class SolutionSecond { + public List> groupAnagrams(String[] strs) { + if (strs.length <= 1) Arrays.asList(strs); + + List> res = new ArrayList<>(); + List path = new ArrayList<>(); + boolean[] walked = new boolean[strs.length]; + for (int i = 0; i < strs.length; i++) { + if (walked[i]) continue; + path.add(strs[i]); + for (int j = i + 1; j < strs.length; j++) { + if (isAnagram(strs[i], strs[j])) { + path.add(strs[j]); + walked[j] = true; + } + } + res.add(new ArrayList<>(path)); + path.clear(); + } + return res; + } + + private boolean isAnagram(String s, String t) { + if (s.length() != t.length()) return false; + int[] count = new int[26]; + for (char c : s.toCharArray()) { + count[c - 'a']++; + } + for (char c : t.toCharArray()) { + count[c - 'a']--; + if (count[c - 'a'] < 0) return false; + } + return true; + } + + public List> groupAnagrams(String[] strs, int a) { + Map> map = new HashMap<>(); + for (int i = 0; i < strs.length; i++) { + char[] array = strs[i].toCharArray(); + Arrays.sort(array); + String str = new String(array); + List list = map.getOrDefault(str, new ArrayList<>()); + list.add(strs[i]); + map.put(str, list); + } + return new ArrayList<>(map.values()); + } +} diff --git a/com/zerroi/leetcode/Three19/IsAnagram.java b/com/zerroi/leetcode/Three19/IsAnagram.java new file mode 100644 index 0000000..6c2f8ae --- /dev/null +++ b/com/zerroi/leetcode/Three19/IsAnagram.java @@ -0,0 +1,22 @@ +package com.zerroi.leetcode.Three19; + +public class IsAnagram { + public static void main(String[] args) { + + } +} + +class SolutionFirst { + public boolean isAnagram(String s, String t) { + if (s.length() != t.length()) return false; + int[] count = new int[26]; + for (char c : s.toCharArray()) { + count[c - 'a']++; + } + for (char c : t.toCharArray()) { + count[c - 'a']--; + if (count[c - 'a'] < 0) return false; + } + return true; + } +} diff --git a/com/zerroi/leetcode/Three19/TwoSum.java b/com/zerroi/leetcode/Three19/TwoSum.java new file mode 100644 index 0000000..0693fb1 --- /dev/null +++ b/com/zerroi/leetcode/Three19/TwoSum.java @@ -0,0 +1,23 @@ +package com.zerroi.leetcode.Three19; + +import java.util.HashMap; +import java.util.Map; + +public class TwoSum { + public static void main(String[] args) { + + } +} + +class SolutionThird { + public int[] twoSum(int[] nums, int target) { + Map map = new HashMap<>(); + for (int i = 0; i < nums.length; i++) { + if(map.containsKey(target - nums[i])){ + return new int[]{i, map.get(target - nums[i])}; + } + map.put(nums[i], i); + } + return new int[0]; + } +} diff --git a/com/zerroi/leetcode/Three20/ContainsNearbyDuplicate.java b/com/zerroi/leetcode/Three20/ContainsNearbyDuplicate.java new file mode 100644 index 0000000..5364fac --- /dev/null +++ b/com/zerroi/leetcode/Three20/ContainsNearbyDuplicate.java @@ -0,0 +1,27 @@ +package com.zerroi.leetcode.Three20; + +import java.util.HashMap; +import java.util.Map; + +public class ContainsNearbyDuplicate { + public static void main(String[] args) { + SolutionSecond solutionSecond = new SolutionSecond(); + boolean res = solutionSecond.containsNearbyDuplicate(new int[]{1,2,3,1}, 3); + System.out.println("res = " + res); + } +} + +class SolutionSecond { + public boolean containsNearbyDuplicate(int[] nums, int k) { + Map map = new HashMap<>(); + for (int i = 0; i < nums.length; i++) { + if (map.containsKey(nums[i])) { + if (Math.abs(map.get(nums[i]) - i) <= k) { + return true; + } + } + map.put(nums[i], i); + } + return false; + } +} diff --git a/com/zerroi/leetcode/Three20/IsHappy.java b/com/zerroi/leetcode/Three20/IsHappy.java new file mode 100644 index 0000000..956266c --- /dev/null +++ b/com/zerroi/leetcode/Three20/IsHappy.java @@ -0,0 +1,40 @@ +package com.zerroi.leetcode.Three20; + +import java.util.HashMap; +import java.util.Map; + +public class IsHappy { + public static void main(String[] args) { + SolutionFirst solutionFirst = new SolutionFirst(); + boolean res = solutionFirst.isHappy(2); + System.out.println("res = " + res); + } +} + +class SolutionFirst { + public boolean isHappy(int n) { + return isHappyHelper(n, new HashMap<>()); + } + + private boolean isHappyHelper(int n, Map map) { + if (n == 1) return true; + + int key = getNum(n); + if (map.containsKey(key)) { + return false; + } + + map.put(key, 1); + return isHappyHelper(key, map); + } + + private int getNum(int n) { + int sum = 0; + while (n > 0) { + int a = n % 10; + n /= 10; + sum += a * a; + } + return sum; + } +} diff --git a/com/zerroi/leetcode/Three20/LongestConsecutive.java b/com/zerroi/leetcode/Three20/LongestConsecutive.java new file mode 100644 index 0000000..f21af96 --- /dev/null +++ b/com/zerroi/leetcode/Three20/LongestConsecutive.java @@ -0,0 +1,39 @@ +package com.zerroi.leetcode.Three20; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +public class LongestConsecutive { + public static void main(String[] args) { + SolutionThird solutionThird = new SolutionThird(); + int res = solutionThird.longestConsecutive(new int[]{0,3,7,2,5,8,4,6,0,1}); + System.out.println("res = " + res); + } + +} +class SolutionThird { + public int longestConsecutive(int[] nums) { + if (nums.length == 0) return 0; + + Set set = new HashSet<>(); + for (int num : nums) { + set.add(num); + } + + int longest = 0; + for (int num : nums) { + if (!set.contains(num - 1)) { + int currentNum = num; + int currentLength = 1; + while (set.contains(currentNum + 1)) { + currentNum++; + currentLength++; + } + longest = Math.max(longest, currentLength); + } + } + return longest; + } +}