From cd363ddc8eaf061f50f6b2bb3138475aa8c3d4f6 Mon Sep 17 00:00:00 2001 From: fanxb Date: Mon, 21 Jun 2021 16:33:00 +0800 Subject: [PATCH] add --- 5.leetcode/src/com/fanxb/common/Q401.java | 39 +++++++++++++++++++++++ 5.leetcode/src/com/fanxb/common/Q877.java | 38 ++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 5.leetcode/src/com/fanxb/common/Q401.java create mode 100644 5.leetcode/src/com/fanxb/common/Q877.java diff --git a/5.leetcode/src/com/fanxb/common/Q401.java b/5.leetcode/src/com/fanxb/common/Q401.java new file mode 100644 index 0000000..b2a2173 --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q401.java @@ -0,0 +1,39 @@ +package com.fanxb.common; + +import com.sun.tools.jconsole.JConsoleContext; + +import java.util.*; + +/** + * 两数相加 + * + * @author fanxb + * @date 2021/6/1 + **/ +public class Q401 { + private static Map> map=new HashMap<>(10); + static { + for(int i=0;i<=11;i++){ + for(int j=0;j<=59;j++){ + LinkedList linkedList = map.computeIfAbsent(count(i)+count(j),k->new LinkedList<>()); + linkedList.add(i+":"+(j<10?"0"+j:j)); + } + } + } + private static int count(int x){ + int count=0; + while (x>0){ + x-=x&(-x); + count++; + } + return count; + } + + public List readBinaryWatch(int turnedOn) { + return map.getOrDefault(turnedOn,new LinkedList()); + } + + public static void main(String[] args) { + new Q401().readBinaryWatch(1); + } +} diff --git a/5.leetcode/src/com/fanxb/common/Q877.java b/5.leetcode/src/com/fanxb/common/Q877.java new file mode 100644 index 0000000..02fb30a --- /dev/null +++ b/5.leetcode/src/com/fanxb/common/Q877.java @@ -0,0 +1,38 @@ +package com.fanxb.common; + +/** + * 两数相加 + * + * @author fanxb + * @date 2021/6/1 + **/ +public class Q877 { + public boolean stoneGame(int[] piles) { + int l=0,r=piles.length-1; + long a=0,b=0; + //是否a选择 + boolean aChange=true; + while (l<=r){ + int res =piles[r]-piles[l]; + if (res <= 0) { + //说明左边的不小于右边的 + if(aChange){ + a+=piles[l++]; + }else{ + b+=piles[l++]; + } + }else{ + if(aChange){ + a+=piles[r--]; + }else{ + b+=piles[r--]; + } + } + aChange=!aChange; + } + return a>b; + } + public static void main(String[] args) { + System.out.println(new Q877().stoneGame(new int[]{5,3,4,5})); + } +}