From 48daf27020dcea29e5ab23e54b04950c0ea217b3 Mon Sep 17 00:00:00 2001 From: shuzijun Date: Mon, 12 Jul 2021 11:04:00 +0800 Subject: [PATCH 1/3] 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 2/3] 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 3/3] 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