add
This commit is contained in:
parent
7d263d1735
commit
255999758c
39
5.leetcode/src/com/fanxb/common/Q134.java
Normal file
39
5.leetcode/src/com/fanxb/common/Q134.java
Normal file
@ -0,0 +1,39 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
/**
|
||||
* 分割回文串
|
||||
*
|
||||
* @author fanxb
|
||||
* @date 2022/3/9 10:10
|
||||
*/
|
||||
public class Q134 {
|
||||
|
||||
public int canCompleteCircuit(int[] gas, int[] cost) {
|
||||
int length = gas.length;
|
||||
for (int i = 0; i < length; i++) {
|
||||
gas[i] = gas[i] - cost[i];
|
||||
}
|
||||
for (int i = 0; i < length; i++) {
|
||||
//try from i
|
||||
int sum = 0;
|
||||
boolean ok = true;
|
||||
for (int j = 0; j < length; j++) {
|
||||
sum += gas[(i + j) % length];
|
||||
if (sum < 0) {
|
||||
ok = false;
|
||||
i += j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ok) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q134().canCompleteCircuit(new int[]{1, 2, 3, 4, 5}, new int[]{3, 4, 5, 1, 2}));
|
||||
}
|
||||
}
|
@ -38,9 +38,32 @@ public class Q135 {
|
||||
return count;
|
||||
}
|
||||
|
||||
public static int candy1(int[] ratings) {
|
||||
//every one set one candy
|
||||
int length = ratings.length;
|
||||
int[] res = new int[length];
|
||||
Arrays.fill(res, 1);
|
||||
for (int i = 1; i < length; i++) {
|
||||
//从左到右遍历,如果下一个人分数更高那么多发一颗糖
|
||||
if (ratings[i] > ratings[i - 1]) {
|
||||
res[i] = res[i - 1] + 1;
|
||||
}
|
||||
}
|
||||
for (int i = length - 2; i >= 0; i--) {
|
||||
if (ratings[i] > ratings[i + 1] && res[i] <= res[i + 1]) {
|
||||
res[i] = res[i + 1] + 1;
|
||||
}
|
||||
}
|
||||
int sum = 0;
|
||||
for (int temp : res) {
|
||||
sum += temp;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] s = {1, 2, 87, 87, 87, 2, 1};
|
||||
int[] s = {1, 3, 4, 5, 2};
|
||||
System.out.println(Arrays.toString(s));
|
||||
System.out.println(candy(s));
|
||||
System.out.println(candy1(s));
|
||||
}
|
||||
}
|
||||
|
33
5.leetcode/src/com/fanxb/common/Q238.java
Normal file
33
5.leetcode/src/com/fanxb/common/Q238.java
Normal file
@ -0,0 +1,33 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class Q238 {
|
||||
|
||||
public int[] productExceptSelf(int[] nums) {
|
||||
int length = nums.length;
|
||||
if (length <= 1) return nums;
|
||||
int[] left = new int[length];
|
||||
left[0] = nums[0];
|
||||
for (int i = 1; i < length; i++) {
|
||||
left[i] = left[i - 1] * nums[i];
|
||||
}
|
||||
int[] right = new int[length];
|
||||
right[length - 1] = nums[length - 1];
|
||||
for (int i = length - 2; i >= 0; i--) {
|
||||
right[i] = right[i + 1] * nums[i];
|
||||
}
|
||||
for (int i = 1; i < length - 1; i++) {
|
||||
nums[i] = left[i - 1] * right[i + 1];
|
||||
}
|
||||
nums[0] = right[1];
|
||||
nums[length - 1] = left[length - 2];
|
||||
return nums;
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(Arrays.toString(new Q238().productExceptSelf(new int[]{1, 0})));
|
||||
}
|
||||
}
|
28
5.leetcode/src/com/fanxb/common/Q274.java
Normal file
28
5.leetcode/src/com/fanxb/common/Q274.java
Normal file
@ -0,0 +1,28 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA
|
||||
*
|
||||
* @author fanxb
|
||||
* Date: 2020/6/11 9:56
|
||||
*/
|
||||
public class Q274 {
|
||||
public int hIndex(int[] citations) {
|
||||
Arrays.sort(citations);
|
||||
int length = citations.length, count = 0;
|
||||
for (int i = length - 1; i >= 0; i--) {
|
||||
if (citations[i] < count + 1) {
|
||||
break;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q274().hIndex(new int[]{1, 3, 1}));
|
||||
}
|
||||
}
|
@ -67,9 +67,31 @@ public class Q42 {
|
||||
return res;
|
||||
}
|
||||
|
||||
public int trap2(int[] height) {
|
||||
int length = height.length;
|
||||
//i左边的最大高度
|
||||
int[] left = new int[length];
|
||||
left[0] = 0;
|
||||
//i右边的最大高度
|
||||
int[] right = new int[length];
|
||||
right[length - 1] = 0;
|
||||
for (int i = 1; i < length; i++) {
|
||||
left[i] = Math.max(left[i - 1], height[i - 1]);
|
||||
right[length - i - 1] = Math.max(right[length - i], height[length - i]);
|
||||
}
|
||||
int res = 0;
|
||||
for (int i = 1; i < length - 1; i++) {
|
||||
int temp = Math.min(left[i], right[i]) - height[i];
|
||||
if (temp > 0) {
|
||||
res += temp;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q42().trap(new int[]{0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}));
|
||||
System.out.println(new Q42().trap1(new int[]{0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}));
|
||||
System.out.println(new Q42().trap2(new int[]{0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}));
|
||||
}
|
||||
}
|
||||
|
36
5.leetcode/src/com/fanxb/common/Q55.java
Normal file
36
5.leetcode/src/com/fanxb/common/Q55.java
Normal file
@ -0,0 +1,36 @@
|
||||
package com.fanxb.common;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
* Created with IntelliJ IDEA
|
||||
*
|
||||
* @author fanxb
|
||||
* Date: 2020/6/9 15:10
|
||||
*/
|
||||
public class Q55 {
|
||||
|
||||
public boolean canJump(int[] nums) {
|
||||
int n = nums.length;
|
||||
if (n == 1) {
|
||||
return true;
|
||||
}
|
||||
int val = nums[0];
|
||||
for (int i = 1; i < nums.length; i++) {
|
||||
if (val < i) {
|
||||
//说明走不到这一步,不用继续了
|
||||
return false;
|
||||
}
|
||||
val = Math.max(val, i + nums[i]);
|
||||
}
|
||||
return val >= n - 1;
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(new Q55().canJump(new int[]{1, 0, 1, 0}));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user