***砍竹子I:
动态规划:
public int cuttingBamboo(int bamboo_len) {int[] dp=new int[bamboo_len+1];dp[0]=0;dp[1]=1;for (int i = 2; i <=bamboo_len ; i++) {int max=0;for (int j = 1; j < i; j++) {max=Math.max(max,Math.max(j*(i-j),j*dp[i-j]));}dp[i]=max;}return dp[bamboo_len];}
数学解法:
public int cuttingBamboo2(int bamboo_len) {if (bamboo_len<=3){return bamboo_len-1;}int a=bamboo_len/3;int b=bamboo_len%3;if (b==0){return (int) Math.pow(3,a);}else if(b==1){return (int) Math.pow(3,(a-1))*4;}else{return (int) Math.pow(3,a)*2;}}
***砍竹子II:
***零钱兑换:
. - 力扣(LeetCode)
public int coinChange(int[] coins, int amount) {int[] dp = new int[amount + 1];Arrays.fill(dp,amount+1);dp[0]=0;for (int coin : coins) {for (int i = coin; i <=amount; i++) {dp[i]=Math.min(dp[i],1+dp[i-coin]);}}return dp[amount]>amount?-1:dp[amount];}
**完全平方数:
. - 力扣(LeetCode)
public int numSquares(int n) {int[] dp=new int[n+1];Arrays.fill(dp,n+1);int d =(int)Math.sqrt(n);int[] a=new int[d];for (int i=1; i <=d; i++) {a[i-1]=i*i;}dp[0]=0;for (int i : a) {for (int j = i; j <=n; j++) {dp[j]=Math.min(dp[j],1+dp[j-i]);}}return dp[n];}
**打家劫舍II:
. - 力扣(LeetCode)
public int rob(int[] nums) {if (nums.length==1){return nums[0];}if (nums.length==2){return Math.max(nums[0],nums[1]);}return Math.max(robRange(0,nums.length-2,nums),robRange(1,nums.length-1,nums));}public int robRange(int start,int end,int nums[]){int first=nums[start],second=Math.max(nums[start],nums[start+1]);for (int i = start+2; i <=end; i++) {int temp=second;second=Math.max(first+nums[i],second);first=temp;}return second;}
*******比特位计数:
. - 力扣(LeetCode)
public int[] countBits(int n) {int[] dp=new int[n+1];for (int i = 1; i <=n; i++) {dp[i]=dp[i&(i-1)]+1;}return dp;}
public int[] countBits2(int n) {int[] dp=new int[n+1];for (int i = 1; i <=n; i++) {dp[i]=dp[i>>1]+(i&1);}return dp;}
**砝码称重:
用户登录
public class Main {public static void main(String[] args) {Scanner scanner=new Scanner(System.in);int n=scanner.nextInt();int[] w=new int[n];Set<Integer> set=new HashSet<>();for (int i = 0; i < n; i++) {w[i]=scanner.nextInt();}set.add(w[0]);set.add(0);Set<Integer> tempSet=new HashSet<>();for (int i = 1; i < n; i++) {for (Integer num : set) {tempSet.add(num+w[i]);tempSet.add(Math.abs(num-w[i]));}set.addAll(tempSet);}System.out.println(set.size()-1);}
}