From 48daf27020dcea29e5ab23e54b04950c0ea217b3 Mon Sep 17 00:00:00 2001 From: shuzijun Date: Mon, 12 Jul 2021 11:04:00 +0800 Subject: [PATCH 01/11] update --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 863e88c..6a63ea3 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # leetcode-question - [leetcode-editor](https://github.com/shuzijun/leetcode-editor) custom code demo + [leetcode-editor](https://github.com/shuzijun/leetcode-editor) custom code demo + This branch is a private record branch. For configuration examples, please refer to master. ## leetcode-editor config CodeFileName: ```java From 00fa9f5ca0a5904064eee294b58cf26a700148c4 Mon Sep 17 00:00:00 2001 From: shuzijun Date: Tue, 3 Aug 2021 10:24:06 +0800 Subject: [PATCH 02/11] update --- .idea/leetcode-pro/editor.xml | 45 ++++++ README.md | 10 +- .../editor/cn/Q29DivideTwoIntegers.java | 47 ++++++ ...0SubstringWithConcatenationOfAllWords.java | 62 ++++++++ .../editor/cn/Q31NextPermutation.java | 97 ++++++++++++ .../editor/cn/Q32LongestValidParentheses.java | 116 ++++++++++++++ .../leetcode/editor/cn/Q36ValidSudoku.java | 114 ++++++++++++++ .../editor/cn/doc/content/Q1TwoSum.md | 144 ++++++++++++++---- .../cn/doc/content/Q29DivideTwoIntegers.md | 30 ++++ ...Q30SubstringWithConcatenationOfAllWords.md | 42 +++++ .../cn/doc/content/Q31NextPermutation.md | 45 ++++++ .../doc/content/Q32LongestValidParentheses.md | 40 +++++ .../editor/cn/doc/content/Q36ValidSudoku.md | 61 ++++++++ 13 files changed, 818 insertions(+), 35 deletions(-) create mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/Q29DivideTwoIntegers.java create mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/Q30SubstringWithConcatenationOfAllWords.java create mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/Q31NextPermutation.java create mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/Q32LongestValidParentheses.java create mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/Q36ValidSudoku.java create mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q29DivideTwoIntegers.md create mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q30SubstringWithConcatenationOfAllWords.md create mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q31NextPermutation.md create mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q32LongestValidParentheses.md create mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q36ValidSudoku.md diff --git a/.idea/leetcode-pro/editor.xml b/.idea/leetcode-pro/editor.xml index 5cbad66..951080a 100644 --- a/.idea/leetcode-pro/editor.xml +++ b/.idea/leetcode-pro/editor.xml @@ -30,6 +30,15 @@ + + + + + + @@ -39,6 +48,33 @@ + + + + + + + + + + + + + + + + + + @@ -66,6 +102,15 @@ + + + + + + diff --git a/README.md b/README.md index 6a63ea3..871c5cf 100644 --- a/README.md +++ b/README.md @@ -4,16 +4,16 @@ ## leetcode-editor config CodeFileName: ```java - $!velocityTool.camelCaseName(${question.titleSlug}) + Q${question.frontendQuestionId}$!velocityTool.camelCaseName(${question.titleSlug}) ``` CodeTemplate: ```java + package com.shuzijun.leetcode.editor.cn; ${question.content} - package com.shuzijun.leetcode.editor.en; - public class $!velocityTool.camelCaseName(${question.titleSlug}){ - public static void main(String[] args) { - Solution solution = new $!velocityTool.camelCaseName(${question.titleSlug})().new Solution(); + public class Q${question.frontendQuestionId}$!velocityTool.camelCaseName(${question.titleSlug}){ + public static void main(String[] args) { + Solution solution = new Q${question.frontendQuestionId}$!velocityTool.camelCaseName(${question.titleSlug})().new Solution(); } ${question.code} } diff --git a/src/main/java/com/shuzijun/leetcode/editor/cn/Q29DivideTwoIntegers.java b/src/main/java/com/shuzijun/leetcode/editor/cn/Q29DivideTwoIntegers.java new file mode 100644 index 0000000..c397622 --- /dev/null +++ b/src/main/java/com/shuzijun/leetcode/editor/cn/Q29DivideTwoIntegers.java @@ -0,0 +1,47 @@ +package com.shuzijun.leetcode.editor.cn; +/** +

给定两个整数,被除数 dividend 和除数 divisor。将两数相除,要求不使用乘法、除法和 mod 运算符。

+ +

返回被除数 dividend 除以除数 divisor 得到的商。

+ +

整数除法的结果应当截去(truncate)其小数部分,例如:truncate(8.345) = 8 以及 truncate(-2.7335) = -2

+ +

 

+ +

示例 1:

+ +
输入: dividend = 10, divisor = 3
+输出: 3
+解释: 10/3 = truncate(3.33333..) = truncate(3) = 3
+ +

示例 2:

+ +
输入: dividend = 7, divisor = -3
+输出: -2
+解释: 7/-3 = truncate(-2.33333..) = -2
+ +

 

+ +

提示:

+ +
    +
  • 被除数和除数均为 32 位有符号整数。
  • +
  • 除数不为 0。
  • +
  • 假设我们的环境只能存储 32 位有符号整数,其数值范围是 [−231,  231 − 1]。本题中,如果除法结果溢出,则返回 231 − 1。
  • +
