leetcode
luoganttcc
微信:luogantt
展开
-
leetcode 组合总和
39. 组合总和给定一个无重复元素的正整数数组 candidates 和一个正整数 target ,找出 candidates 中所有可以使数字和为目标数 target 的唯一组合。candidates 中的数字可以无限制重复被选取。如果至少一个所选数字数量不同,则两种组合是唯一的。 对于给定的输入,保证和为 target 的唯一组合数少于 150 个。 示例 1:输入: candidates = [2,3,6,7], target = 7输出: [[7],[2,2,3]]示例 2原创 2021-07-20 10:42:09 · 130 阅读 · 0 评论 -
leetcode 外观数列
def countAndSay( n): """ :type n: int :rtype: str """ pre = '' cur = '1' for _ in range(1, n): pre = cur cur = '' start = 0 end = 0 print('#####################################原创 2021-07-19 09:48:51 · 84 阅读 · 0 评论 -
leetcode 搜索插入位置
给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。你可以假设数组中无重复元素。示例 1:输入: [1,3,5,6], 5输出: 2示例 2:输入: [1,3,5,6], 2输出: 1示例 3:输入: [1,3,5,6], 7输出: 4示例 4:输入: [1,3,5,6], 0输出: 0class Solution(object): def searchInsert(self, nums, target)原创 2021-07-18 18:01:28 · 72 阅读 · 0 评论 -
leetcode 在排序数组中查找元素的第一个和最后一个位置
给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。如果数组中不存在目标值 target,返回 [-1, -1]。进阶:你可以设计并实现时间复杂度为 O(log n) 的算法解决此问题吗?示例 1:输入:nums = [5,7,7,8,8,10], target = 8输出:[3,4]示例 2:输入:nums = [5,7,7,8,8,10], target = 6输出:[-1,-1]示例 3:输入:nums = [], t原创 2021-07-18 17:32:53 · 165 阅读 · 0 评论 -
leetcode 搜索旋转排序数组
class Solution(object): def search(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """ #nn=nums[k:]+nums[:k] nn=nums if target in nn: return nn.ind原创 2021-07-18 17:27:12 · 130 阅读 · 0 评论 -
下一个排列
class Solution(object): def cartesian_product(self,a,b): l = [] #空的集合 for x in a: #x为a中所有的成员 for y in b: #y为b中所有的成员 kk=str(x)+str(y) # print(kk) if len(set(kk))==len(kk):原创 2021-07-18 17:17:04 · 82 阅读 · 0 评论 -
leetcode 有效的括号
给定一个只包括 ‘(’,’)’,’{’,’}’,’[’,’]’ 的字符串 s ,判断字符串是否有效。有效字符串需满足:左括号必须用相同类型的右括号闭合。左括号必须以正确的顺序闭合。示例 1:输入:s = “()”输出:true示例 2:输入:s = “()[]{}”输出:true示例 3:输入:s = “(]”输出:false示例 4:输入:s = “([)]”输出:false示例 5:输入:s = “{[]}”输出:true提示:1 <= s.length &原创 2021-07-18 16:49:49 · 111 阅读 · 0 评论 -
python 四数之和
# -*- coding: utf-8 -*-"""Created on Sat Jul 17 17:23:50 2021@author: luogantt""" class Solution(object): def cartesian_product(self,a,b): l = [] #空的集合 for x in a: #x为a中所有的成员 for y in b: #y为b中所有的成员 ..原创 2021-07-18 13:55:37 · 387 阅读 · 0 评论 -
leetcode -python 三数之和原创
对于列表的几个数之和,一种通用的算法是计算其笛卡尔积,然后再计算,这是一种通用做法# -*- coding: utf-8 -*-"""Created on Sat Jul 17 17:23:50 2021@author: luogantt""" class Solution(object): def cartesian_product(self,a,b): l = [] #空的集合 for x in a: #x为a中所有的成员原创 2021-07-18 13:31:36 · 128 阅读 · 0 评论 -
leetcode 最长公共前缀
def cm(s): s1=list(zip(*s)) cc='' for k in s1: ss=list(set(k)) if len(ss)==1: cc+=ss[0] return cca= ['flower', 'flow', 'flight']print(cm(a))def cm1(a): a.sort(key=len) char='' for i in ra原创 2021-07-17 16:09:59 · 77 阅读 · 0 评论 -
LeetCode:罗马数字转整数(Python版本)
class Solution(object): def romanToInt(self, s): """ :type s: str :rtype: int """ doAnd = 0 hashmap = {'I': 1, 'IV': 4, 'IX': 9, 'XL': 40, 'XC': 90, 'CD': 400, 'CM': 900, 'V': 5, 'X': 1原创 2021-07-17 14:46:33 · 112 阅读 · 0 评论 -
整数转罗马数字 python
原文链接一个容易理解的方法class Solution(object): def intToRoman(self, num): """ :type num: int :rtype: str """ tmp = [] roman = '' if not num: raise ValueError while num >= 1000:转载 2021-07-17 13:54:41 · 81 阅读 · 0 评论 -
盛最多水的容器
给你 n 个非负整数 a1,a2,…,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0) 。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。说明:你不能倾斜容器。class Solution(object): def maxArea(self, height): """ :type height: List[int] :rtype: int原创 2021-07-17 13:33:09 · 69 阅读 · 0 评论 -
leetcode 字符串转换整数 (atoi)
class Solution(object): def myAtoi(self, x): """ :type x: int :rtype: int """ pp=1 xs=list(str(x)) xt=[] all_number=[str(p) for p in list(range(10)) ] pp原创 2021-07-17 13:18:06 · 124 阅读 · 0 评论 -
leetcode- 整数反转
# -*- coding: utf-8 -*-"""Created on Sat Jul 17 12:40:04 2021@author: luogantt"""class Solution(object): def reverse(self, x): """ :type x: int :rtype: int """ pp=1 xs=list(str(x)) if x<原创 2021-07-17 12:50:20 · 127 阅读 · 0 评论 -
leetcode-寻找两个正序数组的中位数
a=[1,2,9]b=[4,5]def middle(a,b): c=a+b c.sort() if len(c)%2==1: return c[int(len(c)/2)] else: return 0.5*( c[int(len(c)/2)-1]+ c[int(len(c)/2)]) print(middle(a,b))原创 2021-07-17 12:30:09 · 67 阅读 · 0 评论 -
python 无重复字符的最长子串
class Solution(object): def lengthOfLongestSubstring(self, s): """ :type s: str :rtype: int """ temp = '' length = 0 for i in s: if i not in temp: temp+=i原创 2021-07-17 12:16:50 · 71 阅读 · 0 评论 -
leetcode- 两数之和
# -*- coding: utf-8 -*-"""Spyder EditorThis is a temporary script file."""import timeimport numpy as np'''class Solution: def twoSum(self,nums, target): """ :type nums: List[int] :type target: int :rtype: Li原创 2021-07-16 14:43:20 · 101 阅读 · 0 评论