From 1b4e864ca9069ae94916d965129548ccf9ead253 Mon Sep 17 00:00:00 2001 From: fanxb Date: Tue, 15 Jun 2021 14:17:00 +0800 Subject: [PATCH] add --- 5.leetcode/src/com/fanxb/common/Q1449.java | 41 ++++++++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q278.java | 30 ++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q374.java | 31 ++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q852.java | 35 ++++++++++++++++++ 4 files changed, 137 insertions(+) create mode 100644 5.leetcode/src/com/fanxb/common/Q1449.java create mode 100644 5.leetcode/src/com/fanxb/common/Q278.java create mode 100644 5.leetcode/src/com/fanxb/common/Q374.java create mode 100644 5.leetcode/src/com/fanxb/common/Q852.java diff --git a/5.leetcode/src/com/fanxb/common/Q1449.java b/5.leetcode/src/com/fanxb/common/Q1449.java new file mode 100644 index 0000000..79565ca --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q1449.java @@ -0,0 +1,41 @@ +package com.fanxb.common; + +import java.util.Arrays; + +/** + * Created with IntelliJ IDEA + * x 的平方根 + * 地址: https://leetcode-cn.com/problems/sqrtx/ + * 思路: 双路规避排序查找 + * + * + * @author fanxb + * Date: 2020/6/11 9:56 + */ +public class Q1449 { + public String largestNumber(int[] cost, int target) { + int[] f = new int[target + 1]; + Arrays.fill(f, Integer.MIN_VALUE); + f[0] = 0; + for (int i = 1; i <= 9; i++) { + int u = cost[i - 1]; + for (int j = u; j <= target; j++) { + f[j] = Math.max(f[j], f[j - u] + 1); + } + } + if (f[target] < 0) return "0"; + StringBuilder ans = new StringBuilder(); + for (int i = 9, j = target; i >= 1; i--) { + int u = cost[i - 1]; + while (j >= u && f[j] == f[j - u] + 1) { + ans.append(i); + j -= u; + } + } + return ans.toString(); + } + + public static void main(String[] args) { + System.out.println(new Q1449().largestNumber(new int[]{4,3,2,5,6,7,2,5,5},9)); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q278.java b/5.leetcode/src/com/fanxb/common/Q278.java new file mode 100644 index 0000000..3227a7e --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q278.java @@ -0,0 +1,30 @@ +package com.fanxb.common; + +/** + * Created with IntelliJ IDEA + * + * @author fanxb + * Date: 2020/6/11 9:56 + */ +public class Q278 { + private boolean isBadVersion(int n){ + return true; + } + + public int firstBadVersion(int n) { + int left=1,right=n; + while (left>1); + if(isBadVersion(mid)){ + right=mid; + }else{ + left=mid+1; + } + } + return left; + } + + public static void main(String[] args) { + System.out.println(new Q278().firstBadVersion(16)); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q374.java b/5.leetcode/src/com/fanxb/common/Q374.java new file mode 100644 index 0000000..85dcb03 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q374.java @@ -0,0 +1,31 @@ +package com.fanxb.common; + +/** + * 两数相加 + * + * @author fanxb + * @date 2021/6/1 + **/ +public class Q374 { + private static int target=6; + private static int guess(int num){ + return Integer.compare(target, num); + } + + public int guessNumber(int n) { + int l=1,r=n; + while (larr[mid+1]){ + //说明是递减的 + r=mid; + }else{ + //说明是递增的 + l=mid+1; + } + } + return l; + } + + public static void main(String[] args) { + System.out.println(new Q852().peakIndexInMountainArray(new int[]{0,10,5,2})); + } +}