diff --git a/.gitignore b/.gitignore index fc21647..99d0bda 100644 --- a/.gitignore +++ b/.gitignore @@ -1,26 +1,33 @@ -# Compiled class file -*.class +#maven +target/ +captcha/*.png -# Log file -*.log +work/ -# BlueJ files -*.ctxt +#eclipse ignore +.settings/ +.project +.classpath +bin/ -# Mobile Tools for Java (J2ME) -.mtj.tmp/ +#idea ignore +.idea/ +*.ipr +*.iml +*.iws +out/ -# Package Files # -*.jar -*.war -*.nar -*.ear -*.zip +#temp ignore +*.log +*.cache +*.diff +*.patch +*.tmp +*.swp +logs +test-output +.recommenders +.DS_Store +/app/EvaApp/node_modules/ *.tar.gz -*.rar - -# 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/*.* +*.zip diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..c995aa5 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "java.debug.settings.onBuildFailureProceed": true +} \ No newline at end of file diff --git a/src/main/java/com/shuzijun/leetcode/HelloWorld.java b/src/main/java/com/shuzijun/leetcode/HelloWorld.java deleted file mode 100644 index 2383b20..0000000 --- a/src/main/java/com/shuzijun/leetcode/HelloWorld.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.shuzijun.leetcode; - -/** - * Created with IntelliJ IDEA. - * User: shuzijun - * Date: 2018-11-22 - * Time: 20:52 - * To change this template use File | Settings | File Templates. - */ -public class HelloWorld { - - public static void main(String[] args) { - System.out.println("hello world"); - } -} diff --git a/src/main/java/com/shuzijun/leetcode/editor/cn/Q153FindMinimumInRotatedSortedArray.java b/src/main/java/com/shuzijun/leetcode/editor/cn/Q153FindMinimumInRotatedSortedArray.java deleted file mode 100644 index 16e98c4..0000000 --- a/src/main/java/com/shuzijun/leetcode/editor/cn/Q153FindMinimumInRotatedSortedArray.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.shuzijun.leetcode.editor.cn; - -/** - * 已知一个长度为 n 的数组,预先按照升序排列,经由 1n旋转 后,得到输入数组。例如,原数组 nums = [0,1,2,4,5,6,7] 在变化后可能得到: - * - * - *

注意,数组 [a[0], a[1], a[2], ..., a[n-1]] 旋转一次 的结果为数组 [a[n-1], a[0], a[1], a[2], ..., a[n-2]]

- * - *

给你一个元素值 互不相同 的数组 nums ,它原来是一个升序排列的数组,并按上述情形进行了多次旋转。请你找出并返回数组中的 最小元素

- * - *

- * - *

示例 1:

- * - *
- * 输入:nums = [3,4,5,1,2]
- * 输出:1
- * 解释:原数组为 [1,2,3,4,5] ,旋转 3 次得到输入数组。
- * 
- * - *

示例 2:

- * - *
- * 输入:nums = [4,5,6,7,0,1,2]
- * 输出:0
- * 解释:原数组为 [0,1,2,4,5,6,7] ,旋转 4 次得到输入数组。
- * 
- * - *

示例 3:

- * - *
- * 输入:nums = [11,13,15,17]
- * 输出:11
- * 解释:原数组为 [11,13,15,17] ,旋转 4 次得到输入数组。
- * 
- * - *

- * - *

提示:

