My personal micro-channel public number: Microstrong
Micro-channel public number ID: MicrostrongAI
Micro-channel public number Description: Microstrong (Bauer) students mainly study machine learning, deep learning, computer vision, intelligent dialogue system-related content, share study notes in the learning process! Look forward to your attention, welcome the exchange of learning progress together!
Know almost home page: https: //www.zhihu.com/people/MicrostrongAI/activities
Topic links:
Subject description:
Problem-solving ideas:
(1) time complexity is
AC has code:
# -*- coding:utf-8 -*-
import math
class Solution:
def FindContinuousSequence(self, tsum):
if tsum == 0 or tsum == 1:
return []
mid_ceil = int(math.ceil(float(tsum) / 2))
result_list = []
for i in range(1, mid_ceil + 1, 1):
temp_sum = 0
for j in range(i, 0, -1):
temp_sum += j
if temp_sum == tsum:
temp_list = [k for k in range(j, i + 1, 1)]
result_list.append(temp_list)
return result_list
if __name__ == "__main__":
sol = Solution()
print(sol.FindContinuousSequence(3))
(2) time complexity is
We can consider two numbers big and small sequences of maximum and minimum values, respectively. First, the small initialized to 1, big initialized to 2. If s is greater than the sequence from small to big, and, the smaller value can be removed from the sequence, i.e. to increase the value of small. If the sequence from small to big, and is less than s, the big can be increased, so that the sequence contains more digits. Because this must be at least two sequence numbers, we have to increase the small (1 + s) until the / 2.
And in order for all continuous sequence 9, for example, we put small initialized to 1, big initialized to 2. At this time, between the small and big sequence is {1,2}, the sequence is 3, less than 9, so we want the next sequence comprising more digits. We put 3 into a big increase, this time sequence {1,2,3}. And since the sequence is 6, is still less than 9, we then add 4 becomes big, interposed between the small and big sequence also will become {1,2,34}. Since the sequence is greater than 10 and 9, we have omitted to some numbers in the sequence, then we add 2 becomes small, the sequence obtained at this time is {2,3,4}, and a sequence exactly 9. We found the first and continuous sequence 9, print it out. Next we add Big, repeat the previous process, and can be found for the second consecutive sequence of 9 {4, 5}. The whole process can be summarized in the following table.
step | small | big | sequence | And sequence | Compared with s | The next step |
1 | 1 | 2 | 1,2 | 3 | Less than | Big increase |
2 | 1 | 3 | 1,2,3 | 6 | Less than | Big increase |
3 | 1 | 4 | 1,2,3,4 | 10 | more than the | Small increase |
4 | 2 | 4 | 2,3,4 | 9 | equal | Save the sequence, increase big |
5 | 2 | 5 | 2,3,4,5 | 14 | more than the | Small increase |
6 | 3 | 5 | 3,4,5 | 12 | more than the | Small increase |
7 | 4 | 5 | 4,5 | 9 | equal | Save the sequence |
After the formation of a clear problem-solving ideas, we can start writing code. Here is the reference code has AC:
# -*- coding:utf-8 -*-
class Solution:
def FindContinuousSequence(self, tsum):
# write code here
if tsum == 0 or tsum == 1 or tsum == 2:
return []
result_list = []
mid = int((tsum + 1) // 2)
array = [i for i in range(0, mid + 1)]
small = 1
big = 2
temp_sum = array[small] + array[big]
temp = [array[small], array[big]]
while True:
if temp_sum == tsum:
temp_list = [t for t in temp]
result_list.append(temp_list)
if big < len(array) - 1:
big += 1
temp_sum = temp_sum + array[big]
temp.append(array[big])
else:
return result_list
if temp_sum > tsum:
temp_sum = temp_sum - array[small]
temp.remove(array[small])
small += 1
if temp_sum < tsum:
if big < len(array) - 1:
big += 1
temp_sum = temp_sum + array[big]
temp.append(array[big])
else:
return result_list
return result_list
In the above code, seeking a consecutive sequence and a small application technique. Typically we might use a continuous loop and sequence, but taking into account the sequence of the sequence after each operation and before the operation of most of the figures are the same as compared to simply increasing or decreasing a number, so we can previous and after the seek operation sequence and the base sequence of the. This can reduce unnecessary operation, thereby improving the efficiency of the code.
Reference:
[1] "to prove safety offer", the Haitao.