【[LeetCode]题解(python):122-Best Time to Buy and Sell Stock II】教程文章相关的互联网学习教程文章

151. Reverse Words in a String Leetcode Python

Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the".Update (2015-02-12): For C programmers: Try to solve it in-place in O(1) space. linear time: 1. go through the string detect whether the place is ‘ ‘ if not append the char to word 2 if the place is ‘ ‘ and word is not "" append the word to res 3. go to the end append ...

[LeetCode][Python]Add Two Numbers【代码】

# -*- coding: utf8 -*-‘‘‘__author__ = ‘dabay.wang@gmail.com‘https://oj.leetcode.com/problems/add-two-numbers/You are given two linked lists representing two non-negative numbers.The digits are stored in reverse order and each of their nodes contain a single digit.Add the two numbers and return it as a linked list.Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)Output: 7 -> 0 -> 8===Comments by Dabay===这...

[LeetCode][Python]Top K Frequent Elements【代码】

op K Frequent ElementsGiven a non-empty array of integers, return the k most frequent elements.For example,Given [1,1,1,2,2,3] and k = 2, return [1,2].Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements.Your algorithm‘s time complexity must be better than O(n log n), where n is the array‘s size. 求出现频率最高的K个数,要求时间复杂度不能超过 O(n log n)。选择最简单的快排...

leetcode Valid Parentheses python【代码】

# 解题思路: # 创建一个字典映射关系 dicts# 使用一个栈stk 遍历字符串s 得到一个新的字符串curItem 如果lastItem在dicts中的value和它相等 不做任何操作# 如果不等 入栈 有lastItem的 先append lastItem 然后是curItem ## 最后判断如果stk为空说明所给字符串匹配 return true class Solution(object):def isValid(self, s):""":type s: str:rtype: bool"""strLen = len(s)if strLen <= 1:return Falsedicts={"(":")","{":"}","[":...

[LeetCode]题解(python):035-Search Insert Position【代码】【图】

题目来源:  https://leetcode.com/problems/search-insert-position/ 题意分析:  给定一个排好序的数组和一个target,如果target在数组里面,那么返回他的位置,否者返回他应该插入哪个位置。 题目思路:  这也是一个标准的二分查找。如果没有找到,那么和first和last位置的数比较一下就可以得到答案。 代码(python): 1class Solution(object):2def searchInsert(self, nums, target):3""" 4 :type nums: List[i...

[LeetCode in Python] 5390 (M) minimum number of frogs croaking 数青蛙【代码】

题目https://leetcode-cn.com/problems/minimum-number-of-frogs-croaking/给你一个字符串 croakOfFrogs,它表示不同青蛙发出的蛙鸣声(字符串 "croak" )的组合。由于同一时间可以有多只青蛙呱呱作响,所以?croakOfFrogs 中会混合多个 “croak” 。请你返回模拟字符串中所有蛙鸣所需不同青蛙的最少数目。注意:要想发出蛙鸣 "croak",青蛙必须 依序 输出 ‘c’, ’r’, ’o’, ’a’, ’k’ 这 5 个字母。如果没有输出全部五个字母...

[LeetCode&Python] Problem 590. N-ary Tree Postorder Traversal【代码】【图】

Given an n-ary tree, return the postorder traversal of its nodes‘ values. For example, given a 3-ary tree: Return its postorder traversal as: [5,6,3,2,4,1]. Note: Recursive solution is trivial, could you do it iteratively? Recursion Solution:""" # Definition for a Node. class Node(object):def __init__(self, val, children):self.val = valself.children = children """ class Solution(object):def posto...

[leetcode]First Missing Positive @ Python【代码】【图】

原题地址:https://oj.leetcode.com/problems/first-missing-positive/题意:Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm should run in O(n) time and uses constant space.解题思路:要求时间复杂度为O(n)和常数空间。又是一个tricky的解法。class Solution:# @param A, a list of integers# @return an integer# ...