+
Related Topics
  • 位运算
  • 数学

  • 👍 625
  • 👎 0
  • +*/ + +public class Q29DivideTwoIntegers{ + public static void main(String[] args) { + Solution solution = new Q29DivideTwoIntegers().new Solution(); + } + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public int divide(int dividend, int divisor) { + return 0; + } +} +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/com/shuzijun/leetcode/editor/cn/Q30SubstringWithConcatenationOfAllWords.java b/src/main/java/com/shuzijun/leetcode/editor/cn/Q30SubstringWithConcatenationOfAllWords.java new file mode 100644 index 0000000..b7ff8e4 --- /dev/null +++ b/src/main/java/com/shuzijun/leetcode/editor/cn/Q30SubstringWithConcatenationOfAllWords.java @@ -0,0 +1,62 @@ +package com.shuzijun.leetcode.editor.cn; + +import java.util.List; + +/** +

    给定一个字符串 s 和一些 长度相同 的单词 words找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置。

    + +

    注意子串要与 words 中的单词完全匹配,中间不能有其他字符 ,但不需要考虑 words 中单词串联的顺序。

    + +

    + +

    示例 1:

    + +
    +输入:s = "barfoothefoobarman", words = ["foo","bar"]
    +输出:[0,9]
    +解释:
    +从索引 0 和 9 开始的子串分别是 "barfoo" 和 "foobar" 。
    +输出的顺序不重要, [9,0] 也是有效答案。
    +
    + +

    示例 2:

    + +
    +输入:s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]
    +输出:[]
    +
    + +

    示例 3:

    + +
    +输入:s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]
    +输出:[6,9,12]
    +
    + +

    + +

    提示:

    + +
      +
    • 1 <= s.length <= 104
    • +
    • s 由小写英文字母组成
    • +
    • 1 <= words.length <= 5000
    • +
    • 1 <= words[i].length <= 30
    • +
    • words[i] 由小写英文字母组成
    • +
    +
    Related Topics
  • 哈希表
  • 字符串
  • 滑动窗口

  • 👍 510
  • 👎 0
  • +*/ + +public class Q30SubstringWithConcatenationOfAllWords{ + public static void main(String[] args) { + Solution solution = new Q30SubstringWithConcatenationOfAllWords().new Solution(); + } + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public List findSubstring(String s, String[] words) { + return null; + } +} +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/com/shuzijun/leetcode/editor/cn/Q31NextPermutation.java b/src/main/java/com/shuzijun/leetcode/editor/cn/Q31NextPermutation.java new file mode 100644 index 0000000..c7802fc --- /dev/null +++ b/src/main/java/com/shuzijun/leetcode/editor/cn/Q31NextPermutation.java @@ -0,0 +1,97 @@ +package com.shuzijun.leetcode.editor.cn; + +import java.util.Arrays; + +/** + *

    实现获取 下一个排列 的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列。

    + * + *

    如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列)。

    + * + *

    必须 原地 修改,只允许使用额外常数空间。

    + * + *

    + * + *

    示例 1:

    + * + *
    + * 输入:nums = [1,2,3]
    + * 输出:[1,3,2]
    + * 
    + * + *

    示例 2:

    + * + *
    + * 输入:nums = [3,2,1]
    + * 输出:[1,2,3]
    + * 
    + * + *

    示例 3:

    + * + *
    + * 输入:nums = [1,1,5]
    + * 输出:[1,5,1]
    + * 
    + * + *

    示例 4:

    + * + *
    + * 输入:nums = [1]
    + * 输出:[1]
    + * 
    + * + *

    + * + *

    提示:

    + * + *
      + *
    • 1 <= nums.length <= 100
    • + *
    • 0 <= nums[i] <= 100
    • + *
    + *
    Related Topics
  • 数组
  • 双指针

  • 👍 1247
  • 👎 0
  • + */ + +public class Q31NextPermutation { + public static void main(String[] args) { + Solution solution = new Q31NextPermutation().new Solution(); + int[] nums = new int[]{2,3,1}; + solution.nextPermutation(nums); + System.out.println(Arrays.toString(nums)); + } + + // [4,5,2,6,3,1] + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public void nextPermutation(int[] nums) { + int i = nums.length - 2; + while (i >= 0 && nums[i] >= nums[i + 1]) { + i--; + } + if (i >= 0) { + int j = nums.length - 1; + while (j >= 0 && nums[i] >= nums[j]) { + j--; + } + swap(nums, i, j); + } + reverse(nums, i + 1); + } + + public void swap(int[] nums, int i, int j) { + int temp = nums[i]; + nums[i] = nums[j]; + nums[j] = temp; + } + + public void reverse(int[] nums, int start) { + int left = start, right = nums.length - 1; + while (left < right) { + swap(nums, left, right); + left++; + right--; + } + } + + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/com/shuzijun/leetcode/editor/cn/Q32LongestValidParentheses.java b/src/main/java/com/shuzijun/leetcode/editor/cn/Q32LongestValidParentheses.java new file mode 100644 index 0000000..e2d920d --- /dev/null +++ b/src/main/java/com/shuzijun/leetcode/editor/cn/Q32LongestValidParentheses.java @@ -0,0 +1,116 @@ +package com.shuzijun.leetcode.editor.cn; + +/** + *

    给你一个只包含 '('')' 的字符串,找出最长有效(格式正确且连续)括号子串的长度。

    + * + *

    + * + *
    + *
    + *

    示例 1:

    + * + *
    + * 输入:s = "(()"
    + * 输出:2
    + * 解释:最长有效括号子串是 "()"
    + * 
    + * + *

    示例 2:

    + * + *
    + * 输入:s = ")()())"
    + * 输出:4
    + * 解释:最长有效括号子串是 "()()"
    + * 
    + * + *

    示例 3:

    + * + *
    + * 输入:s = ""
    + * 输出:0
    + * 
    + * + *

    + * + *

    提示:

    + * + *
      + *
    • 0 <= s.length <= 3 * 104
    • + *
    • s[i]'('')'
    • + *
    + *
    + *
    + *
    Related Topics
  • 字符串
  • 动态规划

  • 👍 1391
  • 👎 0
  • + */ + +public class Q32LongestValidParentheses { + public static void main(String[] args) { + Solution solution = new Q32LongestValidParentheses().new Solution(); + System.out.println(solution.longestValidParentheses("()(()")); + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int longestValidParentheses(String s) { + int maxLen = 0; + int len = 0; + int stack = 0; + for (int i = 0; i < s.length(); i++) { + if (s.charAt(i) == '(') { + stack = stack + 1; + } else { + if (stack < 1) { + if (len > maxLen) { + maxLen = len; + } + if (len > maxLen) { + maxLen = len; + } + stack = 0; + len = 0; + } else { + stack = stack - 1; + len = len + 2; + if (len > maxLen && stack == 0) { + maxLen = len; + } + } + } + } + if (len > maxLen && stack == 0) { + maxLen = len; + } + stack = 0; + len = 0; + for (int i = s.length() - 1; i >= 0; i--) { + if (s.charAt(i) == ')') { + stack = stack + 1; + } else { + if (stack < 1) { + if (len > maxLen) { + maxLen = len; + } + if (len > maxLen) { + maxLen = len; + } + stack = 0; + len = 0; + } else { + stack = stack - 1; + len = len + 2; + if (len > maxLen && stack == 0) { + maxLen = len; + } + } + } + } + if (len > maxLen && stack == 0) { + maxLen = len; + } + + return maxLen; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/com/shuzijun/leetcode/editor/cn/Q36ValidSudoku.java b/src/main/java/com/shuzijun/leetcode/editor/cn/Q36ValidSudoku.java new file mode 100644 index 0000000..04962b4 --- /dev/null +++ b/src/main/java/com/shuzijun/leetcode/editor/cn/Q36ValidSudoku.java @@ -0,0 +1,114 @@ +package com.shuzijun.leetcode.editor.cn; + +import java.util.ArrayList; +import java.util.List; + +/** + *

    请你判断一个 9x9 的数独是否有效。只需要 根据以下规则 ,验证已经填入的数字是否有效即可。

    + * + *
      + *
    1. 数字 1-9 在每一行只能出现一次。
    2. + *
    3. 数字 1-9 在每一列只能出现一次。
    4. + *
    5. 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)
    6. + *
    + * + *

    数独部分空格内已填入了数字,空白格用 '.' 表示。

    + * + *

    注意:

    + * + *
      + *
    • 一个有效的数独(部分已被填充)不一定是可解的。
    • + *
    • 只需要根据以上规则,验证已经填入的数字是否有效即可。
    • + *
    + * + *

    + * + *

    示例 1:

    + * + *
    + * 输入:board =
    + * [["5","3",".",".","7",".",".",".","."]
    + * ,["6",".",".","1","9","5",".",".","."]
    + * ,[".","9","8",".",".",".",".","6","."]
    + * ,["8",".",".",".","6",".",".",".","3"]
    + * ,["4",".",".","8",".","3",".",".","1"]
    + * ,["7",".",".",".","2",".",".",".","6"]
    + * ,[".","6",".",".",".",".","2","8","."]
    + * ,[".",".",".","4","1","9",".",".","5"]
    + * ,[".",".",".",".","8",".",".","7","9"]]
    + * 输出:true
    + * 
    + * + *

    示例 2:

    + * + *
    + * 输入:board =
    + * [["8","3",".",".","7",".",".",".","."]
    + * ,["6",".",".","1","9","5",".",".","."]
    + * ,[".","9","8",".",".",".",".","6","."]
    + * ,["8",".",".",".","6",".",".",".","3"]
    + * ,["4",".",".","8",".","3",".",".","1"]
    + * ,["7",".",".",".","2",".",".",".","6"]
    + * ,[".","6",".",".",".",".","2","8","."]
    + * ,[".",".",".","4","1","9",".",".","5"]
    + * ,[".",".",".",".","8",".",".","7","9"]]
    + * 输出:false
    + * 解释:除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。 但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。
    + * + *

    + * + *

    提示:

    + * + *
      + *
    • board.length == 9
    • + *
    • board[i].length == 9
    • + *
    • board[i][j] 是一位数字或者 '.'
    • + *
    + *
    Related Topics
  • 数组
  • 哈希表
  • 矩阵

  • 👍 562
  • 👎 0
  • + */ + +public class Q36ValidSudoku { + public static void main(String[] args) { + Solution solution = new Q36ValidSudoku().new Solution(); + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public boolean isValidSudoku(char[][] board) { + List[] rows = new List[9]; + List[] columns = new List[9]; + List[] boxes = new List[9]; + for (int i = 0; i < 9; i++) { + rows[i] = new ArrayList(); + columns[i] = new ArrayList(); + boxes[i] = new ArrayList(); + } + for (int i = 0; i < board.length; i++) { + char[] row = board[i]; + for (int j = 0; j < row.length; j++) { + char num = board[i][j]; + if (num == '.') { + continue; + } + Integer integer = Integer.valueOf(num); + if (rows[i].contains(integer)) { + return false; + } + rows[i].add(integer); + if (columns[j].contains(integer)) { + return false; + } + columns[j].add(integer); + int boxeInx = (i / 3) * 3 + j / 3; + if (boxes[boxeInx].contains(integer)) { + return false; + } + boxes[boxeInx].add(integer); + } + } + return true; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q1TwoSum.md b/src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q1TwoSum.md index c04dd04..0ba3cdc 100644 --- a/src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q1TwoSum.md +++ b/src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q1TwoSum.md @@ -1,43 +1,127 @@ -

    给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target  的那 两个 整数,并返回它们的数组下标。

    + + + + + + + + -

    你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。

    +## Video Solution -

    你可以按任意顺序返回答案。

    +--- -

     

    +
    -

    示例 1:

    +
      +
    -
    -输入:nums = [2,7,11,15], target = 9
    -输出:[0,1]
    -解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
    -
    +## Solution Article -

    示例 2:

    +--- -
    -输入:nums = [3,2,4], target = 6
    -输出:[1,2]
    -
    +#### Approach 1: Brute Force -

    示例 3:

    +**Intuition** -
    -输入:nums = [3,3], target = 6
    -输出:[0,1]
    -
    +Check all the substring one by one to see if it has no duplicate character. -

     

    +**Algorithm** -

    提示:

    +Suppose we have a function `boolean allUnique(String substring)` which will return true if the characters in the substring are all unique, otherwise false. We can iterate through all the possible substrings of the given string `s` and call the function `allUnique`. If it turns out to be true, then we update our answer of the maximum length of substring without duplicate characters. -
      -
    • 2 <= nums.length <= 104
    • -
    • -109 <= nums[i] <= 109
    • -
    • -109 <= target <= 109
    • -
    • 只会存在一个有效答案
    • -
    +Now let's fill the missing parts: -

    进阶:你可以想出一个时间复杂度小于 O(n2) 的算法吗?

    -
    Related Topics
  • 数组
  • 哈希表

  • 👍 11546
  • 👎 0
  • \ No newline at end of file +1. To enumerate all substrings of a given string, we enumerate the start and end indices of them. Suppose the start and end indices are $$i$$ and $$j$$, respectively. Then we have $$0 \leq i \lt j \leq n$$ (here end index $$j$$ is exclusive by convention). Thus, using two nested loops with $$i$$ from 0 to $$n - 1$$ and $$j$$ from $$i+1$$ to $$n$$, we can enumerate all the substrings of `s`. + +2. To check if one string has duplicate characters, we can use a set. We iterate through all the characters in the string and put them into the `set` one by one. Before putting one character, we check if the set already contains it. If so, we return `false`. After the loop, we return `true`. + + + +**Complexity Analysis** + +* Time complexity : $$O(n^3)$$. + + To verify if characters within index range $$[i, j)$$ are all unique, we need to scan all of them. Thus, it costs $$O(j - i)$$ time. + + For a given `i`, the sum of time costed by each $$j \in [i+1, n]$$ is + + $$ + \sum_{i+1}^{n}O(j - i) + $$ + + Thus, the sum of all the time consumption is: + + $$ + O\left(\sum_{i = 0}^{n - 1}\left(\sum_{j = i + 1}^{n}(j - i)\right)\right) = + O\left(\sum_{i = 0}^{n - 1}\frac{(1 + n - i)(n - i)}{2}\right) = + O(n^3) + $$ + +* Space complexity : $$O(min(n, m))$$. We need $$O(k)$$ space for checking a substring has no duplicate characters, where $$k$$ is the size of the `Set`. The size of the Set is upper bounded by the size of the string $$n$$ and the size of the charset/alphabet $$m$$. +
    +
    +--- +#### Approach 2: Sliding Window + +**Algorithm** + +The naive approach is very straightforward. But it is too slow. So how can we optimize it? + +In the naive approaches, we repeatedly check a substring to see if it has duplicate character. But it is unnecessary. If a substring $$s_{ij}$$ from index $$i$$ to $$j - 1$$ is already checked to have no duplicate characters. We only need to check if $$s[j]$$ is already in the substring $$s_{ij}$$. + +To check if a character is already in the substring, we can scan the substring, which leads to an $$O(n^2)$$ algorithm. But we can do better. + +By using HashSet as a sliding window, checking if a character in the current can be done in $$O(1)$$. + +A sliding window is an abstract concept commonly used in array/string problems. A window is a range of elements in the array/string which usually defined by the start and end indices, i.e. $$[i, j)$$ (left-closed, right-open). A sliding window is a window "slides" its two boundaries to the certain direction. For example, if we slide $$[i, j)$$ to the right by $$1$$ element, then it becomes $$[i+1, j+1)$$ (left-closed, right-open). + +Back to our problem. We use HashSet to store the characters in current window $$[i, j)$$ ($$j = i$$ initially). Then we slide the index $$j$$ to the right. If it is not in the HashSet, we slide $$j$$ further. Doing so until s[j] is already in the HashSet. At this point, we found the maximum size of substrings without duplicate characters start with index $$i$$. If we do this for all $$i$$, we get our answer. + + + +**Complexity Analysis** + +* Time complexity : $$O(2n) = O(n)$$. In the worst case each character will be visited twice by $$i$$ and $$j$$. + +* Space complexity : $$O(min(m, n))$$. Same as the previous approach. We need $$O(k)$$ space for the sliding window, where $$k$$ is the size of the `Set`. The size of the Set is upper bounded by the size of the string $$n$$ and the size of the charset/alphabet $$m$$. +
    +
    +--- +#### Approach 3: Sliding Window Optimized + +The above solution requires at most 2n steps. In fact, it could be optimized to require only n steps. Instead of using a set to tell if a character exists or not, we could define a mapping of the characters to its index. Then we can skip the characters immediately when we found a repeated character. + +The reason is that if $$s[j]$$ have a duplicate in the range $$[i, j)$$ with index $$j'$$, we don't need to increase $$i$$ little by little. We can skip all the elements in the range $$[i, j']$$ and let $$i$$ to be $$j' + 1$$ directly. + +**Java (Using HashMap)** + + + +Here is a visualization of the above code. + +
    + +
    + +**Java (Assuming ASCII 128)** + +The previous implements all have no assumption on the charset of the string `s`. + +If we know that the charset is rather small, we can replace the `Map` with an integer array as direct access table. + +Commonly used tables are: + +* `int[26]` for Letters 'a' - 'z' or 'A' - 'Z' +* `int[128]` for ASCII +* `int[256]` for Extended ASCII + + + +**Complexity Analysis** + +* Time complexity : $$O(n)$$. Index $$j$$ will iterate $$n$$ times. + +* Space complexity (HashMap) : $$O(min(m, n))$$. Same as the previous approach. + +* Space complexity (Table): $$O(m)$$. $$m$$ is the size of the charset. \ No newline at end of file diff --git a/src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q29DivideTwoIntegers.md b/src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q29DivideTwoIntegers.md new file mode 100644 index 0000000..6869dfd --- /dev/null +++ b/src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q29DivideTwoIntegers.md @@ -0,0 +1,30 @@ +

    给定两个整数,被除数 dividend 和除数 divisor。将两数相除,要求不使用乘法、除法和 mod 运算符。

    + +

    返回被除数 dividend 除以除数 divisor 得到的商。

    + +

    整数除法的结果应当截去(truncate)其小数部分,例如:truncate(8.345) = 8 以及 truncate(-2.7335) = -2

    + +

     

    + +

    示例 1:

    + +
    输入: dividend = 10, divisor = 3
    +输出: 3
    +解释: 10/3 = truncate(3.33333..) = truncate(3) = 3
    + +

    示例 2:

    + +
    输入: dividend = 7, divisor = -3
    +输出: -2
    +解释: 7/-3 = truncate(-2.33333..) = -2
    + +

     

    + +

    提示:

    + +
      +
    • 被除数和除数均为 32 位有符号整数。
    • +
    • 除数不为 0。
    • +
    • 假设我们的环境只能存储 32 位有符号整数,其数值范围是 [−231,  231 − 1]。本题中,如果除法结果溢出,则返回 231 − 1。
    • +
    +
    Related Topics
  • 位运算
  • 数学

  • 👍 625
  • 👎 0
  • \ No newline at end of file diff --git a/src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q30SubstringWithConcatenationOfAllWords.md b/src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q30SubstringWithConcatenationOfAllWords.md new file mode 100644 index 0000000..ee9fb15 --- /dev/null +++ b/src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q30SubstringWithConcatenationOfAllWords.md @@ -0,0 +1,42 @@ +

    给定一个字符串 s 和一些 长度相同 的单词 words找出 s 中恰好可以由 words 中所有单词串联形成的子串的起始位置。

    + +

    注意子串要与 words 中的单词完全匹配,中间不能有其他字符 ,但不需要考虑 words 中单词串联的顺序。

    + +

     

    + +

    示例 1:

    + +
    +输入:s = "barfoothefoobarman", words = ["foo","bar"]
    +输出:[0,9]
    +解释:
    +从索引 0 和 9 开始的子串分别是 "barfoo" 和 "foobar" 。
    +输出的顺序不重要, [9,0] 也是有效答案。
    +
    + +

    示例 2:

    + +
    +输入:s = "wordgoodgoodgoodbestword", words = ["word","good","best","word"]
    +输出:[]
    +
    + +

    示例 3:

    + +
    +输入:s = "barfoofoobarthefoobarman", words = ["bar","foo","the"]
    +输出:[6,9,12]
    +
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= s.length <= 104
    • +
    • s 由小写英文字母组成
    • +
    • 1 <= words.length <= 5000
    • +
    • 1 <= words[i].length <= 30
    • +
    • words[i] 由小写英文字母组成
    • +
    +
    Related Topics
  • 哈希表
  • 字符串
  • 滑动窗口

  • 👍 510
  • 👎 0
  • \ No newline at end of file diff --git a/src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q31NextPermutation.md b/src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q31NextPermutation.md new file mode 100644 index 0000000..1c32e35 --- /dev/null +++ b/src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q31NextPermutation.md @@ -0,0 +1,45 @@ +

    实现获取 下一个排列 的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列。

    + +

    如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列)。

    + +

    必须 原地 修改,只允许使用额外常数空间。

    + +

     

    + +

    示例 1:

    + +
    +输入:nums = [1,2,3]
    +输出:[1,3,2]
    +
    + +

    示例 2:

    + +
    +输入:nums = [3,2,1]
    +输出:[1,2,3]
    +
    + +

    示例 3:

    + +
    +输入:nums = [1,1,5]
    +输出:[1,5,1]
    +
    + +

    示例 4:

    + +
    +输入:nums = [1]
    +输出:[1]
    +
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= nums.length <= 100
    • +
    • 0 <= nums[i] <= 100
    • +
    +
    Related Topics
  • 数组
  • 双指针

  • 👍 1247
  • 👎 0
  • \ No newline at end of file diff --git a/src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q32LongestValidParentheses.md b/src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q32LongestValidParentheses.md new file mode 100644 index 0000000..db3b83d --- /dev/null +++ b/src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q32LongestValidParentheses.md @@ -0,0 +1,40 @@ +

    给你一个只包含 '(' 和 ')' 的字符串,找出最长有效(格式正确且连续)括号子串的长度。

    + +

     

    + +
    +
    +

    示例 1:

    + +
    +输入:s = "(()"
    +输出:2
    +解释:最长有效括号子串是 "()"
    +
    + +

    示例 2:

    + +
    +输入:s = ")()())"
    +输出:4
    +解释:最长有效括号子串是 "()()"
    +
    + +

    示例 3:

    + +
    +输入:s = ""
    +输出:0
    +
    + +

     

    + +

    提示:

    + +
      +
    • 0 <= s.length <= 3 * 104
    • +
    • s[i]'('')'
    • +
    +
    +
    +
    Related Topics
  • 字符串
  • 动态规划

  • 👍 1391
  • 👎 0
  • \ No newline at end of file diff --git a/src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q36ValidSudoku.md b/src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q36ValidSudoku.md new file mode 100644 index 0000000..e13448d --- /dev/null +++ b/src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q36ValidSudoku.md @@ -0,0 +1,61 @@ +

    请你判断一个 9x9 的数独是否有效。只需要 根据以下规则 ,验证已经填入的数字是否有效即可。

    + +
      +
    1. 数字 1-9 在每一行只能出现一次。
    2. +
    3. 数字 1-9 在每一列只能出现一次。
    4. +
    5. 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)
    6. +
    + +

    数独部分空格内已填入了数字,空白格用 '.' 表示。

    + +

    注意:

    + +
      +
    • 一个有效的数独(部分已被填充)不一定是可解的。
    • +
    • 只需要根据以上规则,验证已经填入的数字是否有效即可。
    • +
    + +

     

    + +

    示例 1:

    + +
    +输入:board = 
    +[["5","3",".",".","7",".",".",".","."]
    +,["6",".",".","1","9","5",".",".","."]
    +,[".","9","8",".",".",".",".","6","."]
    +,["8",".",".",".","6",".",".",".","3"]
    +,["4",".",".","8",".","3",".",".","1"]
    +,["7",".",".",".","2",".",".",".","6"]
    +,[".","6",".",".",".",".","2","8","."]
    +,[".",".",".","4","1","9",".",".","5"]
    +,[".",".",".",".","8",".",".","7","9"]]
    +输出:true
    +
    + +

    示例 2:

    + +
    +输入:board = 
    +[["8","3",".",".","7",".",".",".","."]
    +,["6",".",".","1","9","5",".",".","."]
    +,[".","9","8",".",".",".",".","6","."]
    +,["8",".",".",".","6",".",".",".","3"]
    +,["4",".",".","8",".","3",".",".","1"]
    +,["7",".",".",".","2",".",".",".","6"]
    +,[".","6",".",".",".",".","2","8","."]
    +,[".",".",".","4","1","9",".",".","5"]
    +,[".",".",".",".","8",".",".","7","9"]]
    +输出:false
    +解释:除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。 但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。
    + +

     

    + +

    提示:

    + +
      +
    • board.length == 9
    • +
    • board[i].length == 9
    • +
    • board[i][j] 是一位数字或者 '.'
    • +
    +
    Related Topics
  • 数组
  • 哈希表
  • 矩阵

  • 👍 562
  • 👎 0
  • \ No newline at end of file From 129aad98261b11068692fc6e0dbceb32d330fea6 Mon Sep 17 00:00:00 2001 From: shuzijun Date: Thu, 9 Jun 2022 12:07:06 +0800 Subject: [PATCH 03/11] add Co-Authored-By: shuzijun --- .../editor/cn/Q43MultiplyStrings.java | 86 +++++++++++++++++++ .../cn/doc/content/Q43MultiplyStrings.md | 29 +++++++ 2 files changed, 115 insertions(+) create mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/Q43MultiplyStrings.java create mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q43MultiplyStrings.md diff --git a/src/main/java/com/shuzijun/leetcode/editor/cn/Q43MultiplyStrings.java b/src/main/java/com/shuzijun/leetcode/editor/cn/Q43MultiplyStrings.java new file mode 100644 index 0000000..2a50bd4 --- /dev/null +++ b/src/main/java/com/shuzijun/leetcode/editor/cn/Q43MultiplyStrings.java @@ -0,0 +1,86 @@ +package com.shuzijun.leetcode.editor.cn; + +/** + *

    给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式。

    + * + *

    注意:不能使用任何内置的 BigInteger 库或直接将输入转换为整数。

    + * + *

     

    + * + *

    示例 1:

    + * + *
    + * 输入: num1 = "2", num2 = "3"
    + * 输出: "6"
    + * + *

    示例 2:

    + * + *
    + * 输入: num1 = "123", num2 = "456"
    + * 输出: "56088"
    + * + *

     

    + * + *

    提示:

    + * + *
      + *
    • 1 <= num1.length, num2.length <= 200
    • + *
    • num1 和 num2 只能由数字组成。
    • + *
    • num1 和 num2 都不包含任何前导零,除了数字0本身。
    • + *
    + * + *
    Related Topics
  • 数学
  • 字符串
  • 模拟

  • 👍 950
  • 👎 0
  • + */ +public class Q43MultiplyStrings { + public static void main(String[] args) { + Solution solution = new Q43MultiplyStrings().new Solution(); + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public String multiply(String num1, String num2) { + if (num1.equals("0") || num2.equals("0")) { + return "0"; + } + String ans = "0"; + int m = num1.length(), n = num2.length(); + for (int i = n - 1; i >= 0; i--) { + StringBuffer curr = new StringBuffer(); + int add = 0; + for (int j = n - 1; j > i; j--) { + curr.append(0); + } + int y = num2.charAt(i) - '0'; + for (int j = m - 1; j >= 0; j--) { + int x = num1.charAt(j) - '0'; + int product = x * y + add; + curr.append(product % 10); + add = product / 10; + } + if (add != 0) { + curr.append(add % 10); + } + ans = add(ans, curr.reverse().toString()); + } + return ans; + } + + public String add(String num1, String num2) { + int l1 = num1.length() - 1, l2 = num2.length() - 1, add = 0; + StringBuilder ans = new StringBuilder(); + while (l1 >= 0 || l2 >= 0 || add != 0) { + int x = l1 >= 0 ? num1.charAt(l1) - '0' : 0; + int y = l2 >= 0 ? num2.charAt(l2) - '0' : 0; + int result = x + y + add; + ans.append(result % 10); + add = result / 10; + l1--; + l2--; + } + ans.reverse(); + return ans.toString(); + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q43MultiplyStrings.md b/src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q43MultiplyStrings.md new file mode 100644 index 0000000..875bfe9 --- /dev/null +++ b/src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q43MultiplyStrings.md @@ -0,0 +1,29 @@ +

    给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式。

    + +

    注意:不能使用任何内置的 BigInteger 库或直接将输入转换为整数。

    + +

     

    + +

    示例 1:

    + +
    +输入: num1 = "2", num2 = "3"
    +输出: "6"
    + +

    示例 2:

    + +
    +输入: num1 = "123", num2 = "456"
    +输出: "56088"
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= num1.length, num2.length <= 200
    • +
    • num1 和 num2 只能由数字组成。
    • +
    • num1 和 num2 都不包含任何前导零,除了数字0本身。
    • +
    + +
    Related Topics
  • 数学
  • 字符串
  • 模拟

  • 👍 950
  • 👎 0
  • \ No newline at end of file From f463e7bfda34c11a33f6271937688460c4d77cc5 Mon Sep 17 00:00:00 2001 From: shuzijun Date: Sun, 9 Oct 2022 14:10:00 +0800 Subject: [PATCH 04/11] golang --- .github/workflows/badge.yml | 23 +++ .gitignore | 5 +- .idea/leetcode-pro/editor.xml | 134 ++++-------------- README.md | 71 ++++++---- go.mod | 4 + pom.xml | 12 -- src/leetcode/editor/cn/Q1TwoSum_test.go | 71 ++++++++++ .../editor/cn/Q2AddTwoNumbers_test.go | 96 +++++++++++++ .../editor/cn/Q856ScoreOfParentheses_test.go | 81 +++++++++++ .../editor/cn/Q870AdvantageShuffle_test.go | 73 ++++++++++ src/leetcode/editor/cn/common.go | 18 +++ .../com/shuzijun/leetcode/HelloWorld.java | 15 -- .../java/com/shuzijun/leetcode/ListNode.java | 22 --- .../Q153FindMinimumInRotatedSortedArray.java | 79 ----------- .../shuzijun/leetcode/editor/cn/Q1TwoSum.java | 76 ---------- .../editor/cn/Q29DivideTwoIntegers.java | 47 ------ ...0SubstringWithConcatenationOfAllWords.java | 62 -------- .../editor/cn/Q31NextPermutation.java | 97 ------------- .../editor/cn/Q32LongestValidParentheses.java | 116 --------------- .../cn/Q33SearchInRotatedSortedArray.java | 85 ----------- ...AndLastPositionOfElementInSortedArray.java | 104 -------------- .../editor/cn/Q35SearchInsertPosition.java | 73 ---------- .../leetcode/editor/cn/Q36ValidSudoku.java | 114 --------------- .../editor/cn/Q43MultiplyStrings.java | 86 ----------- .../shuzijun/leetcode/editor/cn/Q69Sqrtx.java | 50 ------- .../editor/cn/Q74SearchA2dMatrix.java | 64 --------- .../shuzijun/leetcode/editor/cn/doc/all.json | 1 - .../Q153FindMinimumInRotatedSortedArray.md | 48 ------- .../editor/cn/doc/content/Q1TwoSum.md | 127 ----------------- .../cn/doc/content/Q29DivideTwoIntegers.md | 30 ---- ...Q30SubstringWithConcatenationOfAllWords.md | 42 ------ .../cn/doc/content/Q31NextPermutation.md | 45 ------ .../doc/content/Q32LongestValidParentheses.md | 40 ------ .../content/Q33SearchInRotatedSortedArray.md | 44 ------ ...stAndLastPositionOfElementInSortedArray.md | 41 ------ .../cn/doc/content/Q35SearchInsertPosition.md | 28 ---- .../editor/cn/doc/content/Q36ValidSudoku.md | 61 -------- .../cn/doc/content/Q43MultiplyStrings.md | 29 ---- .../editor/cn/doc/content/Q69Sqrtx.md | 20 --- .../cn/doc/content/Q74SearchA2dMatrix.md | 34 ----- .../leetcode/editor/cn/doc/translation.json | 1 - .../leetcode/editor/en/AddTwoNumbers.java | 68 --------- .../editor/en/ContainerWithMostWater.java | 51 ------- .../editor/en/GenerateParentheses.java | 53 ------- .../leetcode/editor/en/PalindromeNumber.java | 64 --------- .../leetcode/editor/en/ReverseInteger.java | 52 ------- .../editor/en/SearchInRotatedSortedArray.java | 67 --------- .../editor/en/StringToIntegerAtoi.java | 105 -------------- .../leetcode/editor/en/SwapNodesInPairs.java | 53 ------- .../shuzijun/leetcode/editor/en/ThreeSum.java | 81 ----------- .../leetcode/editor/en/ThreeSumClosest.java | 57 -------- .../shuzijun/leetcode/editor/en/TwoSum.java | 45 ------ .../leetcode/editor/en/ZigzagConversion.java | 76 ---------- 53 files changed, 440 insertions(+), 2601 deletions(-) create mode 100644 .github/workflows/badge.yml create mode 100644 go.mod delete mode 100644 pom.xml create mode 100644 src/leetcode/editor/cn/Q1TwoSum_test.go create mode 100644 src/leetcode/editor/cn/Q2AddTwoNumbers_test.go create mode 100644 src/leetcode/editor/cn/Q856ScoreOfParentheses_test.go create mode 100644 src/leetcode/editor/cn/Q870AdvantageShuffle_test.go create mode 100644 src/leetcode/editor/cn/common.go delete mode 100644 src/main/java/com/shuzijun/leetcode/HelloWorld.java delete mode 100644 src/main/java/com/shuzijun/leetcode/ListNode.java delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/Q153FindMinimumInRotatedSortedArray.java delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/Q1TwoSum.java delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/Q29DivideTwoIntegers.java delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/Q30SubstringWithConcatenationOfAllWords.java delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/Q31NextPermutation.java delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/Q32LongestValidParentheses.java delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/Q33SearchInRotatedSortedArray.java delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/Q34FindFirstAndLastPositionOfElementInSortedArray.java delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/Q35SearchInsertPosition.java delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/Q36ValidSudoku.java delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/Q43MultiplyStrings.java delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/Q69Sqrtx.java delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/Q74SearchA2dMatrix.java delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/doc/all.json delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q153FindMinimumInRotatedSortedArray.md delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q1TwoSum.md delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q29DivideTwoIntegers.md delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q30SubstringWithConcatenationOfAllWords.md delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q31NextPermutation.md delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q32LongestValidParentheses.md delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q33SearchInRotatedSortedArray.md delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q34FindFirstAndLastPositionOfElementInSortedArray.md delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q35SearchInsertPosition.md delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q36ValidSudoku.md delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q43MultiplyStrings.md delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q69Sqrtx.md delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q74SearchA2dMatrix.md delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/cn/doc/translation.json delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/en/AddTwoNumbers.java delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/en/ContainerWithMostWater.java delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/en/GenerateParentheses.java delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/en/PalindromeNumber.java delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/en/ReverseInteger.java delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/en/SearchInRotatedSortedArray.java delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/en/StringToIntegerAtoi.java delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/en/SwapNodesInPairs.java delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/en/ThreeSum.java delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/en/ThreeSumClosest.java delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/en/TwoSum.java delete mode 100644 src/main/java/com/shuzijun/leetcode/editor/en/ZigzagConversion.java diff --git a/.github/workflows/badge.yml b/.github/workflows/badge.yml new file mode 100644 index 0000000..fe6481e --- /dev/null +++ b/.github/workflows/badge.yml @@ -0,0 +1,23 @@ +# This is a basic workflow to help you get started with Actions + +name: badge + +on: + push: + branches: + - golang + workflow_dispatch: + +jobs: + + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + + - uses: shuzijun/leetcode-editor-action@v0.1.1 + with: + STATISTICS_DIRECTORY: .idea/leetcode-pro/ + LEETCODE_SITE: leetcode.cn + diff --git a/.gitignore b/.gitignore index fc21647..dd35a47 100644 --- a/.gitignore +++ b/.gitignore @@ -22,5 +22,6 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* -src/main/java/com/shuzijun/leetcode/editor/cn/doc/solution/*.* -src/main/java/com/shuzijun/leetcode/editor/cn/doc/submission/*.* +.idea +src/leetcode/editor/cn/doc/*.* +src/leetcode/editor/cn/doc/*/*.* diff --git a/.idea/leetcode-pro/editor.xml b/.idea/leetcode-pro/editor.xml index 951080a..d87f759 100644 --- a/.idea/leetcode-pro/editor.xml +++ b/.idea/leetcode-pro/editor.xml @@ -3,129 +3,51 @@
    + + + + + + diff --git a/.idea/leetcode-pro/statistics.xml b/.idea/leetcode-pro/statistics.xml index 848ba63..75ecd99 100644 --- a/.idea/leetcode-pro/statistics.xml +++ b/.idea/leetcode-pro/statistics.xml @@ -7,10 +7,10 @@ diff --git a/src/leetcode/editor/cn/Q801MinimumSwapsToMakeSequencesIncreasing_test.go b/src/leetcode/editor/cn/Q801MinimumSwapsToMakeSequencesIncreasing_test.go new file mode 100644 index 0000000..24deab9 --- /dev/null +++ b/src/leetcode/editor/cn/Q801MinimumSwapsToMakeSequencesIncreasing_test.go @@ -0,0 +1,91 @@ +package main + +import "testing" + +/** +

    我们有两个长度相等且不为空的整型数组 nums1 和 nums2 。在一次操作中,我们可以交换 nums1[i] 和 nums2[i]的元素。

    + + + +

    返回 使 nums1nums2 严格递增 所需操作的最小次数

    + +

    数组 arr 严格递增 且  arr[0] < arr[1] < arr[2] < ... < arr[arr.length - 1] 。

    + +

    注意:

    + + + +

     

    + +

    示例 1:

    + +
    +输入: nums1 = [1,3,5,4], nums2 = [1,2,3,7]
    +输出: 1
    +解释: 
    +交换 A[3] 和 B[3] 后,两个数组如下:
    +A = [1, 3, 5, 7] , B = [1, 2, 3, 4]
    +两个数组均为严格递增的。
    + +

    示例 2:

    + +
    +输入: nums1 = [0,3,5,8,9], nums2 = [2,1,4,6,9]
    +输出: 1
    +
    + +

     

    + +

    提示:

    + + + +
    Related Topics
  • 数组
  • 动态规划
  • +
  • 👍 371
  • 👎 0
  • +*/ + +func TestMinSwap(t *testing.T) { + t.Log(minSwap([]int{1, 3, 5, 4}, []int{1, 2, 3, 7})) + t.Log(minSwap([]int{0, 3, 5, 8, 9}, []int{2, 1, 4, 6, 9})) +} + +//leetcode submit region begin(Prohibit modification and deletion) +func minSwap(nums1 []int, nums2 []int) int { + dp := make([][2]int, len(nums1)) + dp[0][0] = 0 + dp[0][1] = 1 + for i := 1; i < len(nums1); i++ { + a1 := nums1[i-1] + a2 := nums1[i] + b1 := nums2[i-1] + b2 := nums2[i] + + if (a1 < a2 && b1 < b2) && (a1 < b2 && b1 < a2) { + dp[i][0] = min(dp[i-1][0], dp[i-1][1]) + dp[i][1] = min(dp[i-1][0], dp[i-1][1]) + 1 + } else if a1 < a2 && b1 < b2 { + dp[i][0] = dp[i-1][0] + dp[i][1] = dp[i-1][1] + 1 + } else { + dp[i][0] = dp[i-1][1] + dp[i][1] = dp[i-1][0] + 1 + } + } + return min(dp[len(nums1)-1][0], dp[len(nums1)-1][1]) +} +func min(a, b int) int { + if a > b { + return b + } + return a +} + +//leetcode submit region end(Prohibit modification and deletion) From 8bb7993203278cfc35b3fb57bcd688bcfeb19f49 Mon Sep 17 00:00:00 2001 From: leetcode-editor-bot <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon, 10 Oct 2022 11:42:22 +0000 Subject: [PATCH 08/11] Update progress badge --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c3860e0..a74a492 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ ## Progress -![Progress](https://img.shields.io/static/v1?logo=leetcode&label=Progress&message=50%2F2812&color=brightgreen) ![Easy](https://img.shields.io/static/v1?logo=leetcode&label=Easy&message=16&color=5CB85C) ![Medium](https://img.shields.io/static/v1?logo=leetcode&label=Medium&message=30&color=F0AD4E) ![Hard](https://img.shields.io/static/v1?logo=leetcode&label=Hard&message=4&color=D9534F) +![Progress](https://img.shields.io/static/v1?logo=leetcode&label=Progress&message=51%2F2812&color=brightgreen) ![Easy](https://img.shields.io/static/v1?logo=leetcode&label=Easy&message=16&color=5CB85C) ![Medium](https://img.shields.io/static/v1?logo=leetcode&label=Medium&message=30&color=F0AD4E) ![Hard](https://img.shields.io/static/v1?logo=leetcode&label=Hard&message=5&color=D9534F) ## leetcode-editor config From 00fbf3831c346cc9b4e30e29a2d8c8cc50adf0c1 Mon Sep 17 00:00:00 2001 From: shuzijun Date: Tue, 11 Oct 2022 19:23:25 +0800 Subject: [PATCH 09/11] 1790 --- .github/workflows/badge.yml | 1 + .idea/leetcode-pro/editor.xml | 14 +++- ...IfOneStringSwapCanMakeStringsEqual_test.go | 75 +++++++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 src/leetcode/editor/cn/Q1790CheckIfOneStringSwapCanMakeStringsEqual_test.go diff --git a/.github/workflows/badge.yml b/.github/workflows/badge.yml index fe6481e..7156cd0 100644 --- a/.github/workflows/badge.yml +++ b/.github/workflows/badge.yml @@ -16,6 +16,7 @@ jobs: steps: - uses: actions/checkout@v3 + - uses: shuzijun/leetcode-editor-action@v0.1.1 with: STATISTICS_DIRECTORY: .idea/leetcode-pro/ diff --git a/.idea/leetcode-pro/editor.xml b/.idea/leetcode-pro/editor.xml index 50318ff..7cf49be 100644 --- a/.idea/leetcode-pro/editor.xml +++ b/.idea/leetcode-pro/editor.xml @@ -3,6 +3,18 @@ - \ No newline at end of file + diff --git a/src/leetcode/editor/cn/Q1790CheckIfOneStringSwapCanMakeStringsEqual_test.go b/src/leetcode/editor/cn/Q1790CheckIfOneStringSwapCanMakeStringsEqual_test.go new file mode 100644 index 0000000..bed5317 --- /dev/null +++ b/src/leetcode/editor/cn/Q1790CheckIfOneStringSwapCanMakeStringsEqual_test.go @@ -0,0 +1,75 @@ +package main + +import "testing" + +/** +

    给你长度相等的两个字符串 s1s2 。一次 字符串交换 操作的步骤如下:选出某个字符串中的两个下标(不必不同),并交换这两个下标所对应的字符。

    + +

    如果对 其中一个字符串 执行 最多一次字符串交换 就可以使两个字符串相等,返回 true ;否则,返回 false

    + +

     

    + +

    示例 1:

    + +
    输入:s1 = "bank", s2 = "kanb"
    +输出:true
    +解释:例如,交换 s2 中的第一个和最后一个字符可以得到 "bank"
    +
    + +

    示例 2:

    + +
    输入:s1 = "attack", s2 = "defend"
    +输出:false
    +解释:一次字符串交换无法使两个字符串相等
    +
    + +

    示例 3:

    + +
    输入:s1 = "kelb", s2 = "kelb"
    +输出:true
    +解释:两个字符串已经相等,所以不需要进行字符串交换
    +
    + +

    示例 4:

    + +
    输入:s1 = "abcd", s2 = "dcba"
    +输出:false
    +
    + +

     

    + +

    提示:

    + +
      +
    • 1 <= s1.length, s2.length <= 100
    • +
    • s1.length == s2.length
    • +
    • s1s2 仅由小写英文字母组成
    • +
    + +
    Related Topics
  • 哈希表
  • 字符串
  • 计数
  • +
  • 👍 78
  • 👎 0
  • +*/ + +func TestAreAlmostEqual(t *testing.T) { + t.Log(areAlmostEqual("bank", "kanb")) +} + +//leetcode submit region begin(Prohibit modification and deletion) +func areAlmostEqual(s1 string, s2 string) bool { + i, j := -1, -1 + for k := 0; k < len(s1); k++ { + if s1[k] != s2[k] { + if i == -1 { + i = k + } else if j == -1 { + j = k + } else { + return false + } + } + } + + return i == -1 || j != -1 && s1[i] == s2[j] && s1[j] == s2[i] +} + +//leetcode submit region end(Prohibit modification and deletion) From cffc2de94cc8155acd2653a03f13f9d1149cff88 Mon Sep 17 00:00:00 2001 From: shuzijun Date: Wed, 12 Oct 2022 20:12:34 +0800 Subject: [PATCH 10/11] 817 --- .idea/leetcode-pro/editor.xml | 14 +++- .idea/leetcode-pro/statistics.xml | 8 +- .../cn/Q817LinkedListComponents_test.go | 78 +++++++++++++++++++ 3 files changed, 95 insertions(+), 5 deletions(-) create mode 100644 src/leetcode/editor/cn/Q817LinkedListComponents_test.go diff --git a/.idea/leetcode-pro/editor.xml b/.idea/leetcode-pro/editor.xml index 7cf49be..183528e 100644 --- a/.idea/leetcode-pro/editor.xml +++ b/.idea/leetcode-pro/editor.xml @@ -51,6 +51,18 @@ + + + + + + @@ -78,4 +90,4 @@ - + \ No newline at end of file diff --git a/.idea/leetcode-pro/statistics.xml b/.idea/leetcode-pro/statistics.xml index 75ecd99..eb9f9b0 100644 --- a/.idea/leetcode-pro/statistics.xml +++ b/.idea/leetcode-pro/statistics.xml @@ -6,11 +6,11 @@ - diff --git a/src/leetcode/editor/cn/Q817LinkedListComponents_test.go b/src/leetcode/editor/cn/Q817LinkedListComponents_test.go new file mode 100644 index 0000000..e3b9058 --- /dev/null +++ b/src/leetcode/editor/cn/Q817LinkedListComponents_test.go @@ -0,0 +1,78 @@ +package main + +import "testing" + +/** +

    给定链表头结点 head,该链表上的每个结点都有一个 唯一的整型值 。同时给定列表 nums,该列表是上述链表中整型值的一个子集。

    + +

    返回列表 nums 中组件的个数,这里对组件的定义为:链表中一段最长连续结点的值(该值必须在列表 nums 中)构成的集合。

    + +

     

    + +

    示例 1:

    + +

    + +
    +输入: head = [0,1,2,3], nums = [0,1,3]
    +输出: 2
    +解释: 链表中,0 和 1 是相连接的,且 nums 中不包含 2,所以 [0, 1] 是 nums 的一个组件,同理 [3] 也是一个组件,故返回 2。
    + +

    示例 2:

    + +

     

    + +
    +输入: head = [0,1,2,3,4], nums = [0,3,1,4]
    +输出: 2
    +解释: 链表中,0 和 1 是相连接的,3 和 4 是相连接的,所以 [0, 1] 和 [3, 4] 是两个组件,故返回 2。
    + +

     

    + +

    提示:

    + +
      +
    • 链表中节点数为n
    • +
    • 1 <= n <= 104
    • +
    • 0 <= Node.val < n
    • +
    • Node.val 中所有值 不同
    • +
    • 1 <= nums.length <= n
    • +
    • 0 <= nums[i] < n
    • +
    • nums 中所有值 不同
    • +
    + +
    Related Topics
  • 数组
  • 哈希表
  • 链表
  • +
  • 👍 167
  • 👎 0
  • +*/ + +func TestNumComponents(t *testing.T) { + t.Log(numComponents(&ListNode{Val: 0, Next: &ListNode{Val: 1, Next: &ListNode{Val: 2, Next: &ListNode{Val: 3}}}}, []int{0, 1, 3})) +} + +//leetcode submit region begin(Prohibit modification and deletion) +/** + * Definition for singly-linked list. + * type ListNode struct { + * Val int + * Next *ListNode + * } + */ +func numComponents(head *ListNode, nums []int) int { + ans := 0 + set := make(map[int]bool) + for _, num := range nums { + set[num] = true + } + for isSet := false; head != nil; head = head.Next { + if _, ok := set[head.Val]; !ok { + isSet = false + } else if !isSet { + isSet = true + ans++ + } + + } + return ans +} + +//leetcode submit region end(Prohibit modification and deletion) From ffb1f4dccef14256683cc2f3f3a9ff33a850b931 Mon Sep 17 00:00:00 2001 From: leetcode-editor-bot <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 12 Oct 2022 12:12:50 +0000 Subject: [PATCH 11/11] Update progress badge --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a74a492..9279211 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ ## Progress -![Progress](https://img.shields.io/static/v1?logo=leetcode&label=Progress&message=51%2F2812&color=brightgreen) ![Easy](https://img.shields.io/static/v1?logo=leetcode&label=Easy&message=16&color=5CB85C) ![Medium](https://img.shields.io/static/v1?logo=leetcode&label=Medium&message=30&color=F0AD4E) ![Hard](https://img.shields.io/static/v1?logo=leetcode&label=Hard&message=5&color=D9534F) +![Progress](https://img.shields.io/static/v1?logo=leetcode&label=Progress&message=53%2F2813&color=brightgreen) ![Easy](https://img.shields.io/static/v1?logo=leetcode&label=Easy&message=17&color=5CB85C) ![Medium](https://img.shields.io/static/v1?logo=leetcode&label=Medium&message=31&color=F0AD4E) ![Hard](https://img.shields.io/static/v1?logo=leetcode&label=Hard&message=5&color=D9534F) ## leetcode-editor config