From bdab08c2a47f788138bf55ea03b13bbc9bc88054 Mon Sep 17 00:00:00 2001 From: exchris Date: Tue, 27 Nov 2018 17:10:18 +0800 Subject: [PATCH 01/24] =?UTF-8?q?leetcode=20537=20=E5=A4=8D=E6=95=B0?= =?UTF-8?q?=E7=9A=84=E4=B9=98=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/code/complexNumberMultiply.py | 37 ++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 leetcode/code/complexNumberMultiply.py diff --git a/leetcode/code/complexNumberMultiply.py b/leetcode/code/complexNumberMultiply.py new file mode 100644 index 0000000..dda92cd --- /dev/null +++ b/leetcode/code/complexNumberMultiply.py @@ -0,0 +1,37 @@ +#!/usr/bin/python +# -*- coding:utf-8 -*- + +class Solution: + # 复数乘法 + def complexNumberMultiply(self, a, b): + # 获得字符串a的实数部分 + a_real = a.replace('i', '').split('+')[0] + a_imag = a.replace('i', '').split('+')[1] + int_a_real = int(a_real) + int_a_imag = int(a_imag) + + b_real = b.replace('i', '').split('+')[0] + b_imag = b.replace('i', '').split('+')[1] + int_b_real = int(b_real) + int_b_imag = int(b_imag) + + s1 = str(int_a_real * int_b_real) + s2 = str(int_a_real * int_b_imag + int_b_real * int_a_imag) + 'i' + s3 = str(int_b_imag * int_a_imag) + less = int(s1) - int(s3) + s = str(less) + '+' + s2 + print(s) + + def complexNumberMultiply1(self, a, b): + A, B = [], [] + for x in a.replace('i', '').split('+'): + A.append(int(x)) + for x in b.replace('i', '').split('+'): + B.append(int(x)) + + s = str(A[0] * B[0] - A[1] * B[1]) + '+' + str(A[0] * B[1] + A[1] * B[0]) + 'i' + print(s) + + +s = Solution() +s.complexNumberMultiply1("1+-1i", "1+-1i") From 434807db87d3d719b3e39519ecd06b9ed16bc928 Mon Sep 17 00:00:00 2001 From: exchris Date: Tue, 27 Nov 2018 17:11:19 +0800 Subject: [PATCH 02/24] =?UTF-8?q?leetcode=20459=20=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E7=9A=84=E5=AD=90=E5=AD=97=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/code/repeatedSubstringPattern.py | 39 +++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 leetcode/code/repeatedSubstringPattern.py diff --git a/leetcode/code/repeatedSubstringPattern.py b/leetcode/code/repeatedSubstringPattern.py new file mode 100644 index 0000000..c50a15d --- /dev/null +++ b/leetcode/code/repeatedSubstringPattern.py @@ -0,0 +1,39 @@ +#!/usr/bin/python +# -*- coding:utf-8 -*- +''' +459,重复的子字符串 +给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成。给定的字符串只含有小写英文字母,并且长度不超过10000。 + +示例 1: + +输入: "abab" + +输出: True + +解释: 可由子字符串 "ab" 重复两次构成。 +示例 2: + +输入: "aba" + +输出: False +示例 3: + +输入: "abcabcabcabc" + +输出: True + +解释: 可由子字符串 "abc" 重复四次构成。 (或者子字符串 "abcabc" 重复两次构成。) +''' + +class Solution: + + def repeatedSubstringPattern(self, s): + """ + :param s:str + :return: bool + """ + return (s+s)[1:-1].find(s) != -1 + +s = Solution(); +boolean = s.repeatedSubstringPattern("abcabcabcabc") +print(boolean) \ No newline at end of file From e7d7a516e5e5978aba81b645217a0dc0c0c3f277 Mon Sep 17 00:00:00 2001 From: exchris Date: Tue, 27 Nov 2018 17:18:06 +0800 Subject: [PATCH 03/24] =?UTF-8?q?leetcode=20118=20=E6=9D=A8=E8=BE=89?= =?UTF-8?q?=E4=B8=89=E8=A7=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/code/triangle.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 leetcode/code/triangle.py diff --git a/leetcode/code/triangle.py b/leetcode/code/triangle.py new file mode 100644 index 0000000..7faa15f --- /dev/null +++ b/leetcode/code/triangle.py @@ -0,0 +1,25 @@ +#!/usr/bin/python +# -*- coding:utf-8 -*- + +# 118 杨辉三角 +class Solution: + + def generate(self, num_rows): + triangle = [] + + for row_num in range(num_rows): + # The first and last row elements are always 1 + row = [None for _ in range(row_num + 1)] + row[0], row[-1] = 1, 1 + + # Each triangle element is equal to the sum of the elements + # above-and-to-the-left and above-and-to-the-right + for j in range(1, len(row) - 1): + row[j] = triangle[row_num-1][j-1] + triangle[row_num-1][j] + + triangle.append(row) + + print(triangle) + +s = Solution() +s.generate(5) From 2372bf55d2ad22c2572bb73d3a0e6ee025a826b3 Mon Sep 17 00:00:00 2001 From: exchris Date: Tue, 27 Nov 2018 17:32:10 +0800 Subject: [PATCH 04/24] =?UTF-8?q?leetcode=20118=20=E6=9D=A8=E8=BE=89?= =?UTF-8?q?=E4=B8=89=E8=A7=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/code/triangle.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/leetcode/code/triangle.py b/leetcode/code/triangle.py index 7faa15f..81068aa 100644 --- a/leetcode/code/triangle.py +++ b/leetcode/code/triangle.py @@ -1,25 +1,23 @@ #!/usr/bin/python # -*- coding:utf-8 -*- -# 118 杨辉三角 class Solution: - def generate(self, num_rows): + def generate(self, row_nums): triangle = [] - for row_num in range(num_rows): - # The first and last row elements are always 1 + for row_num in range(row_nums): + # The first and last row elements area always 1 row = [None for _ in range(row_num + 1)] row[0], row[-1] = 1, 1 - # Each triangle element is equal to the sum of the elements - # above-and-to-the-left and above-and-to-the-right for j in range(1, len(row) - 1): - row[j] = triangle[row_num-1][j-1] + triangle[row_num-1][j] + row[j] = triangle[row_num - 1][j - 1] + triangle[row_num - 1][j] triangle.append(row) - print(triangle) + return triangle + s = Solution() -s.generate(5) +s.getRow(5) From 6cbd1f2c03a4b2a5bc745cffb81c881092e96b45 Mon Sep 17 00:00:00 2001 From: exchris Date: Tue, 27 Nov 2018 17:32:46 +0800 Subject: [PATCH 05/24] =?UTF-8?q?leetcode=20119=20=E6=9D=A8=E8=BE=89?= =?UTF-8?q?=E4=B8=89=E8=A7=92=20index?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/code/triangle_index.py | 42 +++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 leetcode/code/triangle_index.py diff --git a/leetcode/code/triangle_index.py b/leetcode/code/triangle_index.py new file mode 100644 index 0000000..251431b --- /dev/null +++ b/leetcode/code/triangle_index.py @@ -0,0 +1,42 @@ +#!/usr/bin/python +# -*- coding:utf-8 -*- + +class Solution: + + def getRow(self, rowIndex): + lst = self.generate(rowIndex + 1)[-1] + print(lst) + + def generate(self, row_nums): + triangle = [] + + for row_num in range(row_nums): + # The first and last row elements area always 1 + row = [None for _ in range(row_num + 1)] + row[0], row[-1] = 1, 1 + + for j in range(1, len(row) - 1): + row[j] = triangle[row_num - 1][j - 1] + triangle[row_num - 1][j] + + triangle.append(row) + + return triangle + + def getRow1(self, rowIndex): + if rowIndex == 0: + return [1] + lst = [] + i, j = 1, 1 + h = rowIndex + while i < rowIndex: + lst.append(h // j) + h *= rowIndex - i + j *= i + 1 + i += 1 + lst.append(1) + lst.insert(0, 1) + print(lst) + + +s = Solution() +s.getRow(5) From 8acc23e3a7cd570bec216b076d30b3a35151e1f7 Mon Sep 17 00:00:00 2001 From: exchris Date: Tue, 27 Nov 2018 17:56:07 +0800 Subject: [PATCH 06/24] =?UTF-8?q?leetcode=20349=20=E4=B8=A4=E4=B8=AA?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E7=9A=84=E4=BA=A4=E9=9B=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/code/intersection.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 leetcode/code/intersection.py diff --git a/leetcode/code/intersection.py b/leetcode/code/intersection.py new file mode 100644 index 0000000..ea31864 --- /dev/null +++ b/leetcode/code/intersection.py @@ -0,0 +1,13 @@ +#!/usr/bin/python +# -*- coding:utf-8 -*- +class Solution: + """ + 349.两个数组的交集 + """ + + def intersection(self, nums1, nums2): + lst = list(set(nums1) & set(nums2)) + print(lst) + +s = Solution() +s.intersection([1, 2, 2, 1], [2, 2]) From 5026ba2cfc5d9752aa877f52a09448dcfbd66238 Mon Sep 17 00:00:00 2001 From: exchris Date: Wed, 28 Nov 2018 16:21:17 +0800 Subject: [PATCH 07/24] tuple demo --- Basic/{ => tuple}/tuple_demo.py | 62 ++++++++++++++++----------------- 1 file changed, 31 insertions(+), 31 deletions(-) rename Basic/{ => tuple}/tuple_demo.py (97%) diff --git a/Basic/tuple_demo.py b/Basic/tuple/tuple_demo.py similarity index 97% rename from Basic/tuple_demo.py rename to Basic/tuple/tuple_demo.py index f431301..6894855 100644 --- a/Basic/tuple_demo.py +++ b/Basic/tuple/tuple_demo.py @@ -1,32 +1,32 @@ -#! /usr/bin/env python -# -*- coding:utf-8 -*- - -""" -另一种有序列表叫元组:tuple。 -tuple和list非常相似,但是tuple一旦初始化就不能修改 -比如同样是列表同学的名字: -""" -classmates = ('Michael', 'Bob', 'Tracy') - -# 因为tuple不可变,所以代码更安全。如果可能,能用tuple代替list就尽量用tuple -# tuple的陷阱:当您定义一个tuple时,在定义的时候,tuple的元素就必须被确定下来 -t = (1, 2) -print(t) -# 定义一个空的tuple,可以写成(): -t = () -print(t) -# 要定义一个只有1个元素的tuple,如果您这么定义 -t = (1) -print(t) -# 定义的不是tuple,是1 这个数!这是因为括号() 既可以表示tuple,又可以表示数学公式中的小括号,这就产生了 -# 歧义,因此,Python规定,这种情况下,按小括号进行计算,计算结果自然是1 。 - -# 所以,只有1个元素的tuple定义时必须加一个逗号',',来消除歧义 -t = (1, ) -print(t) - -# 最后来看一个"可变的"tuple -t = ('a', 'b', ['A', 'B']) -t[2][0] = 'X' -t[2][1] = 'Y' +#! /usr/bin/env python +# -*- coding:utf-8 -*- + +""" +另一种有序列表叫元组:tuple。 +tuple和list非常相似,但是tuple一旦初始化就不能修改 +比如同样是列表同学的名字: +""" +classmates = ('Michael', 'Bob', 'Tracy') + +# 因为tuple不可变,所以代码更安全。如果可能,能用tuple代替list就尽量用tuple +# tuple的陷阱:当您定义一个tuple时,在定义的时候,tuple的元素就必须被确定下来 +t = (1, 2) +print(t) +# 定义一个空的tuple,可以写成(): +t = () +print(t) +# 要定义一个只有1个元素的tuple,如果您这么定义 +t = (1) +print(t) +# 定义的不是tuple,是1 这个数!这是因为括号() 既可以表示tuple,又可以表示数学公式中的小括号,这就产生了 +# 歧义,因此,Python规定,这种情况下,按小括号进行计算,计算结果自然是1 。 + +# 所以,只有1个元素的tuple定义时必须加一个逗号',',来消除歧义 +t = (1, ) +print(t) + +# 最后来看一个"可变的"tuple +t = ('a', 'b', ['A', 'B']) +t[2][0] = 'X' +t[2][1] = 'Y' print(t) \ No newline at end of file From c0d9c112f841f6c52704eccf61cc7f05a5504f12 Mon Sep 17 00:00:00 2001 From: exchris Date: Thu, 29 Nov 2018 09:25:35 +0800 Subject: [PATCH 08/24] =?UTF-8?q?=E5=BF=BD=E7=95=A5=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 ++ Basic/algorithm/bubble_sort.py | 2 ++ 2 files changed, 4 insertions(+) create mode 100644 Basic/algorithm/bubble_sort.py diff --git a/.gitignore b/.gitignore index 894a44c..ddc6316 100644 --- a/.gitignore +++ b/.gitignore @@ -102,3 +102,5 @@ venv.bak/ # mypy .mypy_cache/ + +test.* diff --git a/Basic/algorithm/bubble_sort.py b/Basic/algorithm/bubble_sort.py new file mode 100644 index 0000000..9136bd7 --- /dev/null +++ b/Basic/algorithm/bubble_sort.py @@ -0,0 +1,2 @@ +#!/usr/bin/python +# -*- coding:utf-8 -*- \ No newline at end of file From e1ca45ce596c04a583e1c127bddedf80dd6d7b5b Mon Sep 17 00:00:00 2001 From: exchris Date: Thu, 29 Nov 2018 09:27:15 +0800 Subject: [PATCH 09/24] update gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index ddc6316..28659a8 100644 --- a/.gitignore +++ b/.gitignore @@ -103,4 +103,4 @@ venv.bak/ # mypy .mypy_cache/ -test.* +*/test.* From ab6257829ba7f2c9026ce8ef79cdda838ff092f5 Mon Sep 17 00:00:00 2001 From: exchris Date: Thu, 29 Nov 2018 09:49:46 +0800 Subject: [PATCH 10/24] =?UTF-8?q?python=E5=86=92=E6=B3=A1=E6=8E=92?= =?UTF-8?q?=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Basic/algorithm/bubble_sort.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Basic/algorithm/bubble_sort.py b/Basic/algorithm/bubble_sort.py index 9136bd7..01db74c 100644 --- a/Basic/algorithm/bubble_sort.py +++ b/Basic/algorithm/bubble_sort.py @@ -1,2 +1,17 @@ #!/usr/bin/python -# -*- coding:utf-8 -*- \ No newline at end of file +# -*- coding:utf-8 -*- + +class Solution: + + def bubble_sort(self, nums): + for i in range(1, len(nums)): + for j in range(0, len(nums) - i): + if nums[j] > nums[j + 1]: + nums[j], nums[j + 1] = nums[j + 1], nums[j] + + return nums + + +s = Solution() +lst = s.bubble_sort([2, 3, 1, 4, 5, 9, 8]) +print(lst) From c32beff47c57c1bac54444e755ef76b1f88a1847 Mon Sep 17 00:00:00 2001 From: exchris Date: Thu, 29 Nov 2018 15:51:25 +0800 Subject: [PATCH 11/24] =?UTF-8?q?leetcode=207.=E6=95=B4=E6=95=B0=E5=8F=8D?= =?UTF-8?q?=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/code/reverse.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 leetcode/code/reverse.py diff --git a/leetcode/code/reverse.py b/leetcode/code/reverse.py new file mode 100644 index 0000000..577816b --- /dev/null +++ b/leetcode/code/reverse.py @@ -0,0 +1,29 @@ +#!/usr/bin/python +# -*- coding:utf-8 -*- + +# 整数反转 +class Solution: + def reverse(self, x): + if x >= 0: + num = int(str(x)[::-1]) + else: + num = int('-' + str(-x)[::-1]) + + if num >= (-2 ** 31) and num <= 2 ** 31 - 1: + return num + else: + return 0 + + +s = Solution() +n = s.reverse(123) +print(n) + +n = s.reverse(-123) +print(n) + +n = s.reverse(120) +print(n) + +n = s.reverse(1563847412) +print(n) From 97c43a81d4dad4eac5c66cc1e15ac2834725ae84 Mon Sep 17 00:00:00 2001 From: exchris Date: Thu, 29 Nov 2018 16:01:26 +0800 Subject: [PATCH 12/24] =?UTF-8?q?leetcode=20231.2=E7=9A=84=E5=B9=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/code/isPowerOfTwo.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 leetcode/code/isPowerOfTwo.py diff --git a/leetcode/code/isPowerOfTwo.py b/leetcode/code/isPowerOfTwo.py new file mode 100644 index 0000000..924b911 --- /dev/null +++ b/leetcode/code/isPowerOfTwo.py @@ -0,0 +1,20 @@ +#!/usr/bin/python +# -*- coding:utf-8 -*- + +# 2的幂 +class Solution: + def isPowerOfTwo(self, n): + if n <= 0: + return False + if n == 1: + return True + + while n > 1: + if (n % 2 != 0): + return False + n /= 2 + return True + +s = Solution() +b = s.isPowerOfTwo(1) +print(b) \ No newline at end of file From f2c76aea79a9d7f950883e6765f8fd6dfad9c978 Mon Sep 17 00:00:00 2001 From: exchris Date: Thu, 29 Nov 2018 16:02:15 +0800 Subject: [PATCH 13/24] =?UTF-8?q?requests=20and=20beautifulsoup=20?= =?UTF-8?q?=E7=88=AC=E5=8F=96=E8=B4=B4=E5=90=A7=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BeautifulSoup4/grabImg.py | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 BeautifulSoup4/grabImg.py diff --git a/BeautifulSoup4/grabImg.py b/BeautifulSoup4/grabImg.py new file mode 100644 index 0000000..315db94 --- /dev/null +++ b/BeautifulSoup4/grabImg.py @@ -0,0 +1,42 @@ +import requests +from bs4 import BeautifulSoup +import os + +baseurl = "https://tieba.baidu.com/p/5461479002" +index = 1 + + +def getpics(url): + global index + r = requests.get(url) + soup = BeautifulSoup(r.text, "lxml") + pagenum = soup.find("li", class_="l_reply_num").find_all("span", class_="red")[1].text + pics = soup.find_all("img", class_="BDE_Image") + for pic in pics: + # print(pic['src']) + savepics(pic['src'], index) + print("picture " + str(index) + " saved") + index = index + 1 + + +def savepics(url, name): + if os.path.exists('pictures/picture' + str(name) + '.jpg'): + return + if not os.path.exists('pictures'): + os.mkdir("pictures") + else: + piccontent = requests.get(url).content + with open('pictures/picture' + str(name) + '.jpg', 'wb') as f: + f.write(piccontent) + + +if __name__ == '__main__': + r = requests.get(baseurl) + soup = BeautifulSoup(r.text, "lxml") + pagenum = soup.find("li", class_="l_reply_num").find_all("span", class_="red")[1].text + for i in range(1, int(pagenum) + 1): + if (i == 1): + url = baseurl + else: + url = baseurl + "?pn=" + str(i) + getpics(url) From 2d95fd096514f4e42d9d2bcb1a310e250e79709f Mon Sep 17 00:00:00 2001 From: exchris Date: Thu, 29 Nov 2018 16:30:50 +0800 Subject: [PATCH 14/24] =?UTF-8?q?=E5=BF=BD=E7=95=A5pictures?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 28659a8..bde1962 100644 --- a/.gitignore +++ b/.gitignore @@ -103,4 +103,4 @@ venv.bak/ # mypy .mypy_cache/ -*/test.* +pictures/ From 454a1676fa0bccf8799dc5e24d7ed486621103ba Mon Sep 17 00:00:00 2001 From: exchris Date: Thu, 29 Nov 2018 17:20:33 +0800 Subject: [PATCH 15/24] =?UTF-8?q?=E6=B5=8B=E8=AF=95numpy=E5=AE=89=E8=A3=85?= =?UTF-8?q?=E6=88=90=E5=8A=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Numpy/testInstallSuccess.py | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 Numpy/testInstallSuccess.py diff --git a/Numpy/testInstallSuccess.py b/Numpy/testInstallSuccess.py new file mode 100644 index 0000000..5913533 --- /dev/null +++ b/Numpy/testInstallSuccess.py @@ -0,0 +1,8 @@ +#!/usr/bin/python +# -*- coding:utf-8 -*- +__author__ = 'exchris' + +# 测试是否安装成功 +from numpy import * + +print(eye(4)) From 560153410eb500bc153384ec5025849d1acd450a Mon Sep 17 00:00:00 2001 From: exchris Date: Thu, 29 Nov 2018 17:25:52 +0800 Subject: [PATCH 16/24] numpy ndarray object --- Numpy/numpy_ndarray_object.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Numpy/numpy_ndarray_object.py diff --git a/Numpy/numpy_ndarray_object.py b/Numpy/numpy_ndarray_object.py new file mode 100644 index 0000000..e4408d7 --- /dev/null +++ b/Numpy/numpy_ndarray_object.py @@ -0,0 +1,20 @@ +#!/usr/bin/python +# -*- coding:utf-8 -*- +__author__ = 'exchris' + +import numpy as np + +a = np.array([1, 2, 3]) +# print(a) + +# 多于一个维度 +a = np.array([[1, 2], [3, 4]]) +# print(a) + +# 最小维度 +a = np.array([1, 2, 3, 4, 5], ndmin=2) +# print(a) + +# dtype参数 +a = np.array([1, 2, 3], dtype=complex) +print(a) From f7ef8c77a29ddb43044bd242a36f247b95880e2a Mon Sep 17 00:00:00 2001 From: exchris Date: Thu, 29 Nov 2018 17:45:57 +0800 Subject: [PATCH 17/24] numpy dtype --- Numpy/numpy_dtype.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Numpy/numpy_dtype.py diff --git a/Numpy/numpy_dtype.py b/Numpy/numpy_dtype.py new file mode 100644 index 0000000..5ef4641 --- /dev/null +++ b/Numpy/numpy_dtype.py @@ -0,0 +1,37 @@ +#!/usr/bin/python +# -*- coding:utf-8 -*- +__author__ = 'exchris' + +import numpy as np + +# 使用标量类型 +dt = np.dtype(np.int32) +# print(dt) + +# int8, int16, int32, int64四种数据类型可以使用字符串'i1','i2','i4','i8'代替 +dt = np.dtype('i4') +# print(dt) + +# 字节顺序标注 +dt = np.dtype('>i4') +# print(dt) + +# 首先创建结构化数据类型 +dt = np.dtype([('age', np.int8)]) +# print(dt) [('age', 'i1')] + +dt = np.dtype([('age', np.int8)]) +a = np.array([(10,), (20,), (30,)], dtype=dt) +# print(a) + +# 类型字段名可以用于存取实际的age列 +dt = np.dtype([('age', np.int8)]) +a = np.array([(10,), (20,), (30,)], dtype=dt) +# print(a['age']) + +student = np.dtype([('name', 'S20'), ('age', 'i1'), ('marks', 'f4')]) +# print(student) + +student = np.dtype([('name', 'S20'), ('age', 'i1'), ('marks', 'f4')]) +a = np.array([('abc', 21, 50), ('xyz', 18, 75)], dtype=student) +print(a) From b5b53ef66042a0096e341ce2311475e35148f8e7 Mon Sep 17 00:00:00 2001 From: exchris Date: Fri, 30 Nov 2018 09:45:28 +0800 Subject: [PATCH 18/24] =?UTF-8?q?leetcode=20137.=E5=8F=AA=E5=87=BA?= =?UTF-8?q?=E7=8E=B0=E4=B8=80=E6=AC=A1=E7=9A=84=E6=95=B0=E5=AD=97II?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/code/singleNumber.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 leetcode/code/singleNumber.py diff --git a/leetcode/code/singleNumber.py b/leetcode/code/singleNumber.py new file mode 100644 index 0000000..bf0b68d --- /dev/null +++ b/leetcode/code/singleNumber.py @@ -0,0 +1,17 @@ +#!/usr/bin/python +# -*- coding:utf-8 -*- +__author__ = 'exchris' + +# 137.只出现一次的数字II +class Solution: + + def singleNumber(self, nums): + a, b = 0, 0 + for num in nums: + b = ~a & (b ^ num) + a = ~b & (a ^ num) + return b + +s = Solution() +s1 = s.singleNumber([2, 2, 3, 2]) +print(s1) \ No newline at end of file From 64d56b1892033eed1f5194b446be7a9bc5bd9261 Mon Sep 17 00:00:00 2001 From: exchris Date: Fri, 30 Nov 2018 10:05:02 +0800 Subject: [PATCH 19/24] =?UTF-8?q?leetcode=20384.=E6=89=93=E4=B9=B1?= =?UTF-8?q?=E6=95=B0=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/code/shuffle_an_array.py | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 leetcode/code/shuffle_an_array.py diff --git a/leetcode/code/shuffle_an_array.py b/leetcode/code/shuffle_an_array.py new file mode 100644 index 0000000..f249fbe --- /dev/null +++ b/leetcode/code/shuffle_an_array.py @@ -0,0 +1,36 @@ +#!/usr/bin/python +# -*- coding:utf-8 -*- +__author__ = 'exchris' + + +# 383.打乱数组 +class Solution: + + def __init__(self, nums): + self.origin = nums[:] + self.output = nums + + def reset(self): + """ + Resets the array to its original configuration and return it. + :rtype: List[int] + """ + return self.origin + + def shuffle(self): + """ + Returns a random shuffling of the array. + :rtype: List[int] + """ + import random + n = len(self.output) + for i in range(n): + j = random.randint(i, n - 1) + self.output[i], self.output[j] = self.output[j], self.output[i] + return self.output + + +nums = [1, 2, 3] +s = Solution(nums) +print(s.shuffle()) +print(s.reset()) From cb9b3b040970464bace18f3f6c073007c88a646b Mon Sep 17 00:00:00 2001 From: exchris Date: Fri, 30 Nov 2018 10:10:35 +0800 Subject: [PATCH 20/24] =?UTF-8?q?leetcode=2066.=E5=8A=A0=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/code/plusOne.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 leetcode/code/plusOne.py diff --git a/leetcode/code/plusOne.py b/leetcode/code/plusOne.py new file mode 100644 index 0000000..42f57c6 --- /dev/null +++ b/leetcode/code/plusOne.py @@ -0,0 +1,17 @@ +#!/usr/bin/python +# -*- coding:utf-8 -*- +__author__ = 'exchris' + +class Solution: + def plusOne(self, digits): + s , output = '', [] + + for i in digits: + s += str(i) + num = int(s) + 1 + for i in str(num): + output.append(int(i)) + print(output) + +s = Solution() +s.plusOne([9, 9]) \ No newline at end of file From d2bc436e2215b773de06eecc347c00e141908766 Mon Sep 17 00:00:00 2001 From: exchris Date: Fri, 30 Nov 2018 10:48:47 +0800 Subject: [PATCH 21/24] =?UTF-8?q?leetcode=20204.=E8=AE=A1=E6=95=B0?= =?UTF-8?q?=E8=B4=A8=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/code/countPrimes.py | 43 ++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 leetcode/code/countPrimes.py diff --git a/leetcode/code/countPrimes.py b/leetcode/code/countPrimes.py new file mode 100644 index 0000000..5ce21f6 --- /dev/null +++ b/leetcode/code/countPrimes.py @@ -0,0 +1,43 @@ +#!/usr/bin/python +# -*- coding:utf-8 -*- +__author__ = 'exchris' + +""" +204.计数质数 +统计所以小于非负整数n的质数的数量 +输入:10 +输出: 4 +解释:小于10的质数一共有4个,它们是2,3,5,7 +""" + + +class Solution: + + def countPrimes(self, n): + if n < 3: + return 0 + prime = [1] * n + prime[0] = prime[1] = 0 + for i in range(2, int(n ** 0.5) + 1): + if prime[i] == 1: + prime[i * i:n:i] = [0] * len(prime[i * i:n:i]) + return sum(prime) + + + def countPrimes1(self, n): + flag, sum = False, 0 + for i in range(2, n): + for j in range(2, int(i**0.5)+1): + if i % j == 0: + flag = True + break + if flag == 0: + print(i) + sum += 1 + else: + flag = False + print(sum) + +s = Solution() +s1 = s.countPrimes1(10) +print(s1) From d442ec403891015b5b123183bdc4116831c9c735 Mon Sep 17 00:00:00 2001 From: exchris Date: Fri, 30 Nov 2018 11:03:08 +0800 Subject: [PATCH 22/24] =?UTF-8?q?leetcode=20400.=E7=AC=ACN=E4=B8=AA?= =?UTF-8?q?=E6=95=B0=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/code/findNthDigit.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 leetcode/code/findNthDigit.py diff --git a/leetcode/code/findNthDigit.py b/leetcode/code/findNthDigit.py new file mode 100644 index 0000000..0f217f6 --- /dev/null +++ b/leetcode/code/findNthDigit.py @@ -0,0 +1,25 @@ +class Solution(object): + def findNthDigit(self, n): + """ + :type n: int + :rtype: int + """ + """ + 个位数:1-9,一共9个,共计9个数字 + 2位数:10-99,一共90个,共计180个数字 + 3位数:100-999,一共900个,共计270个数字 + 4位数,1000-9999,一共9000个,共计36000个数字 36000=4*9*10**(4-1) + ...... + """ + # 第一步确定n是在几位数里,第二步是确定在几位数的第几位数字的第几位 + # 第一步 + digit = 1 # 位数 + while n > digit * 9 * 10 ** (digit - 1): + n -= digit * 9 * 10 ** (digit - 1) + digit += 1 + # 第二步 + a = int((n - 1) / digit) # 得到几位数的第几位数字 + b = int((n - 1) % digit) # 得到几位数的第几位数字的第几位 + num = 10 ** (digit - 1) + a # 得到第几位数字是多少 + res = list(str(num))[b:b + 1] # 数字转字符再转列表把第几位数的第几位切出来 + return int(''.join(res)) # 列表转字符再转数字 From 82e42edd7eb4f4609ed7e5e2a2167ddc9a2bfbca Mon Sep 17 00:00:00 2001 From: exchris Date: Fri, 30 Nov 2018 11:09:24 +0800 Subject: [PATCH 23/24] =?UTF-8?q?leetcode=20461.=E6=B1=89=E6=98=8E?= =?UTF-8?q?=E8=B7=9D=E7=A6=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/code/hammingDistance.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 leetcode/code/hammingDistance.py diff --git a/leetcode/code/hammingDistance.py b/leetcode/code/hammingDistance.py new file mode 100644 index 0000000..16f01a2 --- /dev/null +++ b/leetcode/code/hammingDistance.py @@ -0,0 +1,12 @@ +#!/usr/bin/python +# -*- coding:utf-8 -*- +__author__ = 'exchris' + +# 461.汉明距离 +class Solution: + def hammingDistance(self, x, y): + s = bin(x ^ y)[2:] + print(s.count('1')) + +s = Solution() +s.hammingDistance(1, 4) \ No newline at end of file From bbfeb8db0a3a753ae9c3cee66d7d348c11fd46f6 Mon Sep 17 00:00:00 2001 From: exchris Date: Fri, 30 Nov 2018 13:03:05 +0800 Subject: [PATCH 24/24] =?UTF-8?q?leetcode=20263.=E4=B8=91=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- leetcode/code/isUgly.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 leetcode/code/isUgly.py diff --git a/leetcode/code/isUgly.py b/leetcode/code/isUgly.py new file mode 100644 index 0000000..56f8889 --- /dev/null +++ b/leetcode/code/isUgly.py @@ -0,0 +1,26 @@ +#!/usr/bin/python +# -*- coding:utf-8 -*- +__author__ = 'exchris' + + +# 263.丑数(只包含2,3,5的正整数) +class Solution: + def isUgly(self, num): + if num < 1: + return False + while num % 2 == 0: + num /= 2 + while num % 3 == 0: + num /= 3 + while num % 5 == 0: + num /= 5 + return (num == 1) or (num == 2) or (num == 3) or (num == 5) + + # 因式分解 + def getFactory(self, num): + return [x for x in range(1, num+1) if num % x == 0] + + +s = Solution() +b = s.getFactory(28) +print(b)