From 255999758cc3035e31bbb26212a07fb9aec31476 Mon Sep 17 00:00:00 2001 From: fleyx Date: Tue, 19 Dec 2023 17:08:51 +0800 Subject: [PATCH] add --- 5.leetcode/src/com/fanxb/common/Q134.java | 39 +++++++++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q135.java | 27 ++++++++++++++-- 5.leetcode/src/com/fanxb/common/Q238.java | 33 +++++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q274.java | 28 ++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q42.java | 22 +++++++++++++ 5.leetcode/src/com/fanxb/common/Q55.java | 36 +++++++++++++++++++++ 6 files changed, 183 insertions(+), 2 deletions(-) create mode 100644 5.leetcode/src/com/fanxb/common/Q134.java create mode 100644 5.leetcode/src/com/fanxb/common/Q238.java create mode 100644 5.leetcode/src/com/fanxb/common/Q274.java create mode 100644 5.leetcode/src/com/fanxb/common/Q55.java diff --git a/5.leetcode/src/com/fanxb/common/Q134.java b/5.leetcode/src/com/fanxb/common/Q134.java new file mode 100644 index 0000000..4bb2919 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q134.java @@ -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})); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q135.java b/5.leetcode/src/com/fanxb/common/Q135.java index 387e2ac..3e8834d 100644 --- a/5.leetcode/src/com/fanxb/common/Q135.java +++ b/5.leetcode/src/com/fanxb/common/Q135.java @@ -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)); } } diff --git a/5.leetcode/src/com/fanxb/common/Q238.java b/5.leetcode/src/com/fanxb/common/Q238.java new file mode 100644 index 0000000..774a7d4 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q238.java @@ -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}))); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q274.java b/5.leetcode/src/com/fanxb/common/Q274.java new file mode 100644 index 0000000..6f1b02c --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q274.java @@ -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})); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q42.java b/5.leetcode/src/com/fanxb/common/Q42.java index 4ee4926..5cfba89 100644 --- a/5.leetcode/src/com/fanxb/common/Q42.java +++ b/5.leetcode/src/com/fanxb/common/Q42.java @@ -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})); } } diff --git a/5.leetcode/src/com/fanxb/common/Q55.java b/5.leetcode/src/com/fanxb/common/Q55.java new file mode 100644 index 0000000..7d48575 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q55.java @@ -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})); + } +}