LeetCode472 - Concatenated Words - Hard (Python)【代码】

Given a list of words (without duplicates), please write a program that returns all concatenated words in the given list of words.A concatenated word is defined as a string that is comprised entirely of at least two shorter words in the given array.Example:Input: ["cat","cats","catsdogcats","dog","dogcatsdog","hippopotamuses","rat","ratcatdogcat"]Output: ["catsdogcats","dogcatsdog","ratcatdogcat"]...

【leetcode】Word Break(python)【图】

思路是这样的,我们从第一个字符开始向后依次找,直到找到一个断句的地方,使得当前获得的子串在dict中,若找到最后都没找到,那么就是False了。在找到第一个后,接下来找下一个断句处,当然是从第一个断句处的下一个字符开始找连续的子串,但是这时与第一个就稍有不同,比如说word=‘ab’, dict={ ‘a‘, ab‘, ...},在找到a后,接下来处理的是b,我们发现b不在dict中,但是我们发现b可以和a结合,形成ab,而ab在dict中,所以这里...

[LeetCode]题解(python):074-Search a 2D Matrix【代码】【图】

题目来源:  https://leetcode.com/problems/search-a-2d-matrix/ 题意分析:  给一个m×n矩阵,矩阵是按照从小到大排列,也就是a[i][j]和a[m][n]如果i > m则a[i][j]>a[m][n],如果i == m,j > n,则a[i][j]>a[m][n]。 题目思路:  利用二分查找,首先确定在那个行,然后继续二分查找。 代码(Python): 1class Solution(object):2def searchMatrix(self, matrix, target):3""" 4 :type matrix: List[List[int]]5 ...

[leetcode]Pow(x, n) @ Python【代码】【图】

原题地址:https://oj.leetcode.com/problems/powx-n/题意:Implement pow(x, n).解题思路:求幂函数的实现。使用递归,类似于二分的思路,解法来自Mark Allen Weiss的《数据结构与算法分析》。代码:class Solution:# @param x, a float# @param n, a integer# @return a floatdef pow(self, x, n):if n == 0:return 1elif n == 1:return xelif n % 2:return pow(x*x,n/2)*xelse:return pow(x*x,n/2) 原文:http://www.cnblogs.c...

【python】Leetcode每日一题-寻找旋转排序数组中的最小元素2【代码】

【python】Leetcode每日一题-寻找旋转排序数组中的最小元素2【题目描述】已知一个长度为 n 的数组,预先按照升序排列,经由 1 到 n 次 旋转 后,得到输入数组。例如,原数组nums = [0,1,2,4,5,6,7]在变化后可能得到: 若旋转4次,则可以得到 [4,5,6,7,0,1,2] 若旋转4次,则可以得到 [0,1,2,4,5,6,7] 注意,数组[a[0], a[1], a[2], ..., a[n-1]]旋转一次 的结果为数组[a[n-1], a[0], a[1], a[2], ..., a[n-2]]。给你一个可能存在重复...

[leetcode] Insertion Sort List(python)

简单的插入排序,总是超时,暂且放在这记录一下。class Solution:# @param head, a ListNode# @return a ListNodedef insertionSortList(self, head):if head == None or head.next == None:return headpsuhead = ListNode(-1)while head:tail = psuheadheadnext = head.nextwhile tail.next and tail.next.val < head.val:tail = tail.nexthead.next = tail.nexttail.next = headhead = headnextreturn psuhead.next Last execute...

[Leetcode][Python]54: Spiral Matrix【代码】

# -*- coding: utf8 -*-‘‘‘__author__ = ‘dabay.wang@gmail.com‘54: Spiral Matrixhttps://leetcode.com/problems/spiral-matrix/Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For example,Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]You should return [1,2,3,6,9,8,7,4,5].=== Comments by Dabay===一圈一圈的处理,一共处理n/2圈...