diff --git a/5.leetcode/src/com/fanxb/common/Q122.java b/5.leetcode/src/com/fanxb/common/Q122.java new file mode 100644 index 0000000..ecd4890 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q122.java @@ -0,0 +1,47 @@ +package com.fanxb.common; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +/** + * 题目地址: https://leetcode-cn.com/problems/partition-labels/ + * 解题思路:首先遍历一次字符串,记录每个字母最后出现的位置 + * 然后再遍历一遍,记录当前字符串的开始位置start,结束位置end. 当第i个字母的结束位置》end时end=第i个字母的结束位置,知道i=end说明当前位置为字符串的结束位置 + * + * @author fanxb + * Date: 2020/6/9 15:10 + */ +public class Q122 { + + public static int solution(int[] prices) { + int res = 0; + int start = -1, end = -1; + for (int i = 0; i < prices.length - 1; i++) { + if (prices[i + 1] > prices[i]) { + //说明是上坡 + if (start == -1) { + start = prices[i]; + } + end = prices[i + 1]; + } else if (prices[i + 1] < prices[i]) { + //说明开始下坡了 + if (end > start) { + res += end - start; + } + start = -1; + end = -1; + } + } + if (end > start) { + res += end - start; + } + return res; + } + + public static void main(String[] args) { + int[] prices = {7, 1, 5, 3, 6, 4}; + System.out.println(solution(prices)); + } +} diff --git a/5.leetcode/src/com/fanxb/interview/Q1663.java b/5.leetcode/src/com/fanxb/common/Q1663.java similarity index 91% rename from 5.leetcode/src/com/fanxb/interview/Q1663.java rename to 5.leetcode/src/com/fanxb/common/Q1663.java index d3d0041..14e39fc 100644 --- a/5.leetcode/src/com/fanxb/interview/Q1663.java +++ b/5.leetcode/src/com/fanxb/common/Q1663.java @@ -1,4 +1,4 @@ -package com.fanxb.interview; +package com.fanxb.common; /** * 具有给定数值的最小字符串 @@ -57,7 +57,8 @@ public class Q1663 { } public static void main(String[] args) { - System.out.println(new Q1663().translateNum(23100, 567226)); - System.out.println(new Q1663().translateNum1(23100, 567226)); + System.out.println(new Q1663().translateNum(1, 26)); + System.out.println(new Q1663().translateNum1(1, 26)); + System.out.println(4<<1); } } diff --git a/5.leetcode/src/com/fanxb/common/Q190.java b/5.leetcode/src/com/fanxb/common/Q190.java new file mode 100644 index 0000000..0db81ce --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q190.java @@ -0,0 +1,29 @@ +package com.fanxb.common; + +/** + * 具有给定数值的最小字符串 + *

+ * 题目地址:https://leetcode-cn.com/problems/reverse-bits + *

+ * 解题思路:简单的位运算 + * + * @author fanxb + * Date: 2020/6/9 15:10 + */ +public class Q190 { + + public static int solution(int n) { + int res = 0; + for (int i = 0; i < 32; i++, n = n >>> 1) { + res = res << 1 | (n & 1); + System.out.println(Integer.toBinaryString(res)); + } + return res; + } + + public static void main(String[] args) { + int num = 43261596; + System.out.println(Integer.toBinaryString(num)); + System.out.println(solution(num)); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q406.java b/5.leetcode/src/com/fanxb/common/Q406.java new file mode 100644 index 0000000..83a36e1 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q406.java @@ -0,0 +1,39 @@ +package com.fanxb.common; + +import java.util.Arrays; +import java.util.LinkedList; + +/** + * 题目地址: + * 解题思路: + * + * + * @author fanxb + * Date: 2020/6/9 15:10 + */ +public class Q406 { + + public static int[][] solution(int[][] people) { + Arrays.sort(people, (a, b) -> { + if (a[0] == b[0]) { + return Integer.compare(a[1], b[1]); + } else { + return Integer.compare(b[0], a[0]); + } + }); + LinkedList res = new LinkedList<>(); + for (int[] person : people) { + if (person[1] >= res.size()) { + res.add(person); + } else { + res.add(person[1], person); + } + } + return res.toArray(new int[res.size()][]); + } + + public static void main(String[] args) { + int[][] people = {{7, 0}, {4, 4}, {7, 1}, {5, 0}, {6, 1}, {5, 2}}; + System.out.println(Arrays.deepToString(solution(people))); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q435.java b/5.leetcode/src/com/fanxb/common/Q435.java new file mode 100644 index 0000000..b0eb650 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q435.java @@ -0,0 +1,34 @@ +package com.fanxb.common; + +import java.lang.reflect.AccessibleObject; +import java.util.Arrays; +import java.util.LinkedList; + +/** + * 地址: https://leetcode-cn.com/problems/non-overlapping-intervals + * + * @author fanxb + * Date: 2020/6/11 10:58 + */ +public class Q435 { + public static int solution(int[][] intervals) { + Arrays.sort(intervals, (a, b) -> a[1] - b[1]); + int count = 0, i = 0, j = 1; + while (j < intervals.length) { + if (intervals[i][1] <= intervals[j][0]) { + //说明第i个区间和第j个区间,所以将i移动到j,再和j+1比较 + i = j; + } else { + //说明第i个区间和第j个区间重复,干掉j,i再和j+1比较 + count++; + } + j++; + } + return count; + } + + public static void main(String[] args) { + int[][] arr = {{1, 4}, {2, 3}, {1, 8}}; + System.out.println(solution(arr)); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q452.java b/5.leetcode/src/com/fanxb/common/Q452.java new file mode 100644 index 0000000..c973c1a --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q452.java @@ -0,0 +1,36 @@ +package com.fanxb.common; + +import java.util.Arrays; + +/** + * 题目地址: https://leetcode-cn.com/problems/minimum-number-of-arrows-to-burst-balloons/ + * 解题思路:贪心算法,按照结束点排序, + * + * @author fanxb + * Date: 2020/6/9 15:10 + */ +public class Q452 { + + public static int solution(int[][] points) { + if (points.length == 0) { + return 0; + } + Arrays.sort(points, (a, b) -> Integer.compare(a[1], b[1])); + //第一根箭的位置是第一个气球的结束点 + int endNum = points[0][1], count = 1; + for (int[] point : points) { + if (point[0] > endNum) { + //当某个气球的开始点>箭的位置时说明需要一根新的箭了,箭位置为当前气球的结束点 + endNum = point[1]; + count++; + } + } + return count; + } + + public static void main(String[] args) { + int[][] s = {{-2147483646, -2147483645}, {2147483646, 2147483647}}; +// int[][] s = {{10, 16}, {2, 8}, {1, 6}, {7, 12}}; + System.out.println(solution(s)); + } +} diff --git a/5.leetcode/src/com/fanxb/interview/Q46.java b/5.leetcode/src/com/fanxb/common/Q46.java similarity index 95% rename from 5.leetcode/src/com/fanxb/interview/Q46.java rename to 5.leetcode/src/com/fanxb/common/Q46.java index dec5dfb..16fc1a7 100644 --- a/5.leetcode/src/com/fanxb/interview/Q46.java +++ b/5.leetcode/src/com/fanxb/common/Q46.java @@ -1,4 +1,4 @@ -package com.fanxb.interview; +package com.fanxb.common; /** * Created with IntelliJ IDEA diff --git a/5.leetcode/src/com/fanxb/common/Q605.java b/5.leetcode/src/com/fanxb/common/Q605.java new file mode 100644 index 0000000..e78ac01 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q605.java @@ -0,0 +1,44 @@ +package com.fanxb.common; + +/** + * 具有给定数值的最小字符串 + *

+ * 题目地址:https://leetcode-cn.com/problems/smallest-string-with-a-given-numeric-value/ + *

+ * 解题思路: + * 1. 贪心算法,字典序最小意思就是要把尽量把小的字母放前面,也可以说是把尽量大的数字放后面,因此有两种做法。 + * a. 每次放尽量小的字母在前面,然后判断后面的全放最大的字母能否满足(大于等于)k-已存在的数字和.如果能满足再放第二个,如果不满足就把当前的字母加大1,再判断直到满足要求 + * + * @author fanxb + * Date: 2020/6/9 15:10 + */ +public class Q605 { + + public static boolean solution(int[] flowerbed, int n) { + //第i个位置当成花坛的起点,前一个位置肯定没种花 + int i = 0; + int count = 0; + while (i < flowerbed.length) { + if (flowerbed[i] == 1) { + //第i个有花,下一个可能有花的位置肯定是i+2 + i += 2; + } else { + //第i个位置没花 + if (i == flowerbed.length - 1 || flowerbed[i + 1] == 0) { + //i是最后一个位置 或者 i+1没花 + count++; + i += 2; + } else { + //第i个位置没花,第i+1个位置有花,说明下一个种花的起点至少是i+3 + i += 3; + } + } + } + return count >= n; + } + + public static void main(String[] args) { + int[] s = {1, 0, 0, 0, 0, 1}; + System.out.println(solution(s, 2)); + } +} diff --git a/5.leetcode/src/com/fanxb/interview/Q61.java b/5.leetcode/src/com/fanxb/common/Q61.java similarity index 98% rename from 5.leetcode/src/com/fanxb/interview/Q61.java rename to 5.leetcode/src/com/fanxb/common/Q61.java index 95c6e34..57ff0f1 100644 --- a/5.leetcode/src/com/fanxb/interview/Q61.java +++ b/5.leetcode/src/com/fanxb/common/Q61.java @@ -1,4 +1,4 @@ -package com.fanxb.interview; +package com.fanxb.common; /** * 旋转链表 diff --git a/5.leetcode/src/com/fanxb/interview/Q64.java b/5.leetcode/src/com/fanxb/common/Q64.java similarity index 91% rename from 5.leetcode/src/com/fanxb/interview/Q64.java rename to 5.leetcode/src/com/fanxb/common/Q64.java index 57c4f15..43d02d5 100644 --- a/5.leetcode/src/com/fanxb/interview/Q64.java +++ b/5.leetcode/src/com/fanxb/common/Q64.java @@ -1,4 +1,4 @@ -package com.fanxb.interview; +package com.fanxb.common; /** * Created with IntelliJ IDEA diff --git a/5.leetcode/src/com/fanxb/common/Q665.java b/5.leetcode/src/com/fanxb/common/Q665.java new file mode 100644 index 0000000..c7691a5 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q665.java @@ -0,0 +1,52 @@ +package com.fanxb.common; + +import java.util.Arrays; +import java.util.LinkedList; + +/** + * 题目地址:https://leetcode-cn.com/problems/non-decreasing-array/submissions/ + * 解题思路:贪心算法,遍历数组,每次调整都要尽量用最小的值 + * + * @author fanxb + * Date: 2020/6/9 15:10 + */ +public class Q665 { + + + public static boolean betterSolution(int[] nums) { + if (nums.length <= 2) { + return true; + } + //是否首次调整 + boolean firstAdjust = true; + for (int i = 0; i < nums.length - 1; i++) { + if (nums[i] > nums[i + 1]) { + if (i == 0) { + //首个节点就不满足只能让nums[0]=nums[1]一种调整办法 + firstAdjust = false; + nums[0] = nums[1]; + } else { + //不为首个节点 + if (!firstAdjust) { + return false; + } + if (nums[i + 1] < nums[i - 1]) { + //如果i+1的值小于i-1的值只能将i+1的值调大 + nums[i + 1] = nums[i]; + } else { + //否则将nums[i]调小是最好的 + nums[i] = nums[i - 1]; + } + firstAdjust = false; + } + } + } + return true; + } + + + public static void main(String[] args) { + int[] nums = {-1, 4, 2, 3}; + System.out.println(betterSolution(nums)); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q74.java b/5.leetcode/src/com/fanxb/common/Q74.java new file mode 100644 index 0000000..cf26063 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q74.java @@ -0,0 +1,34 @@ +package com.fanxb.common; + +import java.util.Arrays; + +/** + * 具有给定数值的最小字符串 + * 题目地址: https://leetcode-cn.com/problems/search-a-2d-matrix/ + * 解题思路:两次二分即可,第一次二分列找到所在行,然后二分所在行找到是否存在目标值(由于行列数少于100,直接搜索问题也不大) + * + * + * @author fanxb + * Date: 2020/6/9 15:10 + */ +public class Q74 { + + public static boolean solution(int[][] matrix, int target) { + //m行数,n列数 + int m = matrix.length, n = matrix[0].length; + if (matrix[0][0] > target || matrix[m - 1][n - 1] < target) { + return false; + } + for (int i = 1; i < m; i++) { + if (matrix[i][0] > target) { + return Arrays.stream(matrix[i - 1]).anyMatch(item -> item == target); + } + } + return Arrays.stream(matrix[m - 1]).anyMatch(item -> item == target); + } + + public static void main(String[] args) { + int[][] s = {{1, 3, 5, 7}, {10, 11, 16, 20}, {23, 30, 34, 6}}; + System.out.println(solution(s, 3)); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q763.java b/5.leetcode/src/com/fanxb/common/Q763.java new file mode 100644 index 0000000..d5ead8d --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q763.java @@ -0,0 +1,41 @@ +package com.fanxb.common; + +import java.util.*; + +/** + * 题目地址: https://leetcode-cn.com/problems/partition-labels/ + * 解题思路:首先遍历一次字符串,记录每个字母最后出现的位置 + * 然后再遍历一遍,记录当前字符串的开始位置start,结束位置end. 当第i个字母的结束位置》end时end=第i个字母的结束位置,知道i=end说明当前位置为字符串的结束位置 + * + * @author fanxb + * Date: 2020/6/9 15:10 + */ +public class Q763 { + + public static List solution(String S) { + List res = new LinkedList<>(); + Map map = new HashMap<>(24); + char[] chars = new char[S.length()]; + for (int i = 0; i < chars.length; i++) { + chars[i] = S.charAt(i); + map.put(chars[i], i); + } + int start = 0, end = map.get(chars[0]), temp; + for (int i = 0; i < chars.length; i++) { + if ((temp = map.get(chars[i])) > end) { + //中间的某个点最远出现的位置大于end,说明当前字符串的结束点至少为end + end = temp; + } else if (i == end) { + res.add(end - start + 1); + //进行下一段的搜索 + start = end + 1; + } + } + return res; + } + + public static void main(String[] args) { + String S = "eaaaabaaec"; + System.out.println(solution(S)); + } +} diff --git a/5.leetcode/src/com/fanxb/interview/Q83.java b/5.leetcode/src/com/fanxb/common/Q83.java similarity index 93% rename from 5.leetcode/src/com/fanxb/interview/Q83.java rename to 5.leetcode/src/com/fanxb/common/Q83.java index e5661d6..bb4a456 100644 --- a/5.leetcode/src/com/fanxb/interview/Q83.java +++ b/5.leetcode/src/com/fanxb/common/Q83.java @@ -1,4 +1,4 @@ -package com.fanxb.interview; +package com.fanxb.common; /** * TODO 类描述 @@ -41,7 +41,7 @@ public class Q83 { * 递归版块 * * @param head head - * @return com.fanxb.interview.Q83.ListNode + * @return com.fanxb.common.Q83.ListNode * @author fanxb * @date 2021/3/26 **/