aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--find_unique_binary_str.c27
-rw-r--r--find_unique_binary_str.py14
2 files changed, 16 insertions, 25 deletions
diff --git a/find_unique_binary_str.c b/find_unique_binary_str.c
index e7e0541..533df0a 100644
--- a/find_unique_binary_str.c
+++ b/find_unique_binary_str.c
@@ -7,25 +7,22 @@
* 'nums'. if there are multiple answers, you may return any of them.
*/
-// char *findDifferentBinaryString(char **nums, int nums_size) {
-char *findDifferentBinaryString(int nums_size,
- char nums[nums_size][nums_size]) {
- char *ans = malloc(nums_size + 1);
+char *findDifferentBinaryString(char **nums, int numsSize) {
+ char *ans = (char *)malloc((numsSize + 1) * sizeof(char));
int j = 0;
- for (int i = 0; i < nums_size; i++) {
- if (nums[i][i] == '0')
- ans[j++] = '1';
- else
- ans[j++] = '0';
- }
+ for (int i = 0; i < numsSize; i++)
+ ans[j++] = nums[i][i] == '0' ? '1' : '0';
ans[j] = '\0';
return j ? ans : "";
}
int main() {
- char n1[2][2] = {{"01"}, {"10"}}, n2[2][2] = {{"00"}, {"01"}},
- n3[3][3] = {{"111"}, {"011"}, {"001"}};
- printf("%s\n", findDifferentBinaryString(2, n1)); // expect: 11
- printf("%s\n", findDifferentBinaryString(2, n2)); // expect: 11
- printf("%s\n", findDifferentBinaryString(3, n3)); // expect: 101
+ char *n1[] = {"01", "10"}, *n2[] = {"00", "01"},
+ *n3[] = {"111", "011", "001"};
+ printf("%s\n",
+ findDifferentBinaryString((char **)n1, ARRAY_SIZE(n1))); // expect: 11
+ printf("%s\n",
+ findDifferentBinaryString((char **)n2, ARRAY_SIZE(n2))); // expect: 11
+ printf("%s\n",
+ findDifferentBinaryString((char **)n3, ARRAY_SIZE(n3))); // expect: 101
}
diff --git a/find_unique_binary_str.py b/find_unique_binary_str.py
index 6866870..681369b 100644
--- a/find_unique_binary_str.py
+++ b/find_unique_binary_str.py
@@ -13,17 +13,11 @@ class Solution(object):
:type nums: List[str]
:rtype: str
"""
- s = set([int(i, 2) for i in nums])
- max_len = len(nums[0])
- for i in range(pow(2, max_len)):
- if i not in s:
- r = bin(i)[2:]
- return "0" * (max_len - len(r)) + r
- return "-1"
+ return "".join(["1" if num[i] == "0" else "0" for i, num in enumerate(nums)])
if __name__ == "__main__":
obj = Solution()
- print(obj.findDifferentBinaryString(["01", "10"])) # expect: 11
- print(obj.findDifferentBinaryString(["00", "01"])) # expect: 11
- print(obj.findDifferentBinaryString(["111", "011", "001"])) # expect: 101
+ print(obj.findDifferentBinaryString(nums=["01", "10"]))
+ print(obj.findDifferentBinaryString(nums=["00", "01"]))
+ print(obj.findDifferentBinaryString(nums=["111", "011", "001"]))