From 332e04ead1913e8222349be203ceb559032b845d Mon Sep 17 00:00:00 2001 From: Hinsteny Date: Thu, 20 Jun 2019 13:48:00 +0800 Subject: [PATCH 1/2] Add Solution.py for 0003.Longest Substring Without Repeating Characters --- .../Solution.py | 12 ++++++++++++ solution/README.md | 3 ++- 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 solution/0003.Longest Substring Without Repeating Characters/Solution.py diff --git a/solution/0003.Longest Substring Without Repeating Characters/Solution.py b/solution/0003.Longest Substring Without Repeating Characters/Solution.py new file mode 100644 index 0000000000000..fc699d1e46300 --- /dev/null +++ b/solution/0003.Longest Substring Without Repeating Characters/Solution.py @@ -0,0 +1,12 @@ +class Solution: + def lengthOfLongestSubstring(self, s: str): + max = 0 + length = len(s) + substr = "" + for i in range(0, length): + index = substr.find(s[i]) + if index > -1: + substr = substr[index + 1:] + substr += s[i] + max = (max if (max > len(substr)) else len(substr)) + return max \ No newline at end of file diff --git a/solution/README.md b/solution/README.md index 706764aa96042..cefffab4adb56 100644 --- a/solution/README.md +++ b/solution/README.md @@ -25,7 +25,8 @@ │   ├── README.md │   ├── Solution.go │   ├── Solution.java -│   └── Solution.js +│   ├── Solution.js +│   └── Solution.py ├── 0004.Median of Two Sorted Arrays │   ├── README.md │   ├── Solution.cpp From c51b492bb2e62b7c92f773b9ef5ce88102d034d6 Mon Sep 17 00:00:00 2001 From: Hinsteny Date: Thu, 20 Jun 2019 14:04:03 +0800 Subject: [PATCH 2/2] Add Solution.py for 0421.Maximum XOR of Two Numbers in an Array --- .../Solution.py | 19 +++++++++++++++++++ solution/README.md | 3 ++- 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 solution/0421.Maximum XOR of Two Numbers in an Array/Solution.py diff --git a/solution/0421.Maximum XOR of Two Numbers in an Array/Solution.py b/solution/0421.Maximum XOR of Two Numbers in an Array/Solution.py new file mode 100644 index 0000000000000..be25295ac40a1 --- /dev/null +++ b/solution/0421.Maximum XOR of Two Numbers in an Array/Solution.py @@ -0,0 +1,19 @@ +class Solution: + def findMaximumXOR(self, nums: List[int]) -> int: + max = 0 + mask = 0 + for i in range(30, -1, -1): + current = 1 << i + # 期望的二进制前缀 + mask = mask ^ current + # 在当前前缀下, 数组内的前缀位数所有情况集合 + _set = set() + for num in nums: + _set.add(num & mask) + # 期望最终异或值的从右数第i位为1, 再根据异或运算的特性推算假设是否成立 + flag = max | current + for prefix in _set: + if prefix ^ flag in _set: + max = flag + break + return max \ No newline at end of file diff --git a/solution/README.md b/solution/README.md index cefffab4adb56..f8583b6b8777e 100644 --- a/solution/README.md +++ b/solution/README.md @@ -768,7 +768,8 @@ │   └── Solution.java ├── 0421.Maximum XOR of Two Numbers in an Array │   ├── README.md -│   └── Solution.java +│   ├── Solution.java +│   └── Solution.py ├── 0423.Reconstruct Original Digits from English │   └── Solution.cpp ├── 0427.Construct Quad Tree