This commit is contained in:
fanxb 2021-03-30 18:35:09 +08:00
parent ac0083cb91
commit 0ff3d79282
14 changed files with 365 additions and 8 deletions

View File

@ -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));
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,29 @@
package com.fanxb.common;
/**
* 具有给定数值的最小字符串
* <p>
* 题目地址https://leetcode-cn.com/problems/reverse-bits
* <p>
* 解题思路简单的位运算
*
* @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));
}
}

View File

@ -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<int[]> 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)));
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -1,4 +1,4 @@
package com.fanxb.interview;
package com.fanxb.common;
/**
* Created with IntelliJ IDEA

View File

@ -0,0 +1,44 @@
package com.fanxb.common;
/**
* 具有给定数值的最小字符串
* <p>
* 题目地址https://leetcode-cn.com/problems/smallest-string-with-a-given-numeric-value/
* <p>
* 解题思路
* 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));
}
}

View File

@ -1,4 +1,4 @@
package com.fanxb.interview;
package com.fanxb.common;
/**
* 旋转链表

View File

@ -1,4 +1,4 @@
package com.fanxb.interview;
package com.fanxb.common;
/**
* Created with IntelliJ IDEA

View File

@ -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));
}
}

View File

@ -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));
}
}

View File

@ -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<Integer> solution(String S) {
List<Integer> res = new LinkedList<>();
Map<Character, Integer> 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));
}
}

View File

@ -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
**/