Pārlūkot izejas kodu

java experiment

邓心一 6 gadi atpakaļ
vecāks
revīzija
935fb3ff39

+ 1 - 0
.gitignore

@@ -1,2 +1,3 @@
+.idea
 .vscode
 .vscode
 node_modules
 node_modules

BIN
medium/_365/Solution.class


+ 57 - 0
medium/_365/Solution.java

@@ -0,0 +1,57 @@
+package _365;
+
+import java.util.HashSet;
+
+/**
+ * Error: StackOverFlow!!!
+ */
+class OldSolution {
+    public boolean canMeasureWater(int x, int y, int z) {
+        HashSet<String> set = new HashSet<>();
+        return canMeasureWaterIter(x, y, z, 0, 0, set);
+    }
+
+    /**
+     * @param _x  The water in bottle x
+     * @param _y  The water in bottle y
+     * @param set The set of old states
+     */
+    private boolean canMeasureWaterIter(int x, int y, int z, int _x, int _y, HashSet<String> set) {
+        boolean isDone = _x == z || _y == z || (_x + _y) == z;
+        if (isDone)
+            return true;
+        boolean isTried = set.contains(_x + ":" + _y);
+        if (isTried)
+            return false;
+        set.add(_x + ":" + _y);
+        int _x_x2y, _y_x2y, _x_y2x, _y_y2x;
+        int sum_xy = _x + _y;
+        if (sum_xy > x) {
+            _x_x2y = x;
+            _y_x2y = sum_xy - x;
+        } else {
+            _x_x2y = sum_xy;
+            _y_x2y = 0;
+        }
+        if (sum_xy > y) {
+            _y_y2x = y;
+            _x_y2x = sum_xy - y;
+        } else {
+            _y_y2x = sum_xy;
+            _x_y2x = 0;
+        }
+        return canMeasureWaterIter(x, y, z, x, _y, set) || canMeasureWaterIter(x, y, z, _x, y, set)
+                || canMeasureWaterIter(x, y, z, 0, _y, set) || canMeasureWaterIter(x, y, z, _x, 0, set)
+                || canMeasureWaterIter(x, y, z, _x_x2y, _y_x2y, set) || canMeasureWaterIter(x, y, z, _x_y2x, _y_y2x, set);
+    }
+
+    private static void testCanMeasureWater(int x, int y, int z) {
+        OldSolution solution = new OldSolution();
+        System.out.println(solution.canMeasureWater(x, y, z));
+    }
+
+    public static void main(String[] args) {
+        testCanMeasureWater(3, 5, 4);
+        testCanMeasureWater(2, 6, 5);
+    }
+}

BIN
medium/_402/Solution.class


+ 43 - 0
medium/_402/Solution.java

@@ -0,0 +1,43 @@
+package _402;
+
+class Solution {
+    public String removeKdigits(String num, int k) {
+        if (k <= 0)
+            return num;
+        int len = num.length();
+        StringBuilder res = new StringBuilder(len);
+        return removeKdigitsIter(num, len - k, -1, res);
+    }
+
+    /**
+     * @param left Number of the digits left
+     * @param head The head index of current sub string
+     * @param res  Temp sb to build result
+     */
+    private String removeKdigitsIter(String num, int left, int head, StringBuilder res) {
+        if (left <= 0)
+            return res.length() == 0 ? "0" : res.toString();
+        head++;
+        char min = num.charAt(head);
+        int tail = num.length() - left + 1;
+        for (int i = head; i < tail; i++) {
+            if (num.charAt(i) < min) {
+                min = num.charAt(i);
+                head = i;
+            }
+        }
+        boolean notLeading0 = res.length() != 0 || min != '0';
+        if (notLeading0)
+            res.append(min);
+        return removeKdigitsIter(num, left - 1, head, res);
+    }
+
+    private static void testRemoveKdigits(String num, int k) {
+        Solution solution = new Solution();
+        System.out.println(solution.removeKdigits(num, k));
+    }
+
+    public static void main(String[] args) {
+        testRemoveKdigits("4325643", 0);
+    }
+}

BIN
medium/_464/Solution.class


+ 7 - 3
medium/_464/Solution.java

@@ -7,10 +7,14 @@ class Solution {
         return canIWinIter(maxChoosableInteger, desiredTotal, 0, 0, true);
         return canIWinIter(maxChoosableInteger, desiredTotal, 0, 0, true);
     }
     }
 
 
+    private static void testCanIWin(int maxChoosableInteger, int desiredTotal) {
+        Solution solution = new Solution();
+        System.out.println(solution.canIWin(maxChoosableInteger, desiredTotal));
+    }
+
     public static void main(String[] args) {
     public static void main(String[] args) {
-        Solution s = new Solution();
-        System.out.println(s.canIWin(10, 11));
-        System.out.println(s.canIWin(10, 0));
+        testCanIWin(10, 11);
+        testCanIWin(2, 3);
     }
     }
 
 
     private boolean canIWinIter(int maxChoosableInteger, int desiredTotal, int intChoosen, int total,
     private boolean canIWinIter(int maxChoosableInteger, int desiredTotal, int intChoosen, int total,

BIN
medium/_468/Solution.class


+ 29 - 0
medium/_468/Solution.java

@@ -0,0 +1,29 @@
+package _468;
+
+import java.util.regex.Pattern;
+
+class Solution {
+    public String validIPAddress(String IP) {
+        Pattern ipv4 = Pattern.compile("^(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d).(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d).(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d).(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)$");
+        Pattern ipv6 = Pattern.compile("^[a-fA-F0-9]{1,4}:[a-fA-F0-9]{1,4}:[a-fA-F0-9]{1,4}:[a-fA-F0-9]{1,4}:[a-fA-F0-9]{1,4}:[a-fA-F0-9]{1,4}:[a-fA-F0-9]{1,4}:[a-fA-F0-9]{1,4}$");
+        boolean isV4 = ipv4.matcher(IP).matches();
+        boolean isV6 = ipv6.matcher(IP).matches();
+        if (isV4) {
+            return "IPv4";
+        } else if (isV6) {
+            return "IPv6";
+        }
+        return "Neither";
+    }
+
+    private static void testValidIPAddress(String IP) {
+        Solution solution = new Solution();
+        System.out.println(solution.validIPAddress(IP));
+    }
+
+    public static void main(String[] args) {
+        testValidIPAddress("172.16.254.1");
+        testValidIPAddress("265.16.254.1");
+        testValidIPAddress("0001:db8:85a3:0::8AFE:0370:7334");
+    }
+}

BIN
medium/_483/Solution.class


+ 16 - 0
medium/_483/Solution.java

@@ -0,0 +1,16 @@
+import java.math.BigInteger;
+
+class Solution {
+    public String smallestGoodBase(String n) {
+        BigInteger sum = new BigInteger(n);
+    }
+
+    private static void testSmallestGoodBase(String n) {
+        Solution solution = new Solution();
+        System.out.println(solution.smallestGoodBase(n));
+    }
+
+    public static void main(String[] args) {
+        testSmallestGoodBase("32141432");
+    }
+}