Bläddra i källkod

#feat:leetcode p1980题题解

yangyi 2 veckor sedan
förälder
incheckning
2b27053664
2 ändrade filer med 119 tillägg och 0 borttagningar
  1. 75 0
      src/leetcode/p1980/Solution.java
  2. 44 0
      src/leetcode/p1980/SolutionTest.java

+ 75 - 0
src/leetcode/p1980/Solution.java

@@ -0,0 +1,75 @@
+package leetcode.p1980;
+
+import java.util.HashSet;
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ * @ProjectName: LeetCode
+ * @FileName: Solution
+ * @Author: 杨逸
+ * @Data:2026/3/8 14:52
+ * @Description:    https://leetcode.cn/problems/find-unique-binary-string/
+ * 1980. 找出不同的二进制字符串
+ */
+public class Solution {
+
+    public String findDifferentBinaryString(String[] nums) {
+        return hashSolution(nums);
+    }
+
+    private String binaryString2intSolution(String[] nums){
+        /**
+         * 灵神的方法1
+         * 因为n最大为16,所有可以将二进制字符串转为int
+         * 然后遍历int,判断是否出现
+         */
+        Set<Integer> set  =new HashSet<>();
+        for (String num : nums) {
+            set.add(Integer.parseInt(num,2));
+        }
+        int ans = 0;
+        while (true){
+            if (!set.contains(ans)) {
+                break;
+            }
+            ans++;
+        }
+        String binaryString = Integer.toBinaryString(ans);
+        return "0".repeat(set.size() - binaryString.length()) + binaryString;
+    }
+
+    private Set<String> set  = new HashSet<>();
+    private char[] ans;
+    /**
+     * 回溯枚举字符串,哈希判断是否出现
+     * @param nums
+     * @return
+     */
+    private String hashSolution(String[] nums){
+        ans = new char[nums.length];
+        for (String num : nums) {
+            set.add(num);
+        }
+        return dfs(nums,0);
+    }
+
+    private String dfs(String[] nums,int index) {
+        if (index >= nums.length){
+            String temp = new String(ans);
+            if (!set.contains(temp)){
+                return temp;
+            }
+            return null;
+        }
+        //取0
+        ans[index] = '0';
+        String dfs = dfs(nums, index + 1);
+        if(Objects.nonNull(dfs)){
+            return dfs;
+        }
+        //取1
+        ans[index] = '1';
+        return dfs(nums, index + 1);
+    }
+}

+ 44 - 0
src/leetcode/p1980/SolutionTest.java

@@ -0,0 +1,44 @@
+package leetcode.p1980;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * @ProjectName: LeetCode
+ * @FileName: SolutionTest
+ * @Author: 杨逸
+ * @Data:2026/3/8 15:01
+ * @Description:
+ */
+@RunWith(Parameterized.class)
+public class SolutionTest {
+    public static final Solution solution = new Solution();
+    private String[] nums;
+    private String expected;
+
+    public SolutionTest(String[] nums, String expected) {
+        this.nums = nums;
+        this.expected = expected;
+    }
+    @Parameterized.Parameters
+    public static Collection<Object[]> data() {
+        return Arrays.asList(
+                new Object[][]{
+                        {new String[]{"01","10"},"00"},
+                        {new String[]{"00","01"},"11"}
+                }
+        );
+    }
+
+    @Test
+    public void findDifferentBinaryString() {
+        String result = solution.findDifferentBinaryString(nums);
+        assertEquals(expected, result);
+    }
+}