- 博客(119)
- 收藏
- 关注
原创 NOWCODER 剑指offer 构造栈,取最小
top只是取值,不删除;pop取值之后会删除用Python有点犯规# -*- coding:utf-8 -*-class Solution: def __init__(self): self.stack = [] def push(self, node): # write code here self.stack.appe...
2018-08-24 13:58:51
163
原创 NOWCODER 剑指offer 顺时针打印矩阵
折腾了好久。。写得好繁琐。。相当于试出来的运行时间:27ms占用内存:5852k# -*- coding:utf-8 -*-class Solution: # matrix类型为二维列表,需要返回列表 def printMatrix(self, matrix): # write code here if not matrix: ...
2018-08-23 15:23:52
125
原创 NOWCODER 剑指offer 二叉树的镜像
运行时间:26ms占用内存:5728k# -*- coding:utf-8 -*-# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: # 返回镜像树的...
2018-08-22 23:59:26
92
原创 NOWCODER 剑指offer ※树的子结构
这个一定要想清楚匹配成功的条件,属于子结构,必然是树2遍历完为空,树1可能遍历完了可能没遍历完而刚开始需要两棵树都不为空,所以必须要写一个内部代码循环调用运行时间:36ms占用内存:5624k# -*- coding:utf-8 -*-# class TreeNode:# def __init__(self, x):# self.val = x# ...
2018-08-22 23:52:53
118
原创 NOWCODER 剑指offer 合并两个排序的链表
运行时间:21ms占用内存:5724k链表我经常进入死循环而且不知道哪里出错。。要细致非递归算法O(min(m,n))# -*- coding:utf-8 -*-# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Soluti...
2018-08-22 23:02:11
110
原创 NOWCODER 剑指offer 反转链表
运行时间:28ms占用内存:5624k三个变量,pre,current,cnext以及注意最后的返回值是pre,因为current已经移出链表为null,而pre保留了前一个current的值# -*- coding:utf-8 -*-# class ListNode:# def __init__(self, x):# self.val = x# ...
2018-08-22 16:11:29
141
原创 NOWCODER 剑指offer 链表中倒数第k个结点
运行时间:30ms占用内存:5624k比较简单,就是少考虑了一些输入问题的情况# -*- coding:utf-8 -*-# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def FindKthTo...
2018-08-22 15:07:44
89
原创 NOWCODER 剑指offer 调整数组顺序使奇数位于偶数前面
开辟新空间的尝试运行时间:31ms占用内存:5712k# -*- coding:utf-8 -*-class Solution: def reOrderArray(self, array): # write code here #找到前半部分第一个奇数以及后半部分第一个偶数调换位置,误区,奇偶不一定对半分 #n = len(a...
2018-08-22 14:56:32
261
原创 NOWCODER 剑指offer 数值的整数次幂
运行时间:30ms占用内存:5724kPython犯规吗直接return啊运行时间:30ms占用内存:5724k若不用这个,主要是判断正负以及递归计算若指数n为偶数,直接a**n = (a**(n/2))*(a**(n/2))若指数n为奇数,则a**n = (a**((n-1)/2))*(a**((n-1)/2))*a ...
2018-08-22 14:17:44
131
原创 NOWCODER 剑指offer ※※二进制中1的个数
正数弄出来了,负数不对。讲究符号位之类的事情。正数反码的值、Python位运算一种不需要考虑转换成二进制是什么结构以及正负数的符号位补码之类的东西但是运行时间超出# -*- coding:utf-8 -*-class Solution: def NumberOf1(self, n): # write code here count = 0...
2018-08-22 14:01:48
119
原创 NOWCODER 剑指offer 矩形覆盖
明明和跳台阶以及斐波那契数列一样啊,但是显示超时另一种数组方法可以通过运行时间:30ms占用内存:5728k# -*- coding:utf-8 -*-class Solution: def rectCover(self, number): # write code here if number == 1: ret...
2018-08-21 16:40:06
126
原创 NOWCODER 剑指offer 跳台阶/变态跳台阶
# -*- coding:utf-8 -*-class Solution: def jumpFloor(self, number): # write code here if number==1: return 1 if number == 2: return 2 return...
2018-08-21 16:26:02
131
原创 NOWCODER 剑指offer 斐波那契数列
25 ms 5724K 居然不能用函数自己递归关于边界之类的问题还是要考虑细致# -*- coding:utf-8 -*-class Solution: def Fibonacci(self, n): # write code here fibo = [0,1] if n<2: return...
2018-08-21 15:55:47
137
原创 NOWCODER 剑指offer 旋转数组的最小数字
运行时间:843ms占用内存:5624kO(n)# -*- coding:utf-8 -*-class Solution: def minNumberInRotateArray(self, rotateArray): # write code here if not rotateArray: return 0 ...
2018-08-21 15:04:00
141
原创 NOWCODER 剑指offer 用两个栈实现队列
运行时间:30ms占用内存:5624kpython写类以及数组pop问题、栈和队列的概念# -*- coding:utf-8 -*-class Solution: def __init__(self): self.stack1 = [] self.stack2 = [] def push(self, node): # ...
2018-08-20 23:06:22
104
原创 NOWCODER 剑指offer ※(前序+中序建树)重建二叉树
运行时间:64ms占用内存:5712k递归还是不熟,或者说是树不熟注意递归的终止条件,是大于不是大于等于# -*- coding:utf-8 -*-# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.righ...
2018-08-20 21:25:32
152
原创 NOWCODER 剑指offer 从尾到头打印链表
一开始想着reverse()函数,但是没有成功,后来用的[::-1]倒序遍历赋值,成功运行时间:32ms占用内存:5728k# -*- coding:utf-8 -*-# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solu...
2018-08-07 16:31:39
114
原创 NOWCODER 剑指offer 替换空格
记得Python貌似有直接的函数,但是没想起来,用的splite拆分之后再合并的操作运行时间:24ms占用内存:5600k# -*- coding:utf-8 -*-class Solution: # s 源字符串 def replaceSpace(self, s): # write code here chars = s.split...
2018-08-07 16:25:24
120
原创 NOWCODER 剑指offer 二维数组中的查找
第一种:从左下角开始查找,大了往上移,小了往右移运行时间:182ms占用内存:5860k# -*- coding:utf-8 -*-class Solution: # array 二维列表 def Find(self, target, array): # write code here m = len(array) - 1 ...
2018-08-07 16:03:41
105
原创 统计学习方法-一些概念
统计学习方法三要素模型:模型的假设空间,即假设要学习的模型属于某个函数的集合,即有输入空间到输出空间的映射的集合 策略:模型选择的标准 算法:模型学习的算法,实现求解最优模型的算法统计学习包括监督学习、非监督学习、半监督学习、强化学习。根据输入输出变量类型的不同,对预测任务给予不同的名称:输入、输出均为连续变量:回归问题 输入为连续变量,输出为离散变量:分类问题 输入和输出均...
2018-07-29 19:14:47
193
原创 规范化、标准化、归一化、正则化
规范化:针对数据库规范化把关系满足的规范要求分为几级,满足要求最低的是第一范式(1NF),再来是第二范式、第三范式、BC范式和4NF、5NF等等,范数的等级越高,满足的约束集条件越严格。针对数据数据的规范化包括归一化标准化正则化,是一个统称(也有人把标准化作为统称)。数据规范化是数据挖掘中的数据变换的一种方式,数据变换将数据变换或统一成适合于数据挖掘的形式,将被挖掘对象的属性数据按...
2018-07-18 21:52:39
30668
原创 Linux的一些必要软件的安装和配置
1. wps很用不惯Linux自带的文档编辑软件,所以下载wps,使用感比较好前往wps官网,找到Linux的下载包http://community.wps.cn/download/如上图,我下载的是64为的deb安装包,下载到 ~/下载/ 目录下sudo dpkg -i wps-office_10.1.0.6634_amd64.deb若碰上依赖问题:sudo...
2018-07-04 12:12:46
2628
原创 【leetcode】※669. Trim a Binary Search Tree
对树有抵触心理。。题干并没有说明如果根节点不在范围内且左右子树都存在的情况下,是左节点上位还是右节点上位;以及左右子树都有左右子树的情况怎么摆放。看结果后发现我忽略了一点,binary tree有左小右大的规律。node的整颗左子树节点都必然小于它,node的整颗右子树节点都必然大于他。所以每次切的时候,如果根节点都不在范围内,则根和整个左子树或者右子树必然都要切除;如果根节点在范围内,则递归查看...
2018-07-02 21:15:27
115
原创 【leetcode】463. Island Perimeter
26.26%class Solution(object): def islandPerimeter(self, grid): """ :type grid: List[List[int]] :rtype: int """ result = 0 leni = len(grid) l...
2018-06-26 11:49:35
212
原创 【leetcode】682. Baseball Game
33.89% class Solution(object): def calPoints(self, ops): """ :type ops: List[str] :rtype: int """ result = 0 for i,char in enumerate(ops): ...
2018-06-25 20:54:29
132
原创 【leetcode】620. Not Boring Movies
97.66%(如果改为id%2=1则变为91.13% ,因此与python一样,若是判断范围更大,则效率会更高)# Write your MySQL query statement belowselect id,movie,description,ratingfrom cinemawhere id%2>0 and description!='boring'order by ratin...
2018-06-25 20:08:31
238
原创 【leetcode】791. Custom Sort String
7.68%class Solution(object): def customSortString(self, S, T): """ :type S: str :type T: str :rtype: str """ lists = [] result = '' ...
2018-06-24 16:12:38
136
原创 【leetcode】500. Keyboard Row
42.21% 很笨的方法,连用三个循环 不过一次过class Solution(object): def findWords(self, words): """ :type words: List[str] :rtype: List[str] """ keyboard = ['qwertyuiopQWERTY...
2018-06-24 15:42:08
202
原创 【leetcode】344. Reverse String
85.25%简直是上一道题目的子命题class Solution(object): def reverseString(self, s): """ :type s: str :rtype: str """ return s[::-1]
2018-06-23 10:17:23
94
原创 python 位运算
正数的源码与补码、反码一致;负数的反码是将其源码除符号位之外的各位求反,负数的补码是将其源码出符号位之外的各位求反后再加1。总结所有来说,就是进行运算的时候是各个数的补码,最后得出来的也是结果的补码。按位运算就把数字转换为机器语言——二进制的数字来运算的一种运算形式。在计算机系统中,数值一律用补码来表示(存储)。按位与 &eg:3&5正数补码与源码一致,3的补码为 115的补...
2018-06-22 18:10:24
279
原创 【leetcode】557 Reverse Words in a String III
找到让我认为replace之类的函数是改变本身的罪魁祸首了。就是reverse()函数。作用在list上,而且改变的是原函数,返回的是none。38.45%class Solution(object): def reverseWords(self, s): """ :type s: str :rtype: str "...
2018-06-22 17:38:04
118
原创 【leetcode】476 Number Complement
Input: 5Output: 2Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.搞了半天位运算,从小到大都搞不清位运算,专开一章分析吧 最后还是字串弄的93%note:replace()函数...
2018-06-22 17:20:38
151
原创 【leetcode】821. Shortest Distance to a Character
85msclass Solution(object): def shortestToChar(self, S, C): """ :type S: str :type C: str :rtype: List[int] """ result = [S.find(C)] for i i...
2018-06-22 15:24:56
216
原创 【leetcode】811. Subdomain Visit Count
16.08%class Solution(object): def subdomainVisits(self, cpdomains): """ :type cpdomains: List[str] :rtype: List[str] """ do_dict = {} for domain in ...
2018-06-21 20:20:07
299
原创 【leetcode】338. Counting Bits
21.58% 时间复杂度没有按要求note:while判断的时候,首先要满足的条件放and左边class Solution(object): def countBits(self, num): """ :type num: int :rtype: List[int] """ result = [] ...
2018-06-21 14:55:25
129
原创 【leetcode】852. Peak Index in a Mountain Array
56ms class Solution(object): def peakIndexInMountainArray(self, A): """ :type A: List[int] :rtype: int """ return A.index(max(A))--------------------------...
2018-06-21 13:43:03
223
原创 【leetcode】806. Number of Lines To Write String
84.36%note: string.ascii_letters string.digitsimport stringclass Solution(object): def numberOfLines(self, widths, S): """ :type widths: List[int] :type S: str ...
2018-06-21 11:38:53
129
原创 【leetcode】419. Battleships in a Board
想到后面觉得好难board = [['X', ' . ', ' . '], [' . ', ' . ', 'X'], [' . ', ' . ', 'X'], [' . ', ' . ', ' X ', ]]记录一下我的挣扎:if not filter(lambda x:x<len(b...
2018-06-21 09:34:17
347
原创 【leetcode】537. Complex Number Multiplication
被上个medium吓到,这个很简单86.25%note:print int('-1') #-1print str(-1) #'-1'class Solution(object): def complexNumberMultiply(self, a, b): """ :type a: str :type b: str ...
2018-06-20 19:06:14
124
原创 【leetcode】763. Partition Labels
不超时已经满足。。11.48%class Solution(object): def partitionLabels(self, S): """ :type S: str :rtype: List[int] """ s_list = list(S) lenth = len(s_list) ...
2018-06-20 18:35:43
125
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人
RSS订阅