diff --git a/.gitignore b/.gitignore index 894a44c..bde1962 100644 --- a/.gitignore +++ b/.gitignore @@ -102,3 +102,5 @@ venv.bak/ # mypy .mypy_cache/ + +pictures/ diff --git a/Basic/algorithm/bubble_sort.py b/Basic/algorithm/bubble_sort.py new file mode 100644 index 0000000..01db74c --- /dev/null +++ b/Basic/algorithm/bubble_sort.py @@ -0,0 +1,17 @@ +#!/usr/bin/python +# -*- 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) 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) 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) 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) 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)) 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") 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) 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)) # 列表转字符再转数字 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 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]) 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 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) 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 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) 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()) 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 diff --git a/leetcode/code/triangle.py b/leetcode/code/triangle.py new file mode 100644 index 0000000..81068aa --- /dev/null +++ b/leetcode/code/triangle.py @@ -0,0 +1,23 @@ +#!/usr/bin/python +# -*- coding:utf-8 -*- + +class Solution: + + 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 + + +s = Solution() +s.getRow(5) 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)