- * - * - *
Related Topics
  • 数组
  • 二分查找

  • 👍 512
  • 👎 0
  • - */ - -public class Q153FindMinimumInRotatedSortedArray { - public static void main(String[] args) { - Q153FindMinimumInRotatedSortedArray.Solution solution = new Q153FindMinimumInRotatedSortedArray().new Solution(); - int[] nums = {4, 5, 6, 7, 0, 1, 2}; - System.out.println(solution.findMin(nums)); - } - - //leetcode submit region begin(Prohibit modification and deletion) - class Solution { - public int findMin(int[] nums) { - int low = 0; - int high = nums.length - 1; - while (low < high) { - int pivot = low + (high - low) / 2; - if (nums[pivot] < nums[high]) { - high = pivot; - } else { - low = pivot + 1; - } - } - return nums[low]; - } - } -//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/Q1TwoSum.java b/src/main/java/com/shuzijun/leetcode/editor/cn/Q1TwoSum.java deleted file mode 100644 index e8c4a1e..0000000 --- a/src/main/java/com/shuzijun/leetcode/editor/cn/Q1TwoSum.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.shuzijun.leetcode.editor.cn; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - -/** -

    给定一个整数数组 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
  • 数组
  • 哈希表

  • 👍 11546
  • 👎 0
  • -*/ - -public class Q1TwoSum{ - public static void main(String[] args) { - Solution solution = new Q1TwoSum().new Solution(); - int[] nums = {2, 7, 11, 15}; - int target = 9; - System.out.println(Arrays.toString(solution.twoSum(nums, target))); - } - //leetcode submit region begin(Prohibit modification and deletion) - class Solution { - public int[] twoSum(int[] nums, int target) { - Map map = new HashMap(); - for (int i = 0; i < nums.length; i++) { - int complement = target - nums[i]; - if (map.containsKey(complement)) { - return new int[]{map.get(complement), i}; - } - map.put(nums[i], i); - } - throw new IllegalArgumentException("No two sum solution"); - } - } -//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/Q33SearchInRotatedSortedArray.java b/src/main/java/com/shuzijun/leetcode/editor/cn/Q33SearchInRotatedSortedArray.java deleted file mode 100644 index 1bad48c..0000000 --- a/src/main/java/com/shuzijun/leetcode/editor/cn/Q33SearchInRotatedSortedArray.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.shuzijun.leetcode.editor.cn; -/** -

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

    - -

    在传递给函数之前,nums 在预先未知的某个下标 k0 <= k < nums.length)上进行了 旋转,使数组变为 [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]](下标 从 0 开始 计数)。例如, [0,1,2,4,5,6,7] 在下标 3 处经旋转后可能变为 [4,5,6,7,0,1,2]

    - -

    给你 旋转后 的数组 nums 和一个整数 target ,如果 nums 中存在这个目标值 target ,则返回它的下标,否则返回 -1

    - -

    - -

    示例 1:

    - -
    -输入:nums = [4,5,6,7,0,1,2], target = 0
    -输出:4
    -
    - -

    示例 2:

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

    示例 3:

    - -
    -输入:nums = [1], target = 0
    -输出:-1
    -
    - -

    - -

    提示:

    - - - -

    - -

    进阶:你可以设计一个时间复杂度为 O(log n) 的解决方案吗?

    -
    Related Topics
  • 数组
  • 二分查找

  • 👍 1442
  • 👎 0
  • -*/ - -public class Q33SearchInRotatedSortedArray{ - public static void main(String[] args) { - Solution solution = new Q33SearchInRotatedSortedArray().new Solution(); - } - //leetcode submit region begin(Prohibit modification and deletion) -class Solution { - public int search(int[] nums, int target) { - if (nums.length <= 0) return -1; - if (nums.length == 1 && nums[0] == target) return 0; - int s = 0; - int e = nums.length - 1; - while (s <= e) { - int m = (s+e)/2; - if (nums[m] == target) { - return m; - } - if (nums[m] < nums[e]) { - if (nums[m] < target && nums[e] >= target) { - s = m + 1; - } else { - e = m - 1; - } - } else { - if (nums[s] <= target && nums[m] > target) { - e = m - 1; - } else { - s = m + 1; - - } - } - } - return -1; - } -} -//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/Q34FindFirstAndLastPositionOfElementInSortedArray.java b/src/main/java/com/shuzijun/leetcode/editor/cn/Q34FindFirstAndLastPositionOfElementInSortedArray.java deleted file mode 100644 index 2ffcb38..0000000 --- a/src/main/java/com/shuzijun/leetcode/editor/cn/Q34FindFirstAndLastPositionOfElementInSortedArray.java +++ /dev/null @@ -1,104 +0,0 @@ -package com.shuzijun.leetcode.editor.cn; - -import java.util.Arrays; - -/** - *

    给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。

    - * - *

    如果数组中不存在目标值 target,返回 [-1, -1]

    - * - *

    进阶:

    - * - * - * - *

    - * - *

    示例 1:

    - * - *
    - * 输入:nums = [5,7,7,8,8,10], target = 8
    - * 输出:[3,4]
    - * - *

    示例 2:

    - * - *
    - * 输入:nums = [5,7,7,8,8,10], target = 6
    - * 输出:[-1,-1]
    - * - *

    示例 3:

    - * - *
    - * 输入:nums = [], target = 0
    - * 输出:[-1,-1]
    - * - *

    - * - *

    提示:

    - * - * - *
    Related Topics
  • 数组
  • 二分查找

  • 👍 1098
  • 👎 0
  • - */ - -public class Q34FindFirstAndLastPositionOfElementInSortedArray { - public static void main(String[] args) { - Q34FindFirstAndLastPositionOfElementInSortedArray.Solution solution = new Q34FindFirstAndLastPositionOfElementInSortedArray().new Solution(); - int[] nums = new int[]{5, 7, 7, 8, 8, 10}; - int target = 6; - System.out.println(Arrays.toString(solution.searchRange(nums, target))); - } - - //leetcode submit region begin(Prohibit modification and deletion) - class Solution { - public int[] searchRange(int[] nums, int target) { - int len = nums.length; - - if (len == 0 || target < nums[0] || target > nums[len - 1]) { - return new int[]{-1, -1}; - } - if (len == 1) { - if (nums[0] == target) { - return new int[]{0, 0}; - } else { - return new int[]{-1, -1}; - } - } - return binarySearch(nums, 0, len - 1, target); - } - - private int[] binarySearch(int[] nums, int start, int end, int target) { - int binary = (start + end + 1) / 2; - if (nums[binary] == target) { - int[] ans = {binary, binary}; - while ((ans[0] - 1) >= start) { - if (nums[ans[0] - 1] == target) { - ans[0] = ans[0] - 1; - } else { - break; - } - } - while ((ans[1] + 1) <= end) { - if (nums[ans[1] + 1] == target) { - ans[1] = ans[1] + 1; - } else { - break; - } - } - return ans; - } else if (nums[binary] > target && (binary - 1) >= start) { - return binarySearch(nums, start, binary - 1, target); - } else if (nums[binary] < target && (binary + 1) <= end) { - return binarySearch(nums, binary, end, target); - } else { - return new int[]{-1, -1}; - } - } - } -//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/Q35SearchInsertPosition.java b/src/main/java/com/shuzijun/leetcode/editor/cn/Q35SearchInsertPosition.java deleted file mode 100644 index e0b1dec..0000000 --- a/src/main/java/com/shuzijun/leetcode/editor/cn/Q35SearchInsertPosition.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.shuzijun.leetcode.editor.cn; -/** -

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

    - -

    你可以假设数组中无重复元素。

    - -

    示例 1:

    - -
    输入: [1,3,5,6], 5
    -输出: 2
    -
    - -

    示例 2:

    - -
    输入: [1,3,5,6], 2
    -输出: 1
    -
    - -

    示例 3:

    - -
    输入: [1,3,5,6], 7
    -输出: 4
    -
    - -

    示例 4:

    - -
    输入: [1,3,5,6], 0
    -输出: 0
    -
    -
    Related Topics
  • 数组
  • 二分查找

  • 👍 963
  • 👎 0
  • -*/ - -public class Q35SearchInsertPosition{ - public static void main(String[] args) { - Solution solution = new Q35SearchInsertPosition().new Solution(); - } - //leetcode submit region begin(Prohibit modification and deletion) -class Solution { - public int searchInsert(int[] nums, int target) { - int len = nums.length; - - if (len == 0 || target < nums[0]) { - return 0; - } else if (target > nums[len - 1]) { - return len; - } - return binarySearch(nums, 0, len - 1, target); - } - - private int binarySearch(int[] nums, int start, int end, int target) { - int binary = (start + end + 1) / 2; - if (nums[binary] == target) { - return binary; - } else if (nums[binary] > target && (binary - 1) >= start) { - if (nums[binary - 1] < target) { - return binary; - } else if (nums[binary - 1] == target) { - return binary - 1; - } - return binarySearch(nums, start, binary - 1, target); - } else if (nums[binary] < target && (binary + 1) <= end) { - if (nums[binary + 1] >= target) { - return binary + 1; - } - return binarySearch(nums, binary, end, target); - } else { - return binary; - } - } -} -//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/Q69Sqrtx.java b/src/main/java/com/shuzijun/leetcode/editor/cn/Q69Sqrtx.java deleted file mode 100644 index abe38e6..0000000 --- a/src/main/java/com/shuzijun/leetcode/editor/cn/Q69Sqrtx.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.shuzijun.leetcode.editor.cn; -/** -

    实现 int sqrt(int x) 函数。

    - -

    计算并返回 x 的平方根,其中 x 是非负整数。

    - -

    由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。

    - -

    示例 1:

    - -
    输入: 4
    -输出: 2
    -
    - -

    示例 2:

    - -
    输入: 8
    -输出: 2
    -说明: 8 的平方根是 2.82842..., 
    -     由于返回类型是整数,小数部分将被舍去。
    -
    -
    Related Topics
  • 数学
  • 二分查找

  • 👍 714
  • 👎 0
  • -*/ - -public class Q69Sqrtx{ - public static void main(String[] args) { - Solution solution = new Q69Sqrtx().new Solution(); - } - /** - * k的平方小于等于x - */ - //leetcode submit region begin(Prohibit modification and deletion) - class Solution { - public int mySqrt(int x) { - int l = 0, r = x, ans = -1; - while (l <= r) { - int mid = l + (r - l) / 2; - if ((long) mid * mid <= x) { - ans = mid; - l = mid + 1; - } else { - r = mid - 1; - } - } - return ans; - } - } -//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/Q74SearchA2dMatrix.java b/src/main/java/com/shuzijun/leetcode/editor/cn/Q74SearchA2dMatrix.java deleted file mode 100644 index 6754a00..0000000 --- a/src/main/java/com/shuzijun/leetcode/editor/cn/Q74SearchA2dMatrix.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.shuzijun.leetcode.editor.cn; -/** -

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

    - - - -

    - -

    示例 1:

    - -
    -输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
    -输出:true
    -
    - -

    示例 2:

    - -
    -输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
    -输出:false
    -
    - -

    - -

    提示:

    - - -
    Related Topics
  • 数组
  • 二分查找
  • 矩阵

  • 👍 455
  • 👎 0
  • -*/ - -public class Q74SearchA2dMatrix{ - public static void main(String[] args) { - Solution solution = new Q74SearchA2dMatrix().new Solution(); - } - //leetcode submit region begin(Prohibit modification and deletion) -class Solution { - public boolean searchMatrix(int[][] matrix, int target) { - int m = matrix.length, n = matrix[0].length; - int start = 0, end = m * n - 1; - while (start <= end){ - int binary = (start + end + 1) / 2; - int i = matrix[binary/n][binary % n]; - if(i == target ){ - return true; - }else if (i