python
qq_32371827
这个作者很懒,什么都没留下…
展开
-
lintcode
二分查找给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1。class Solution: """ @param nums: The integer array. @param target: Target to find. @return:...原创 2019-11-08 15:57:09 · 107 阅读 · 0 评论 -
lintcode删除链表中倒数第n个节点
题意:删除链表中倒数第n个结点,尽量只扫描一遍。使用两个指针扫描,当第一个指针扫描到第N个结点后,第二个指针从表头与第一个指针同时向后移动,当第一个指针指向空节点时,另一个指针就指向倒数第n个结点了‘’’"""Definition of ListNodeclass ListNode(object): def __init__(self, val, next=None): ...原创 2019-11-01 17:17:48 · 79 阅读 · 0 评论 -
lintcode链表的中点
第一次写的,如果fast.next是none,那么fast.next.next会报错 def middleNode(self, head): # write your code here i = 0 if head.val != None: slow = head fast = head ...原创 2019-11-01 16:13:01 · 120 阅读 · 0 评论 -
lintcode中位数下标
给出一个含有 n 个互不相等整数的无序数组,找到其中中位数的下标。下标从 0 开始。中位数是指这些数排序后最中间的数。若 n 为偶数,则中位数是数组排序后的第 n/2 个数。https://www.lintcode.com/problem/median-index/descriptionclass Solution: """ @param a: the array a ...原创 2019-07-01 18:38:26 · 380 阅读 · 0 评论 -
lintcode丑数 II
用已有丑数乘以2,3,5寻找丑数。import timeclass Solution: """ @param n: An integer @return: return a integer as description. """ def nthUglyNumber(self, n): if n <= 6: ...转载 2019-07-02 11:49:08 · 143 阅读 · 0 评论 -
[Python] Python 之 __new__() 方法与实例化
写的很详细,收藏,不会就看一遍https://www.cnblogs.com/ifantastic/p/3175735.html转载 2019-07-08 10:12:45 · 100 阅读 · 0 评论 -
lintcode合并数字
给出n个数,现在要将这n个数合并成一个数,每次只能选择两个数a,b合并,每次合并需要消耗a+b的能量,输出将这n个数合并成一个数后消耗的最小能量。class Solution: """ @param numbers: the numbers @return: the minimum cost """ def mergeNumber(self, number...转载 2019-07-02 16:46:48 · 234 阅读 · 0 评论 -
排序算法
https://www.cnblogs.com/brucecloud/p/6085703.html转载 2019-07-03 11:42:58 · 61 阅读 · 0 评论 -
lintcode第k大元素
在数组中找到第 n大的元素。想到之前用的heapq内置模块,heapq.nlargest可以返回前n大的数,最后一个数就是第n大的数import heapqclass Solution: """ @param n: An integer @param nums: An array @return: the Kth largest element """...原创 2019-07-03 16:01:25 · 111 阅读 · 0 评论 -
lintcode多关键字排序
给定 n 个学生的学号(从 1 到 n 编号)以及他们的考试成绩,表示为(学号,考试成绩),请将这些学生按考试成绩降序排序,若考试成绩相同,则按学号升序排序。使用sort方法,先对item0排序,再对item1排序,优先级越高的排序越后做class Solution: """ @param array: the input array @return: the sort...原创 2019-06-28 19:23:44 · 176 阅读 · 0 评论 -
lintcode会议室
给定一系列的会议时间间隔,包括起始和结束时间[[s1,e1],[s2,e2],…(si < ei),确定一个人是否可以参加所有会议。"""Definition of Interval.class Interval(object): def __init__(self, start, end): self.start = start self.en...原创 2019-07-01 10:33:11 · 334 阅读 · 0 评论 -
lintcode尾部的零
设计一个算法,计算出n阶乘中尾部零的个数class Solution: """ @param: n: An integer @return: An integer, denote the number of trailing zeros in n! """ def trailingZeros(self, n): # write your c...原创 2019-07-01 11:05:21 · 125 阅读 · 0 评论 -
lintcode数字1的个数
给定整数n,计算出现在小于等于n的所有非负整数中的数字1的总数。直接数1的个数。class Solution: """ @param n: an integer @return: the total number of digit 1 """ def countDigitOne(self, n): # write your code h...原创 2019-07-01 11:27:07 · 207 阅读 · 0 评论 -
python位运算
pass转载 2019-06-12 11:22:40 · 426 阅读 · 0 评论 -
Python进制转换
bin()、oct()、hex()的返回值均为字符串,且分别带有0b、0o、0x前缀。转载 2019-06-12 11:22:04 · 167 阅读 · 0 评论 -
买卖股票的最佳时机 III
class Solution: """ @param prices: Given an integer array @return: Maximum profit """ def maxProfit1(self, prices): # write your code here maxprofit = 0 mi...原创 2019-06-17 17:19:13 · 114 阅读 · 0 评论 -
生成任意字符组合的密码
import timefrom itertools import product# #列表排列组合# k = [1, 2, 3]# print(list(product(k, k)))# print(list(product(k, repeat=3)))#将元祖数据转变为字符串,返回新列表def TupToStr(list): list3 = [] n = ''...原创 2019-04-25 16:37:02 · 1146 阅读 · 0 评论 -
lintcode :旋转字符串
题目:旋转字符串给定一个字符串和一个偏移量,根据偏移量旋转字符串(从左向右旋转)样例对于字符串 “abcdefg”.offset=0 => “abcdefg”offset=1 => “gabcdef”offset=2 => “fgabcde”offset=3 => “efgabcd”挑战在数组上原地旋转,使用O(1)的额外空间 class Sol...原创 2019-06-06 16:47:42 · 145 阅读 · 0 评论 -
lintcode字符串查找
对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始)。如果不存在,则返回 -1。class Solution: """ @param source: @param target: @return: return the index """ def...原创 2019-06-19 14:17:00 · 127 阅读 · 0 评论 -
Python 字符串操作(截取/替换/查找/分割)
原文:https://www.cnblogs.com/baoendemao/p/3804664.html转载 2019-06-19 14:39:56 · 176 阅读 · 0 评论 -
lintcode判断字符串是否有重复字符
class Solution: """ @param: str: A string @return: a boolean """ def isUnique(self, str): # write your code here for i in range(len(str)): for...原创 2019-06-19 17:38:08 · 412 阅读 · 0 评论 -
Lintcode 给定一个字符串,逐个翻转字符串中的每个单词。
说明单词的构成:无空格字母构成一个单词,有些单词末尾会带有标点符号输入字符串是否包括前导或者尾随空格?可以包括,但是反转后的字符不能包括如何处理两个单词间的多个空格?在反转字符串中间空格减少到只含一个样例样例 1:输入: “the sky is blue”输出: “blue is sky the”样例解释: 返回逐字反转的字符串.样例 2:输入: “hello w...原创 2019-06-19 18:33:55 · 1043 阅读 · 0 评论 -
lintcode第一个只出现一次的字符
样例 1:输入: “abaccdeff”输出: ‘b’解释:'b' 是第一个出现一次的字符样例 2:输入: “aabccd”输出: ‘b’解释:'b' 是第一个出现一次的字符class Solution: """ @param str: str: the given string @return: char: the first unique c...原创 2019-06-19 19:51:48 · 164 阅读 · 0 评论 -
查找n位数的水仙花数
数字变字符串,再将字符串分离。n = 3if n == 1: start = 0else: start = 10 ** (n - 1)end = 10 ** nstart = 10 **(n -1)print(start, end)for i in range (start,end): str1 = str(i) sum1 = 0 for j ...原创 2019-06-17 11:45:43 · 1185 阅读 · 0 评论 -
LintCode-买卖股票的最佳时机
假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格。如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润。您在真实的面试中是否遇到过这个题?样例Example 1Input: [3, 2, 3, 1, 2]Output: 1Explanation: You can buy at the third day and then sell it at th...原创 2019-06-17 14:41:46 · 105 阅读 · 0 评论 -
买卖股票的最佳时机 II
给定一个数组 prices 表示一支股票每天的价格.你可以完成任意次数的交易, 不过你不能同时参与多个交易 (也就是说, 如果你已经持有这支股票, 在再次购买之前, 你必须先卖掉它).设计一个算法求出最大的利润.class Solution: """ @param prices: Given an integer array @return: Maximum prof...原创 2019-06-17 15:11:58 · 87 阅读 · 0 评论 -
生成字典
生成字典import timefrom itertools import product# #列表排列组合# k = [1, 2, 3]# print(list(product(k, k)))# print(list(product(k, repeat=3)))#将数字补位至足够位数的字符串def intadd(num,n): diff = n - len(str(num...原创 2019-04-23 13:42:44 · 873 阅读 · 0 评论