python3
文章平均质量分 52
孤舟钓客
人生如梦,梦何曾醒?不过旧愁新怨。但无他。回首向来萧瑟处,也无风雨也无晴。夜阑风静彀纹平,小舟从此逝,江海寄馀生。
展开
-
打印两个有序链表的公共部分
题目:给定两个有序链表的头指针,打印两个链表的公共数据。思路:两个链表从表头head1, head2开始处理:head1值小于head2值,head1往下移动;head2值小于head1值,head2往下移动;相等,打印这个值,都往下移动;任一链表到尾部,结束实现:class Node(): def __init__(self, value): self....原创 2019-11-04 00:53:09 · 86 阅读 · 0 评论 -
最大值减去最小值小于或等于num的子数组数量
题目:给定数组arr和整数num,求满足下列条件的子数组个数:max(arr[i:j]) - min(arr[i:j]) <= num要求:时间复杂度O(n)思路:1.最简单的想法,暴力求解import randomimport timefrom functools import wrapsdef timethis(func): @wraps(func) ...原创 2019-11-03 21:09:59 · 196 阅读 · 0 评论 -
求最大子矩阵的大小
题目:给定一个值只有0,1的整型矩阵,求其中全是1 的所有矩形区域中,最大的矩形区域中 1 的数量。如:1 0 1 11 1 1 11 1 1 0中最大矩形区域有6个1思路:转化成求解从每一行开始的直方图中最大矩形区域。如:上面的矩阵按第一行看,直方图高度为:1,0,1,1从第二行看为:2,1,2,2从第三行看为:3,2,3,0最简单的解法,遍历高度数组,每个位置,...原创 2019-11-03 19:59:14 · 291 阅读 · 0 评论 -
构造数组的MaxTree
题目:定义二叉树节点:class TreeNode(): def __init__(self, value): self._left = None self._right = None self._value = value def get_left(self): return self._left...原创 2019-10-31 23:22:22 · 598 阅读 · 0 评论 -
生成窗口最大值数组
题目:有一个整型数组arr,和一个大小为window_size的窗口从数组最左边滑到最右边,每次滑动一个位置。求窗口滑动过程中,窗口中的最大值。思路:维护一个双端队列dq,队列中存放的是窗口中最大值的索引的更新结构。遍历数组arr,当遍历到任意位置i时,若dq为空,直接把下标i放入队列;若dq不空,假设队尾下表j,因为dq维护的是窗口中最大值索引的更新结构,若arr[j] > a...原创 2019-10-30 22:55:36 · 123 阅读 · 0 评论 -
用栈求解汗诺塔问题
汉诺塔:汉诺塔(Tower of Hanoi)源于印度传说中,大梵天创造世界时造了三根金钢石柱子,其中一根柱子自底向上叠着64片黄金圆盘。大梵天命令婆罗门把圆盘从下面开始按大小顺序重新摆放在另一根柱子上。并且规定,在小圆盘上不能放大圆盘,在三根柱子之间一次只能移动一个圆盘。常规的汗诺塔相对比较简单,按照如下思路解决:如果只剩下一个盘子,直接移动如果剩下多个盘:1)把前(n-1)个圆盘从左...原创 2019-10-29 23:23:28 · 411 阅读 · 0 评论 -
用一个栈实现另一个栈的排序
题目:一个栈中元素类型为整型,将栈中的元素从栈顶到栈底从小到大排序,只允许使用一个辅助栈和其他简单变量。思路:从栈顶到栈底,依次取出栈中元素x,使用一个辅助栈,1)如果辅助栈为空,直接将x压到辅助栈2)辅助栈不空,x小于栈顶元素,直接压入辅助栈3)辅助栈不空,x大于栈顶元素,辅助栈元素依次出栈,直到x小于等于辅助栈栈顶,或辅助栈变空,再将x压栈实现栈:class Stack: ...原创 2019-10-28 21:18:54 · 178 阅读 · 0 评论 -
猫狗队列
题目:宠物,猫,狗如下class Pet: def __init__(self, no): self.no = no def get_type(self): return self.type def get_no(self): return self.noclass Cat(Pet): ...原创 2019-10-27 21:44:14 · 168 阅读 · 0 评论 -
如何仅用递归函数和栈操作逆序一个栈
题目:只使用递归跟栈的操作,实现逆序栈思路:1.先实现一个栈class Stack: def __init__(self): self.s = [] def push(self, data): self.s.append(data) def pop(self): return self.s.pop() ...原创 2019-10-27 19:53:40 · 189 阅读 · 0 评论 -
由两个栈实现队列
题目:实现一个类,用两个栈实现一个队列,支持队列的基本操作(add, poll, peek)思路:两个栈,stack_push负责接收数据,stack_pop负责弹出数据,当有新数据到来,直接进入stack_push,当要求出队列时,1)当stack_pop为空时,将stack_push里的数据依次出栈,加入stack_pop,则stack_pop里的数据是后入栈stack_push的在上...原创 2019-10-27 18:51:46 · 102 阅读 · 0 评论 -
设计一个有getMin功能的栈
题目:实现一个栈,在实现栈的基本功能的基础上,能够返回栈中最小元素要求:1.pop, push, getMin时间复杂度都是O(1)2.可以使用现成的栈结构思路:用两个栈,一个正常保存数据,一个保存当前最小值代码:class MinStack: def __init__(self): self.data_stack = [] self.min...原创 2019-10-27 18:22:55 · 243 阅读 · 0 评论 -
raspberry pi (5) 轻触开关,倾斜开关,振动开关,干簧管传感器,U型光电传感器
轻触开关#!/usr/bin/env python3import RPi.GPIO as GPIOButtonPin = 11Gpin = 12Rpin = 13def setup(): GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location GPIO.setup(Gpin, GPIO....原创 2019-04-02 22:42:05 · 1696 阅读 · 0 评论 -
raspberry pi (3) RGB-LED
GPIO号代码#!/usr/bin/env pythonimport RPi.GPIO as GPIO import timeimport randompins = {'pin_R': 11, 'pin_G': 12, 'pin_B': 13}GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical loc...原创 2019-03-27 23:43:22 · 455 阅读 · 0 评论 -
raspberry pi (2) GPIO, PWM和双色LED
PWM脉宽调制(PWM)是指用微处理器的数字输出来对模拟电路进行控制,是一种对模拟信号电平进行数字编码的方法。在树莓派上,可以通过对GPIO的编程来实现PWM。简单说就是在对应的引脚产生类似于方波似的波形,可以通过设置相应的参数直接来控制波形的周期,以及高电平在整个波形中所占的比例。占空比占空比(Duty Cycle)有如下含义: 在一串理想的脉冲序列中(如方波),正脉冲的持续时间...原创 2019-03-26 23:21:30 · 1563 阅读 · 0 评论 -
raspberry pi (11) DS18B20温度传感器,DHT11数字温湿度传感器,BMP180气压传感器
1.DS18B20温度传感器#!/usr/bin/env python3import osds18b20 = ''def setup(): global ds18b20 for i in os.listdir('/sys/bus/w1/devices'): if i != 'w1_bus_master1': ds18b20 = idef read():# glob...原创 2019-04-27 13:09:55 · 1029 阅读 · 0 评论 -
raspberry pi (0)
很久很久以前,买了个树莓派,一直没有动手,前不久又买了一套传感器,决定玩一下。1)准备原创 2019-03-24 23:39:25 · 282 阅读 · 0 评论 -
raspberry pi (6) 有源蜂鸣器,无源蜂鸣器
有源蜂鸣器内置震荡源,通电时会发出声音。#!/usr/bin/env python3import RPi.GPIO as GPIOimport timeBuzzer = 11 # pin11def setup(pin): global BuzzerPin BuzzerPin = pin GPIO.setmode(GPIO.BOARD) # Numbers...原创 2019-04-02 23:40:05 · 2514 阅读 · 0 评论 -
raspberry pi (7) PCF8591模数转换器,雨滴探测传感器,PS2操纵杆,电位器传感器
1.PCF8591模数转换器#------------------------------------------------------## This is a program for PCF8591 Module. ## Warnng! The Analog input MUST NOT be over 3.3V!# # In this ...原创 2019-04-21 10:00:48 · 2295 阅读 · 10 评论 -
raspberry pi (8) 模拟霍尔传感器,模拟温度传感器,声音传感器,光敏传感器
1.模拟霍尔传感器#/usr/bin/env python3 import RPi.GPIO as GPIO import PCF8591 as ADCimport timedef setup(): ADC.setup(0x48)def Print(x): if x == 0: print('') print('**********...原创 2019-04-21 12:33:52 · 1227 阅读 · 2 评论 -
raspberry pi (9) 红外遥控器
1.配置1)sudo apt update2)sudo apt-get install lirc3)sudo vi /etc/lirc/hardware.confLIRCD_ATGS="--uinput"DRIVER="default"DEVICE="/dev/lirc0"MODULES="lirc-rpi4)sudo vi /etc/modules最后面加入:l...原创 2019-04-21 12:54:17 · 1326 阅读 · 0 评论 -
raspberry pi (10) 触摸开关传感器,超声波传感器,红外避障传感器
1.触摸开关传感器#!/usr/bin/env pythonimport RPi.GPIO as GPIOTouchPin = 11Gpin = 12Rpin = 13tmp = 0def setup(): GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location GPIO.setup(G...原创 2019-04-27 12:55:59 · 1620 阅读 · 2 评论 -
raspberry pi (12) I2C LCD1602液晶显示器,DS1302事实时钟
1.I2C LCD1602液晶显示器#!/usr/bin/env pythonimport timeimport smbusBUS = smbus.SMBus(1)def write_word(addr, data): global BLEN temp = data if BLEN == 1: temp |= 0x08 else: temp &= 0xF7...原创 2019-04-27 13:15:36 · 1272 阅读 · 0 评论 -
leetcode practice - python3 (3)
53. Maximum SubarrayDescriptionHintsSubmissionsDiscussSolution Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its su...原创 2018-05-16 19:34:49 · 171 阅读 · 0 评论 -
raspberry pi (4) 继电器,激光传感器
import RPi.GPIO as GPIO import timeRelayPin = 11 # pin11 def setup(): GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location GPIO.setup(RelayPin, GPIO.OUT) GPIO.output...原创 2019-03-29 23:31:16 · 647 阅读 · 0 评论 -
python3 数据结构和算法(4) 并查集
import randomimport timeimport sysfrom functools import wrapsdef timethis(func): @wraps(func) def wrapper(*args, **kwargs): start = time.time() result = func(*args, **kwar...原创 2018-08-04 21:14:53 · 432 阅读 · 0 评论 -
python3 数据结构和算法(3) 二叉搜索树
import randomimport timefrom functools import wrapsdef timethis(func): @wraps(func) def wrapper(*args, **kwargs): start = time.time() result = func(*args, **kwargs) ...原创 2018-08-04 21:14:08 · 291 阅读 · 0 评论 -
leetcode practice - python3 (11)
198. House RobberDescriptionHintsSubmissionsDiscussSolution You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constrain...原创 2018-06-03 14:08:56 · 248 阅读 · 0 评论 -
程序员的算法趣题 python3 - (1)
1.回文数求十进制、二进制、八进制表示都是回文数的数字中,大于十进制数10的最小值。num = 11while True: s10 = str(num) s2 = str(bin(num))[2:] s8 = str(oct(num))[2:] if s10 == s10[::-1] and s2 == s2[::-1] and s8 == s8[::...原创 2018-05-27 01:11:58 · 902 阅读 · 0 评论 -
leetcode practice - python3 (5)
98. Validate Binary Search TreeDescriptionHintsSubmissionsDiscussSolution Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left su...原创 2018-05-26 14:15:20 · 321 阅读 · 0 评论 -
leetcode practice - python3 (4)
78. SubsetsDescriptionHintsSubmissionsDiscussSolution Given a set of distinct integers, nums, return all possible subsets (the power set).Note: The solution set must not contain duplicate subsets...原创 2018-05-20 20:01:40 · 224 阅读 · 0 评论 -
leetcode practice - python3 (2)
21. Merge Two Sorted ListsDescriptionHintsSubmissionsDiscussSolution Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the fir...原创 2018-05-15 19:05:43 · 203 阅读 · 0 评论 -
leetcode practice - python3 (9)
617. Merge Two Binary TreesDescriptionHintsSubmissionsDiscussSolution Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped...原创 2018-05-26 15:31:19 · 232 阅读 · 0 评论 -
leetcode practice - python3 (8)
169. Majority ElementDescriptionHintsSubmissionsDiscussSolution Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You m...原创 2018-05-26 15:14:26 · 453 阅读 · 0 评论 -
leetcode practice - python3 (7)
543. Diameter of Binary TreeDescriptionHintsSubmissionsDiscussSolution Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length o...原创 2018-05-26 14:57:55 · 280 阅读 · 0 评论 -
leetcode practice - python3 (6)
56. Merge IntervalsDescriptionHintsSubmissionsDiscussSolution Given a collection of intervals, merge all overlapping intervals.Example 1: Input: [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,1...原创 2018-05-26 14:33:59 · 239 阅读 · 0 评论 -
leetcode practice - python3 (12)
236. Lowest Common Ancestor of a Binary TreeDescriptionHintsSubmissionsDiscussSolution Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the d...原创 2018-06-03 14:42:32 · 272 阅读 · 0 评论 -
leetcode practice - python3 (13)
560. Subarray Sum Equals KDescriptionHintsSubmissionsDiscussSolution Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.E...原创 2018-06-04 19:45:23 · 247 阅读 · 0 评论 -
leetcode practice - python3 (10)
198. House RobberDescriptionHintsSubmissionsDiscussSolution You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constrain...原创 2018-05-28 23:36:46 · 299 阅读 · 0 评论 -
python3 数据结构和算法(2) 优先队列
import randomimport timefrom functools import wrapsdef timethis(func): @wraps(func) def wrapper(*args, **kwargs): start = time.time() result = func(*args, **kwargs) ...原创 2018-08-04 21:13:14 · 364 阅读 · 0 评论 -
python3 数据结构和算法(1) 排序
0.utilimport randomimport timefrom functools import wrapsdef timethis(func): @wraps(func) def wrapper(*args, **kwargs): start = time.time() result = func(*args, **kwargs)...原创 2018-08-04 21:03:40 · 318 阅读 · 0 评论