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
+ * 题目地址: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