新手刷Leetcode
文章平均质量分 63
水喵喵喵
这个作者很懒,什么都没留下…
展开
-
[LeetCode]Medium - Cutting Ribbons - python
在csdn上发现这道题只有java或者cpp的解答,补个python版本的解法吧。思路:就是二分法,之后空一点了再写。class Solution: def maxLength(self, ribbons: List[int], k: int) -> int: def possible(size): num_rib = 0 for rib in ribbons: num_rib +=原创 2021-11-13 03:19:10 · 942 阅读 · 0 评论 -
[LeetCode]easy - Climbing Stairs - python
题目要求:给定一个整数n,是台阶的级数。每次只能向上一级台阶或者两级台阶,求有几种不同的方式可以爬完n级台阶。Problem Description:You are climbing a staircase. It takesnsteps to reach the top.Each time you can either climb1or2steps. In how many distinct ways can you climb to the top?题目链接...原创 2021-10-02 02:25:15 · 80 阅读 · 0 评论 -
[LeetCode]easy - Sqrt(x) - python
题目要求:求一个非负数的平方根,精确到整数位。Problem Description:Given a non-negative integerx,compute and returnthe square root ofx.Since the return typeis an integer, the decimal digits aretruncated, and onlythe integer partof the resultis returned.Note:...原创 2021-10-02 02:16:50 · 82 阅读 · 0 评论 -
[LeetCode]easy - Add Binary - python
题目要求:求两个二进制数的和,返回值也是二进制形式的。Problem Description:Given two binary stringsaandb, returntheir sum as a binary string.题目链接思路一:直接利用python自带函数 int() 将二进制数转化为十进制数进行相加,然后将和转化为二进制数返回,此处取 [2:]是因为要去掉开头的0b标识符。这样的处理速度真的很快。class Solution(object): ...原创 2021-10-02 02:08:32 · 54 阅读 · 0 评论 -
[LeetCode]easy - Plus One - python
题目要求:给定一个非常大的整数,返回它加一后的结果。Problem Description:You are given alarge integerrepresented as an integer arraydigits, where eachdigits[i]is theithdigit of the integer. The digits are ordered from most significant to least significant in left-to-r...原创 2021-10-02 01:59:28 · 92 阅读 · 0 评论 -
[LeetCode]easy - Length of Last Word - python
题目要求:输入一个由一些单词与空格组成的字符串,输出其最后一个单词的长度。Problem Description:Given a stringsconsistingof some words separated by some number of spaces, returnthe length of thelastword in the string.Awordis a maximal substring consisting of non-space characters...原创 2021-10-02 01:49:30 · 61 阅读 · 0 评论 -
[LeetCode]easy - Maximum Subarray - python
Problem Description:题目链接class Solution(object): def maxSubArray(self, nums): """ :type nums: List[int] :rtype: int """ max_sum = nums[0] for i in range(0,len(nums)): current_sum = num原创 2021-09-30 02:16:30 · 148 阅读 · 0 评论 -
[LeetCode]easy - Search Insert Position - python
Problem Description:题目链接class Solution(object): def searchInsert(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """ length = len(nums) left = 0 right =原创 2021-09-28 05:26:57 · 88 阅读 · 0 评论 -
[LeetCode]easy - Implement strStr() - python
Problem Description:题目链接class Solution(object): def strStr(self, haystack, needle): """ :type haystack: str :type needle: str :rtype: int """ if needle == '': return 0 else:原创 2021-09-28 05:13:02 · 84 阅读 · 0 评论 -
[LeetCode]easy - Remove Element - python
Problem Description:Given an integer arraynumsand an integerval, remove all occurrences ofvalinnumsin-place. The relative order of the elements may be changed.Since it is impossible to change the length of the array in some languages, you must...原创 2021-09-27 22:11:21 · 154 阅读 · 0 评论 -
[LeetCode]easy - Remove Duplicates from Sorted Array - python
Problem Description:Given an integer arraynumssorted innon-decreasing order, remove the duplicatesin-placesuch that each unique element appears onlyonce. Therelative orderof the elements should be kept thesame.Since it is impossible to chang...原创 2021-09-27 22:02:35 · 79 阅读 · 0 评论 -
[LeetCode]easy - Merge Two Sorted Lists - python
Problem Description:Merge two sorted linked lists and return it as asortedlist. The list should be made by splicing together the nodes of the first two lists.题目链接题目要求# Definition for singly-linked list.# class ListNode(object):# def __init..原创 2021-09-27 02:17:22 · 60 阅读 · 0 评论 -
[LeetCode]easy - Valid Parentheses - python
Problem Description:Given a stringscontaining just the characters'(',')','{','}','['and']', determine if the input string is valid.An input string is valid if:Open brackets must be closed by the same type of brackets. Open brackets must b...原创 2021-09-25 23:09:14 · 60 阅读 · 0 评论 -
[LeetCode]easy - Longest Common Prefix - python
Problem Description:Write a function to find the longest common prefix string amongst an array of strings.If there is no common prefix, return an empty string"".题目链接题目要求思路一:class Solution(object): def longestCommonPrefix(self, strs):.原创 2021-09-25 22:35:24 · 58 阅读 · 0 评论 -
[LeetCode]easy - Roman to Integer - python
Problem Description:class Solution(object): def romanToInt(self, s): """ :type s: str :rtype: int """ roman = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000} z = roman[s[0]] for i原创 2021-09-25 10:13:57 · 84 阅读 · 0 评论 -
[LeetCode]easy - Palindrome Number - python
Problem Description:Given an integerx, returntrueifxis palindrome integer.An integer is apalindromewhen it reads the same backward as forward. For example,121is palindrome while123is not.class Solution(object): def isPalin...原创 2021-09-25 10:05:08 · 124 阅读 · 0 评论 -
[LeetCode]easy - Reverse Integer - python
Problem Description:Given a signed 32-bit integerx, returnxwith its digits reversed. If reversingxcauses the value to go outside the signed 32-bit integer, then return0.题目要求反转整数中的数字。思路一:首先判断原数字的正负,用flag记录一下。通过循环对10取余得到尾部数字,一步步乘10构造新的翻转后的整数。最后...原创 2021-09-25 00:24:26 · 102 阅读 · 0 评论 -
[LeetCode]easy - Two Sum - python
Problem Description:Given an array of integersnumsand an integertarget, returnindices of the two numbers such that they add up totarget.You may assume that each input would haveexactlyone solution, and you may not use thesameelement twice.Yo...原创 2021-09-24 04:56:01 · 82 阅读 · 0 评论