【Leetcode练习(Python):第342题:4的幂:给定一个整数 (32 位有符号整数),请编写一个函数来判断它是否是 4 的幂次方。】教程文章相关的互联网学习教程文章

leetcode第16题,最接近的三数之和,python实现【代码】

题目解析 该题目和三数之和类似,不同点是target也会发生变化,解答基本思路是根据要求不断变化target值,使其偏离原始target的程度越来越大,在此过程中检测当前的target值是否可以由数组中三数之和进行表示。 三数之和求法回顾 检查数组中是否存在三个数相加之和等于target值,思路为先将数组进行排序,然后进行遍历,遍历过程见代码。class Solution(object):def threeSumClosest(self, nums, target):""":type nums: List[int]...

Leetcode 167. 两数之和 II - 输入有序数组 By Python【代码】

给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数。 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2。 说明:返回的下标值(index1 和 index2)不是从零开始的。 你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素。示例: 输入: numbers = [2, 7, 11, 15], target = 9 输出: [1,2] 解释: 2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。思路 如果...

Leetcode 215. 数组中的第K个最大元素 By Python【代码】

在未排序的数组中找到第 k 个最大的元素。请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。 示例 1: 输入: [3,2,1,5,6,4] 和 k = 2 输出: 5 示例 2: 输入: [3,2,3,1,2,4,5,5,6] 和 k = 4 输出: 4思路 一个sorted再直接返回第K个最大元素就好了 代码 class Solution(object):def findKthLargest(self, nums, k):""":type nums: List[int]:type k: int:rtype: int"""return sorted(nums)[-k]

Leetcode 75.颜色分类 By Python【代码】

给定一个包含红色、白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。 此题中,我们使用整数 0、 1 和 2 分别表示红色、白色和蓝色。 注意: 不能使用代码库中的排序函数来解决这道题。 示例: 输入: [2,0,2,1,1,0] 输出: [0,0,1,1,2,2] 进阶:一个直观的解决方案是使用计数排序的两趟扫描算法。 首先,迭代计算出0、1 和 2 元素的个数,然后按照0、1、2的排序,重写...

[LeetCode&Python] Problem 897. Increasing Order Search Tree【代码】

Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only 1 right child. Example 1: Input: [5,3,6,2,4,null,8,1,null,null,null,7,9]5/ 3 6/ \ 2 4 8 / / \ 1 7 9Output: [1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]1 2 3 4 ...

[LeetCode&Python] Problem 412. Fizz Buzz【代码】

Write a program that outputs the string representation of numbers from 1 to n. But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”. Example: n = 15,Return: ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz" ]class Sol...

[LeetCode&Python] Problem 575. Distribute Candies【代码】

Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister. Return the maximum number of kinds of candies the sister could gain. Example 1: Input: candies = [1,1,2,2,3,3] Output: 3 Explanation: There are three ...

[LeetCode&Python] Problem 669. Trim a Binary Search Tree【代码】

Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the result should return the new root of the trimmed binary search tree. Example 1: Input: 1/ 0 2L = 1R = 2Output: 12 Example 2: Input: 3/ 0 42/1L = 1R = 3Output: 3/ 2 /1# Definition for a binary tr...

[LeetCode&Python] Problem 682. Baseball Game【代码】

Youre now a baseball game point recorder. Given a list of strings, each string can be one of the 4 following types:Integer (one rounds score): Directly represents the number of points you get in this round. "+" (one rounds score): Represents that the points you get in this round are the sum of the last two valid rounds points. "D" (one rounds score): Represents that the points you get in this roun...

LeetCode:139. Word Break - Python

问题描述: 139. 单词拆分 给定一个非空字符串s和一个包含非空单词列表的字典wordDict,判定s是否可以被空格拆分为一个或多个在字典中出现的单词。 说明:拆分时可以重复使用字典中的单词。 你可以假设字典中没有重复的单词。示例 :输入: s = “leetcode”, wordDict = [“leet”, “code”] 输出: true 解释: 返回 true 因为 “leetcode” 可以被拆分成 “leet code”。问题分析: ? ?这个题目很容易想到方法就是广度优先或者深度...

[LeetCode&Python] Problem 868. Binary Gap【代码】

Given a positive integer N, find and return the longest distance between two consecutive 1s in the binary representation of N. If there arent two consecutive 1s, return 0. ?Example 1: Input: 22 Output: 2 Explanation: 22 in binary is 0b10110. In the binary representation of 22, there are three ones, and two consecutive pairs of 1s. The first consecutive pair of 1s have distance 2. The second con...

LeetCode题目--字符串中的第一个唯一字符(python实现)【代码】

题目 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。 案例:s = "leetcode" 返回 0.s = "loveleetcode", 返回 2.注意事项:您可以假定该字符串只包含小写字母。 python代码实现: 方法一:class Solution:def firstUniqChar(self, s):""":type s: str:rtype: int"""dic = collections.Counter(s)l=len(s)for i in range(0,l):if dic[s[i]] == 1: # 如果字典中value为1return ireturn -1 ...

LeetCode题目--有效的数独(python/Java实现)【代码】【图】

题目 判断一个 9x9 的数独是否有效。只需要根据以下规则,验证已经填入的数字是否有效即可。 数字 1-9 在每一行只能出现一次。 数字 1-9 在每一列只能出现一次。 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。上图是一个部分填充的有效的数独。 数独部分空格内已填入了数字,空白格用 '.' 表示。 示例 1:输入: [["5","3",".",".","7",".",".",".","."],["6",".",".","1","9","5",".",".","."],[".","9","8",".",".","."...

【LeetCode】85. Maximal Rectangle 解题报告(Python)【代码】【图】

作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/题目地址: https://leetcode.com/problems/maximal-rectangle/description/ 题目描述: Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing only 1’s and return its area. Example: Input: [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"] ] Output: 6题目大意...

【LeetCode】221. Maximal Square 解题报告(Python)【代码】【图】

作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/题目地址: https://leetcode.com/problems/maximal-square/description/ 题目描述: Given a 2D binary matrix filled with 0’s and 1’s, find the largest square containing only 1’s and return its area. Example: Input: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0Output: 4题目大意 给出了一个二维的数组,求在这里面能够成的最大的正方形面积是...