diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..793630e --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/leetcode/editor.xml b/.idea/leetcode/editor.xml new file mode 100644 index 0000000..b65b556 --- /dev/null +++ b/.idea/leetcode/editor.xml @@ -0,0 +1,64 @@ + + + + + + \ No newline at end of file diff --git a/.idea/leetcode/statistics.xml b/.idea/leetcode/statistics.xml new file mode 100644 index 0000000..a76a4a7 --- /dev/null +++ b/.idea/leetcode/statistics.xml @@ -0,0 +1,20 @@ + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..b784bbc --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..f394444 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,121 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + { + "keyToString": { + "ASKED_ADD_EXTERNAL_FILES": "true", + "RunOnceActivity.ShowReadmeOnStart": "true", + "last_opened_file_path": "/Users/xhlt/Documents/GitHub/leetcode-question", + "node.js.detected.package.eslint": "true", + "node.js.detected.package.tslint": "true", + "node.js.selected.package.eslint": "(autodetect)", + "node.js.selected.package.tslint": "(autodetect)", + "nodejs_package_manager_path": "npm", + "project.structure.last.edited": "Modules", + "project.structure.proportion": "0.15", + "project.structure.side.proportion": "0.2", + "settings.editor.selected.configurable": "leetcode.id", + "vue.rearranger.settings.migration": "true" + } +} + + + + + + + + + + + + + + 1680138252320 + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index e19b6e5..0210fad 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,18 @@ com.shuzijun leetcode-question 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 7 + 7 + + + + \ No newline at end of file diff --git a/src/main/java/com/shuzijun/leetcode/HelloWorld.java b/src/main/java/com/ljz/leetcode/HelloWorld.java similarity index 90% rename from src/main/java/com/shuzijun/leetcode/HelloWorld.java rename to src/main/java/com/ljz/leetcode/HelloWorld.java index 2383b20..7828b9e 100644 --- a/src/main/java/com/shuzijun/leetcode/HelloWorld.java +++ b/src/main/java/com/ljz/leetcode/HelloWorld.java @@ -1,4 +1,4 @@ -package com.shuzijun.leetcode; +package com.ljz.leetcode; /** * Created with IntelliJ IDEA. diff --git a/src/main/java/com/shuzijun/leetcode/ListNode.java b/src/main/java/com/ljz/leetcode/ListNode.java similarity index 92% rename from src/main/java/com/shuzijun/leetcode/ListNode.java rename to src/main/java/com/ljz/leetcode/ListNode.java index 4006de9..c705916 100644 --- a/src/main/java/com/shuzijun/leetcode/ListNode.java +++ b/src/main/java/com/ljz/leetcode/ListNode.java @@ -1,4 +1,4 @@ -package com.shuzijun.leetcode; +package com.ljz.leetcode; /** diff --git a/src/main/java/com/shuzijun/leetcode/editor/cn/Q153FindMinimumInRotatedSortedArray.java b/src/main/java/com/ljz/leetcode/editor/cn/Q153FindMinimumInRotatedSortedArray.java similarity index 98% rename from src/main/java/com/shuzijun/leetcode/editor/cn/Q153FindMinimumInRotatedSortedArray.java rename to src/main/java/com/ljz/leetcode/editor/cn/Q153FindMinimumInRotatedSortedArray.java index 16e98c4..633b818 100644 --- a/src/main/java/com/shuzijun/leetcode/editor/cn/Q153FindMinimumInRotatedSortedArray.java +++ b/src/main/java/com/ljz/leetcode/editor/cn/Q153FindMinimumInRotatedSortedArray.java @@ -1,4 +1,4 @@ -package com.shuzijun.leetcode.editor.cn; +package com.ljz.leetcode.editor.cn; /** * 已知一个长度为 n 的数组,预先按照升序排列,经由 1n旋转 后,得到输入数组。例如,原数组 nums = [0,1,2,4,5,6,7] 在变化后可能得到: diff --git a/src/main/java/com/shuzijun/leetcode/editor/cn/Q1TwoSum.java b/src/main/java/com/ljz/leetcode/editor/cn/Q1TwoSum.java similarity index 98% rename from src/main/java/com/shuzijun/leetcode/editor/cn/Q1TwoSum.java rename to src/main/java/com/ljz/leetcode/editor/cn/Q1TwoSum.java index e8c4a1e..7402efa 100644 --- a/src/main/java/com/shuzijun/leetcode/editor/cn/Q1TwoSum.java +++ b/src/main/java/com/ljz/leetcode/editor/cn/Q1TwoSum.java @@ -1,4 +1,4 @@ -package com.shuzijun.leetcode.editor.cn; +package com.ljz.leetcode.editor.cn; import java.util.Arrays; import java.util.HashMap; diff --git a/src/main/java/com/shuzijun/leetcode/editor/cn/Q33SearchInRotatedSortedArray.java b/src/main/java/com/ljz/leetcode/editor/cn/Q33SearchInRotatedSortedArray.java similarity index 98% rename from src/main/java/com/shuzijun/leetcode/editor/cn/Q33SearchInRotatedSortedArray.java rename to src/main/java/com/ljz/leetcode/editor/cn/Q33SearchInRotatedSortedArray.java index 1bad48c..5e088e8 100644 --- a/src/main/java/com/shuzijun/leetcode/editor/cn/Q33SearchInRotatedSortedArray.java +++ b/src/main/java/com/ljz/leetcode/editor/cn/Q33SearchInRotatedSortedArray.java @@ -1,4 +1,4 @@ -package com.shuzijun.leetcode.editor.cn; +package com.ljz.leetcode.editor.cn; /**

整数数组 nums 按升序排列,数组中的值 互不相同

diff --git a/src/main/java/com/shuzijun/leetcode/editor/cn/Q34FindFirstAndLastPositionOfElementInSortedArray.java b/src/main/java/com/ljz/leetcode/editor/cn/Q34FindFirstAndLastPositionOfElementInSortedArray.java similarity index 98% rename from src/main/java/com/shuzijun/leetcode/editor/cn/Q34FindFirstAndLastPositionOfElementInSortedArray.java rename to src/main/java/com/ljz/leetcode/editor/cn/Q34FindFirstAndLastPositionOfElementInSortedArray.java index 2ffcb38..ffb2f60 100644 --- a/src/main/java/com/shuzijun/leetcode/editor/cn/Q34FindFirstAndLastPositionOfElementInSortedArray.java +++ b/src/main/java/com/ljz/leetcode/editor/cn/Q34FindFirstAndLastPositionOfElementInSortedArray.java @@ -1,4 +1,4 @@ -package com.shuzijun.leetcode.editor.cn; +package com.ljz.leetcode.editor.cn; import java.util.Arrays; diff --git a/src/main/java/com/shuzijun/leetcode/editor/cn/Q35SearchInsertPosition.java b/src/main/java/com/ljz/leetcode/editor/cn/Q35SearchInsertPosition.java similarity index 98% rename from src/main/java/com/shuzijun/leetcode/editor/cn/Q35SearchInsertPosition.java rename to src/main/java/com/ljz/leetcode/editor/cn/Q35SearchInsertPosition.java index e0b1dec..e089acc 100644 --- a/src/main/java/com/shuzijun/leetcode/editor/cn/Q35SearchInsertPosition.java +++ b/src/main/java/com/ljz/leetcode/editor/cn/Q35SearchInsertPosition.java @@ -1,4 +1,4 @@ -package com.shuzijun.leetcode.editor.cn; +package com.ljz.leetcode.editor.cn; /**

给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。

diff --git a/src/main/java/com/shuzijun/leetcode/editor/cn/Q69Sqrtx.java b/src/main/java/com/ljz/leetcode/editor/cn/Q69Sqrtx.java similarity index 97% rename from src/main/java/com/shuzijun/leetcode/editor/cn/Q69Sqrtx.java rename to src/main/java/com/ljz/leetcode/editor/cn/Q69Sqrtx.java index abe38e6..9de093d 100644 --- a/src/main/java/com/shuzijun/leetcode/editor/cn/Q69Sqrtx.java +++ b/src/main/java/com/ljz/leetcode/editor/cn/Q69Sqrtx.java @@ -1,4 +1,4 @@ -package com.shuzijun.leetcode.editor.cn; +package com.ljz.leetcode.editor.cn; /**

实现 int sqrt(int x) 函数。

diff --git a/src/main/java/com/shuzijun/leetcode/editor/cn/Q74SearchA2dMatrix.java b/src/main/java/com/ljz/leetcode/editor/cn/Q74SearchA2dMatrix.java similarity index 98% rename from src/main/java/com/shuzijun/leetcode/editor/cn/Q74SearchA2dMatrix.java rename to src/main/java/com/ljz/leetcode/editor/cn/Q74SearchA2dMatrix.java index 6754a00..983463e 100644 --- a/src/main/java/com/shuzijun/leetcode/editor/cn/Q74SearchA2dMatrix.java +++ b/src/main/java/com/ljz/leetcode/editor/cn/Q74SearchA2dMatrix.java @@ -1,4 +1,4 @@ -package com.shuzijun.leetcode.editor.cn; +package com.ljz.leetcode.editor.cn; /**

编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值。该矩阵具有如下特性:

diff --git a/src/main/java/com/ljz/leetcode/editor/cn/RemoveDuplicatesFromSortedArray.java b/src/main/java/com/ljz/leetcode/editor/cn/RemoveDuplicatesFromSortedArray.java new file mode 100644 index 0000000..74d1861 --- /dev/null +++ b/src/main/java/com/ljz/leetcode/editor/cn/RemoveDuplicatesFromSortedArray.java @@ -0,0 +1,86 @@ +//给你一个 升序排列 的数组 nums ,请你 原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。元素的 相对顺序 应该保持 一致 +//。 +// +// 由于在某些语言中不能改变数组的长度,所以必须将结果放在数组nums的第一部分。更规范地说,如果在删除重复项之后有 k 个元素,那么 nums 的前 k 个 +//元素应该保存最终结果。 +// +// 将最终结果插入 nums 的前 k 个位置后返回 k 。 +// +// 不要使用额外的空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。 +// +// 判题标准: +// +// 系统会用下面的代码来测试你的题解: +// +// +//int[] nums = [...]; // 输入数组 +//int[] expectedNums = [...]; // 长度正确的期望答案 +// +//int k = removeDuplicates(nums); // 调用 +// +//assert k == expectedNums.length; +//for (int i = 0; i < k; i++) { +// assert nums[i] == expectedNums[i]; +//} +// +// 如果所有断言都通过,那么您的题解将被 通过。 +// +// +// +// 示例 1: +// +// +//输入:nums = [1,1,2] +//输出:2, nums = [1,2,_] +//解释:函数应该返回新的长度 2 ,并且原数组 nums 的前两个元素被修改为 1, 2 。不需要考虑数组中超出新长度后面的元素。 +// +// +// 示例 2: +// +// +//输入:nums = [0,0,1,1,1,2,2,3,3,4] +//输出:5, nums = [0,1,2,3,4] +//解释:函数应该返回新的长度 5 , 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4 。不需要考虑数组中超出新长度后面的元素。 +// +// +// +// +// 提示: +// +// +// 1 <= nums.length <= 3 * 10⁴ +// -10⁴ <= nums[i] <= 10⁴ +// nums 已按 升序 排列 +// +// +// Related Topics 数组 双指针 👍 3094 👎 0 + + +package com.ljz.leetcode.editor.cn; +public class RemoveDuplicatesFromSortedArray{ + public static void main(String[] args) { + Solution solution = new RemoveDuplicatesFromSortedArray().new Solution(); + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public int removeDuplicates(int[] nums) { + if (nums == null || nums.length == 0) { + return 0; + } + int i = 0, j = 1; + while (j < nums.length) { + if (nums[j] != nums[i]) { + if (j - i > 1) { + nums[i+1] = nums[j]; + } + i++; + } + j++; + } + return i+1; + } +} +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/com/ljz/leetcode/editor/cn/RemoveElement.java b/src/main/java/com/ljz/leetcode/editor/cn/RemoveElement.java new file mode 100644 index 0000000..9e2a253 --- /dev/null +++ b/src/main/java/com/ljz/leetcode/editor/cn/RemoveElement.java @@ -0,0 +1,87 @@ +//给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。 +// +// 不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组。 +// +// 元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。 +// +// +// +// 说明: +// +// 为什么返回数值是整数,但输出的答案是数组呢? +// +// 请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。 +// +// 你可以想象内部操作如下: +// +// +//// nums 是以“引用”方式传递的。也就是说,不对实参作任何拷贝 +//int len = removeElement(nums, val); +// +//// 在函数里修改输入数组对于调用者是可见的。 +//// 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。 +//for (int i = 0; i < len; i++) { +//    print(nums[i]); +//} +// +// +// +// +// 示例 1: +// +// +//输入:nums = [3,2,2,3], val = 3 +//输出:2, nums = [2,2] +//解释:函数应该返回新的长度 2, 并且 nums 中的前两个元素均为 2。你不需要考虑数组中超出新长度后面的元素。例如,函数返回的新长度为 2 ,而 +//nums = [2,2,3,3] 或 nums = [2,2,0,0],也会被视作正确答案。 +// +// +// 示例 2: +// +// +//输入:nums = [0,1,2,2,3,0,4,2], val = 2 +//输出:5, nums = [0,1,4,0,3] +//解释:函数应该返回新的长度 5, 并且 nums 中的前五个元素为 0, 1, 3, 0, 4。注意这五个元素可为任意顺序。你不需要考虑数组中超出新长度后面 +//的元素。 +// +// +// +// +// 提示: +// +// +// 0 <= nums.length <= 100 +// 0 <= nums[i] <= 50 +// 0 <= val <= 100 +// +// +// Related Topics 数组 双指针 👍 1742 👎 0 + + +package com.ljz.leetcode.editor.cn; +public class RemoveElement{ + public static void main(String[] args) { + Solution solution = new RemoveElement().new Solution(); + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public int removeElement(int[] nums, int val) { + if (nums == null || nums.length == 0) { + return 0; + } + int i = 0, j = 0; + while (j < nums.length) { + if (nums[j] != val) { + nums[i] = nums[j]; + i++; + } + j++; + } + return i; + + } +} +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/com/ljz/leetcode/editor/cn/SearchInsertPosition.java b/src/main/java/com/ljz/leetcode/editor/cn/SearchInsertPosition.java new file mode 100644 index 0000000..7609bc9 --- /dev/null +++ b/src/main/java/com/ljz/leetcode/editor/cn/SearchInsertPosition.java @@ -0,0 +1,79 @@ +//给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。 +// +// 请必须使用时间复杂度为 O(log n) 的算法。 +// +// +// +// 示例 1: +// +// +//输入: nums = [1,3,5,6], target = 5 +//输出: 2 +// +// +// 示例 2: +// +// +//输入: nums = [1,3,5,6], target = 2 +//输出: 1 +// +// +// 示例 3: +// +// +//输入: nums = [1,3,5,6], target = 7 +//输出: 4 +// +// +// +// +// 提示: +// +// +// 1 <= nums.length <= 10⁴ +// -10⁴ <= nums[i] <= 10⁴ +// nums 为 无重复元素 的 升序 排列数组 +// -10⁴ <= target <= 10⁴ +// +// +// Related Topics 数组 二分查找 👍 1977 👎 0 + + +package com.ljz.leetcode.editor.cn; +public class SearchInsertPosition{ + public static void main(String[] args) { + Solution solution = new SearchInsertPosition().new Solution(); + } + + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public int searchInsert(int[] nums, int target) { + return binarySearch(nums, target, 0, nums.length-1); + } + + private int binarySearch(int[] nums, int target, int p, int q) { + // 保证目标值在数组范围内 + if (nums[p] >= target) { return p; } + if (nums[q] == target) { return q; } + if (nums[q] < target) { return q+1; } + // 寻找两个指针中间的值 + int h = (q - p) / 2; + // 如果两个指针中间只有空隙,则找到要插入的位置 + if (h < 1) { + return p + 1; + } + h = p + h; + if (nums[h] < target) { + return binarySearch(nums, target, h, q); + } else if (nums[h] > target) { + return binarySearch(nums, target, p, h); + } else { + return h; + } + + } + +} +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/com/ljz/leetcode/editor/cn/TwoSum.java b/src/main/java/com/ljz/leetcode/editor/cn/TwoSum.java new file mode 100644 index 0000000..bd21ee0 --- /dev/null +++ b/src/main/java/com/ljz/leetcode/editor/cn/TwoSum.java @@ -0,0 +1,74 @@ +//给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。 +// +// 你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。 +// +// 你可以按任意顺序返回答案。 +// +// +// +// 示例 1: +// +// +//输入:nums = [2,7,11,15], target = 9 +//输出:[0,1] +//解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。 +// +// +// 示例 2: +// +// +//输入:nums = [3,2,4], target = 6 +//输出:[1,2] +// +// +// 示例 3: +// +// +//输入:nums = [3,3], target = 6 +//输出:[0,1] +// +// +// +// +// 提示: +// +// +// 2 <= nums.length <= 10⁴ +// -10⁹ <= nums[i] <= 10⁹ +// -10⁹ <= target <= 10⁹ +// 只会存在一个有效答案 +// +// +// +// +// 进阶:你可以想出一个时间复杂度小于 O(n²) 的算法吗? +// +// Related Topics 数组 哈希表 👍 16691 👎 0 + + +package com.ljz.leetcode.editor.cn; + +import java.util.HashMap; + +public class TwoSum{ + public static void main(String[] args) { + Solution solution = new TwoSum().new Solution(); + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int[] twoSum(int[] nums, int target) { + HashMap map = new HashMap<>(); + for (int i = 0; i < nums.length; i++) { + Integer sub = target-nums[i]; + if (map.containsKey(sub)) { + return new int[] {map.get(sub), i}; + } + map.put(nums[i], i); + } + return new int[] {}; + } + } +//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/all.json b/src/main/java/com/ljz/leetcode/editor/cn/doc/all.json similarity index 100% rename from src/main/java/com/shuzijun/leetcode/editor/cn/doc/all.json rename to src/main/java/com/ljz/leetcode/editor/cn/doc/all.json diff --git a/src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q153FindMinimumInRotatedSortedArray.md b/src/main/java/com/ljz/leetcode/editor/cn/doc/content/Q153FindMinimumInRotatedSortedArray.md similarity index 100% rename from src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q153FindMinimumInRotatedSortedArray.md rename to src/main/java/com/ljz/leetcode/editor/cn/doc/content/Q153FindMinimumInRotatedSortedArray.md diff --git a/src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q1TwoSum.md b/src/main/java/com/ljz/leetcode/editor/cn/doc/content/Q1TwoSum.md similarity index 100% rename from src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q1TwoSum.md rename to src/main/java/com/ljz/leetcode/editor/cn/doc/content/Q1TwoSum.md diff --git a/src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q33SearchInRotatedSortedArray.md b/src/main/java/com/ljz/leetcode/editor/cn/doc/content/Q33SearchInRotatedSortedArray.md similarity index 100% rename from src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q33SearchInRotatedSortedArray.md rename to src/main/java/com/ljz/leetcode/editor/cn/doc/content/Q33SearchInRotatedSortedArray.md diff --git a/src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q34FindFirstAndLastPositionOfElementInSortedArray.md b/src/main/java/com/ljz/leetcode/editor/cn/doc/content/Q34FindFirstAndLastPositionOfElementInSortedArray.md similarity index 100% rename from src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q34FindFirstAndLastPositionOfElementInSortedArray.md rename to src/main/java/com/ljz/leetcode/editor/cn/doc/content/Q34FindFirstAndLastPositionOfElementInSortedArray.md diff --git a/src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q35SearchInsertPosition.md b/src/main/java/com/ljz/leetcode/editor/cn/doc/content/Q35SearchInsertPosition.md similarity index 100% rename from src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q35SearchInsertPosition.md rename to src/main/java/com/ljz/leetcode/editor/cn/doc/content/Q35SearchInsertPosition.md diff --git a/src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q69Sqrtx.md b/src/main/java/com/ljz/leetcode/editor/cn/doc/content/Q69Sqrtx.md similarity index 100% rename from src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q69Sqrtx.md rename to src/main/java/com/ljz/leetcode/editor/cn/doc/content/Q69Sqrtx.md diff --git a/src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q74SearchA2dMatrix.md b/src/main/java/com/ljz/leetcode/editor/cn/doc/content/Q74SearchA2dMatrix.md similarity index 100% rename from src/main/java/com/shuzijun/leetcode/editor/cn/doc/content/Q74SearchA2dMatrix.md rename to src/main/java/com/ljz/leetcode/editor/cn/doc/content/Q74SearchA2dMatrix.md diff --git a/src/main/java/com/ljz/leetcode/editor/cn/doc/content/RemoveDuplicatesFromSortedArray.md b/src/main/java/com/ljz/leetcode/editor/cn/doc/content/RemoveDuplicatesFromSortedArray.md new file mode 100644 index 0000000..ed47235 --- /dev/null +++ b/src/main/java/com/ljz/leetcode/editor/cn/doc/content/RemoveDuplicatesFromSortedArray.md @@ -0,0 +1,54 @@ +

给你一个 升序排列 的数组 nums ,请你 原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。元素的 相对顺序 应该保持 一致

+ +

由于在某些语言中不能改变数组的长度,所以必须将结果放在数组nums的第一部分。更规范地说,如果在删除重复项之后有 k 个元素,那么 nums 的前 k 个元素应该保存最终结果。

+ +

将最终结果插入 nums 的前 k 个位置后返回 k

+ +

不要使用额外的空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。

+ +

判题标准:

+ +

系统会用下面的代码来测试你的题解:

+ +
+int[] nums = [...]; // 输入数组
+int[] expectedNums = [...]; // 长度正确的期望答案
+
+int k = removeDuplicates(nums); // 调用
+
+assert k == expectedNums.length;
+for (int i = 0; i < k; i++) {
+    assert nums[i] == expectedNums[i];
+}
+ +

如果所有断言都通过,那么您的题解将被 通过

+ +

 

+ +

示例 1:

+ +
+输入:nums = [1,1,2]
+输出:2, nums = [1,2,_]
+解释:函数应该返回新的长度 2 ,并且原数组 nums 的前两个元素被修改为 1, 2 不需要考虑数组中超出新长度后面的元素。
+
+ +

示例 2:

+ +
+输入:nums = [0,0,1,1,1,2,2,3,3,4]
+输出:5, nums = [0,1,2,3,4]
+解释:函数应该返回新的长度 5 , 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4 。不需要考虑数组中超出新长度后面的元素。
+
+ +

 

+ +

提示:

+ + + +
Related Topics
  • 数组
  • 双指针

  • 👍 3094
  • 👎 0
  • \ No newline at end of file diff --git a/src/main/java/com/ljz/leetcode/editor/cn/doc/content/RemoveElement.md b/src/main/java/com/ljz/leetcode/editor/cn/doc/content/RemoveElement.md new file mode 100644 index 0000000..97e7963 --- /dev/null +++ b/src/main/java/com/ljz/leetcode/editor/cn/doc/content/RemoveElement.md @@ -0,0 +1,56 @@ +

    给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。

    + +

    不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组

    + +

    元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。

    + +

     

    + +

    说明:

    + +

    为什么返回数值是整数,但输出的答案是数组呢?

    + +

    请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。

    + +

    你可以想象内部操作如下:

    + +
    +// nums 是以“引用”方式传递的。也就是说,不对实参作任何拷贝
    +int len = removeElement(nums, val);
    +
    +// 在函数里修改输入数组对于调用者是可见的。
    +// 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
    +for (int i = 0; i < len; i++) {
    +    print(nums[i]);
    +}
    +
    + +

     

    + +

    示例 1:

    + +
    +输入:nums = [3,2,2,3], val = 3
    +输出:2, nums = [2,2]
    +解释:函数应该返回新的长度 2, 并且 nums 中的前两个元素均为 2。你不需要考虑数组中超出新长度后面的元素。例如,函数返回的新长度为 2 ,而 nums = [2,2,3,3] 或 nums = [2,2,0,0],也会被视作正确答案。
    +
    + +

    示例 2:

    + +
    +输入:nums = [0,1,2,2,3,0,4,2], val = 2
    +输出:5, nums = [0,1,4,0,3]
    +解释:函数应该返回新的长度 5, 并且 nums 中的前五个元素为 0, 1, 3, 0, 4。注意这五个元素可为任意顺序。你不需要考虑数组中超出新长度后面的元素。
    +
    + +

     

    + +

    提示:

    + + + +
    Related Topics
  • 数组
  • 双指针

  • 👍 1742
  • 👎 0
  • \ No newline at end of file diff --git a/src/main/java/com/ljz/leetcode/editor/cn/doc/content/SearchInsertPosition.md b/src/main/java/com/ljz/leetcode/editor/cn/doc/content/SearchInsertPosition.md new file mode 100644 index 0000000..236a3ff --- /dev/null +++ b/src/main/java/com/ljz/leetcode/editor/cn/doc/content/SearchInsertPosition.md @@ -0,0 +1,39 @@ +

    给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。

    + +

    请必须使用时间复杂度为 O(log n) 的算法。

    + +

     

    + +

    示例 1:

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

    示例 2:

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

    示例 3:

    + +
    +输入: nums = [1,3,5,6], target = 7
    +输出: 4
    +
    + +

     

    + +

    提示:

    + + + +
    Related Topics
  • 数组
  • 二分查找

  • 👍 1977
  • 👎 0
  • \ No newline at end of file diff --git a/src/main/java/com/ljz/leetcode/editor/cn/doc/content/TwoSum.md b/src/main/java/com/ljz/leetcode/editor/cn/doc/content/TwoSum.md new file mode 100644 index 0000000..d028b84 --- /dev/null +++ b/src/main/java/com/ljz/leetcode/editor/cn/doc/content/TwoSum.md @@ -0,0 +1,46 @@ +

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

    + +

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

    + +

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

    + +

     

    + +

    示例 1:

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

    示例 2:

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

    示例 3:

    + +
    +输入:nums = [3,3], target = 6
    +输出:[0,1]
    +
    + +

     

    + +

    提示:

    + + + +

     

    + +

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

    + +
    Related Topics
  • 数组
  • 哈希表

  • 👍 16691
  • 👎 0
  • \ No newline at end of file diff --git a/src/main/java/com/shuzijun/leetcode/editor/cn/doc/translation.json b/src/main/java/com/ljz/leetcode/editor/cn/doc/translation.json similarity index 100% rename from src/main/java/com/shuzijun/leetcode/editor/cn/doc/translation.json rename to src/main/java/com/ljz/leetcode/editor/cn/doc/translation.json diff --git a/src/main/java/com/shuzijun/leetcode/editor/en/AddTwoNumbers.java b/src/main/java/com/ljz/leetcode/editor/en/AddTwoNumbers.java similarity index 96% rename from src/main/java/com/shuzijun/leetcode/editor/en/AddTwoNumbers.java rename to src/main/java/com/ljz/leetcode/editor/en/AddTwoNumbers.java index 30ab4df..96abda8 100644 --- a/src/main/java/com/shuzijun/leetcode/editor/en/AddTwoNumbers.java +++ b/src/main/java/com/ljz/leetcode/editor/en/AddTwoNumbers.java @@ -11,9 +11,9 @@ // // -package com.shuzijun.leetcode.editor.en; +package com.ljz.leetcode.editor.en; -import com.shuzijun.leetcode.ListNode; +import com.ljz.leetcode.ListNode; public class AddTwoNumbers { public static void main(String[] args) { diff --git a/src/main/java/com/shuzijun/leetcode/editor/en/ContainerWithMostWater.java b/src/main/java/com/ljz/leetcode/editor/en/ContainerWithMostWater.java similarity index 97% rename from src/main/java/com/shuzijun/leetcode/editor/en/ContainerWithMostWater.java rename to src/main/java/com/ljz/leetcode/editor/en/ContainerWithMostWater.java index 1ebeb1e..f5f4ba4 100644 --- a/src/main/java/com/shuzijun/leetcode/editor/en/ContainerWithMostWater.java +++ b/src/main/java/com/ljz/leetcode/editor/en/ContainerWithMostWater.java @@ -17,7 +17,7 @@ //Output: 49 // -package com.shuzijun.leetcode.editor.en; +package com.ljz.leetcode.editor.en; public class ContainerWithMostWater { public static void main(String[] args) { diff --git a/src/main/java/com/shuzijun/leetcode/editor/en/GenerateParentheses.java b/src/main/java/com/ljz/leetcode/editor/en/GenerateParentheses.java similarity index 96% rename from src/main/java/com/shuzijun/leetcode/editor/en/GenerateParentheses.java rename to src/main/java/com/ljz/leetcode/editor/en/GenerateParentheses.java index a91afe9..07f7862 100644 --- a/src/main/java/com/shuzijun/leetcode/editor/en/GenerateParentheses.java +++ b/src/main/java/com/ljz/leetcode/editor/en/GenerateParentheses.java @@ -15,7 +15,7 @@ //] // -package com.shuzijun.leetcode.editor.en; +package com.ljz.leetcode.editor.en; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/com/shuzijun/leetcode/editor/en/PalindromeNumber.java b/src/main/java/com/ljz/leetcode/editor/en/PalindromeNumber.java similarity index 97% rename from src/main/java/com/shuzijun/leetcode/editor/en/PalindromeNumber.java rename to src/main/java/com/ljz/leetcode/editor/en/PalindromeNumber.java index 93cb532..f601a37 100644 --- a/src/main/java/com/shuzijun/leetcode/editor/en/PalindromeNumber.java +++ b/src/main/java/com/ljz/leetcode/editor/en/PalindromeNumber.java @@ -28,7 +28,7 @@ // Coud you solve it without converting the integer to a string? // -package com.shuzijun.leetcode.editor.en; +package com.ljz.leetcode.editor.en; public class PalindromeNumber { public static void main(String[] args) { diff --git a/src/main/java/com/shuzijun/leetcode/editor/en/ReverseInteger.java b/src/main/java/com/ljz/leetcode/editor/en/ReverseInteger.java similarity index 96% rename from src/main/java/com/shuzijun/leetcode/editor/en/ReverseInteger.java rename to src/main/java/com/ljz/leetcode/editor/en/ReverseInteger.java index 2c48e12..5ebeaf4 100644 --- a/src/main/java/com/shuzijun/leetcode/editor/en/ReverseInteger.java +++ b/src/main/java/com/ljz/leetcode/editor/en/ReverseInteger.java @@ -25,7 +25,7 @@ //Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows. // -package com.shuzijun.leetcode.editor.en; +package com.ljz.leetcode.editor.en; public class ReverseInteger { public static void main(String[] args) { diff --git a/src/main/java/com/shuzijun/leetcode/editor/en/SearchInRotatedSortedArray.java b/src/main/java/com/ljz/leetcode/editor/en/SearchInRotatedSortedArray.java similarity index 97% rename from src/main/java/com/shuzijun/leetcode/editor/en/SearchInRotatedSortedArray.java rename to src/main/java/com/ljz/leetcode/editor/en/SearchInRotatedSortedArray.java index bdb41d2..c7cf612 100644 --- a/src/main/java/com/shuzijun/leetcode/editor/en/SearchInRotatedSortedArray.java +++ b/src/main/java/com/ljz/leetcode/editor/en/SearchInRotatedSortedArray.java @@ -22,7 +22,7 @@ //Output: -1 // -package com.shuzijun.leetcode.editor.en; +package com.ljz.leetcode.editor.en; public class SearchInRotatedSortedArray { public static void main(String[] args) { diff --git a/src/main/java/com/shuzijun/leetcode/editor/en/StringToIntegerAtoi.java b/src/main/java/com/ljz/leetcode/editor/en/StringToIntegerAtoi.java similarity index 98% rename from src/main/java/com/shuzijun/leetcode/editor/en/StringToIntegerAtoi.java rename to src/main/java/com/ljz/leetcode/editor/en/StringToIntegerAtoi.java index a7a0479..be79ea4 100644 --- a/src/main/java/com/shuzijun/leetcode/editor/en/StringToIntegerAtoi.java +++ b/src/main/java/com/ljz/leetcode/editor/en/StringToIntegerAtoi.java @@ -56,7 +56,7 @@ //  Thefore INT_MIN (−231) is returned. // -package com.shuzijun.leetcode.editor.en; +package com.ljz.leetcode.editor.en; public class StringToIntegerAtoi { public static void main(String[] args) { diff --git a/src/main/java/com/shuzijun/leetcode/editor/en/SwapNodesInPairs.java b/src/main/java/com/ljz/leetcode/editor/en/SwapNodesInPairs.java similarity index 94% rename from src/main/java/com/shuzijun/leetcode/editor/en/SwapNodesInPairs.java rename to src/main/java/com/ljz/leetcode/editor/en/SwapNodesInPairs.java index 09e070d..0461fc8 100644 --- a/src/main/java/com/shuzijun/leetcode/editor/en/SwapNodesInPairs.java +++ b/src/main/java/com/ljz/leetcode/editor/en/SwapNodesInPairs.java @@ -11,9 +11,9 @@ // // -package com.shuzijun.leetcode.editor.en; +package com.ljz.leetcode.editor.en; -import com.shuzijun.leetcode.ListNode; +import com.ljz.leetcode.ListNode; public class SwapNodesInPairs { public static void main(String[] args) { diff --git a/src/main/java/com/shuzijun/leetcode/editor/en/ThreeSum.java b/src/main/java/com/ljz/leetcode/editor/en/ThreeSum.java similarity index 98% rename from src/main/java/com/shuzijun/leetcode/editor/en/ThreeSum.java rename to src/main/java/com/ljz/leetcode/editor/en/ThreeSum.java index 7ef5540..d43dc6a 100644 --- a/src/main/java/com/shuzijun/leetcode/editor/en/ThreeSum.java +++ b/src/main/java/com/ljz/leetcode/editor/en/ThreeSum.java @@ -17,7 +17,7 @@ // // -package com.shuzijun.leetcode.editor.en; +package com.ljz.leetcode.editor.en; import java.util.ArrayList; import java.util.Arrays; diff --git a/src/main/java/com/shuzijun/leetcode/editor/en/ThreeSumClosest.java b/src/main/java/com/ljz/leetcode/editor/en/ThreeSumClosest.java similarity index 97% rename from src/main/java/com/shuzijun/leetcode/editor/en/ThreeSumClosest.java rename to src/main/java/com/ljz/leetcode/editor/en/ThreeSumClosest.java index c22676f..eaa5097 100644 --- a/src/main/java/com/shuzijun/leetcode/editor/en/ThreeSumClosest.java +++ b/src/main/java/com/ljz/leetcode/editor/en/ThreeSumClosest.java @@ -9,7 +9,7 @@ // // -package com.shuzijun.leetcode.editor.en; +package com.ljz.leetcode.editor.en; import java.util.Arrays; diff --git a/src/main/java/com/shuzijun/leetcode/editor/en/TwoSum.java b/src/main/java/com/ljz/leetcode/editor/en/TwoSum.java similarity index 96% rename from src/main/java/com/shuzijun/leetcode/editor/en/TwoSum.java rename to src/main/java/com/ljz/leetcode/editor/en/TwoSum.java index 9739a9e..73369e2 100644 --- a/src/main/java/com/shuzijun/leetcode/editor/en/TwoSum.java +++ b/src/main/java/com/ljz/leetcode/editor/en/TwoSum.java @@ -12,7 +12,7 @@ // // -package com.shuzijun.leetcode.editor.en; +package com.ljz.leetcode.editor.en; import java.util.HashMap; import java.util.Map; diff --git a/src/main/java/com/shuzijun/leetcode/editor/en/ZigzagConversion.java b/src/main/java/com/ljz/leetcode/editor/en/ZigzagConversion.java similarity index 98% rename from src/main/java/com/shuzijun/leetcode/editor/en/ZigzagConversion.java rename to src/main/java/com/ljz/leetcode/editor/en/ZigzagConversion.java index ca06c6a..a4dadf9 100644 --- a/src/main/java/com/shuzijun/leetcode/editor/en/ZigzagConversion.java +++ b/src/main/java/com/ljz/leetcode/editor/en/ZigzagConversion.java @@ -33,7 +33,7 @@ //P I // -package com.shuzijun.leetcode.editor.en; +package com.ljz.leetcode.editor.en; public class ZigzagConversion{ public static void main(String[] args) { Solution solution = new ZigzagConversion().new Solution();