Word Break Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.For example, givens = "leetcode",dict = ["leet", "code"].Return true because "leetcode" can be segmented as "leet code". python code:class Solution: # @param s, a string # @param wordDict, a set<string> # @return a boolean def word...
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.这题用的是count sort 的方法。首先我们来复习一下count sort已知一个正整数还未排列数列。 先遍历一遍得到最大值,建一个counter数组。再将counter里面的数依次赋值到原来的数组当中去。代码如下def counts(A):maxval...
题目:填充每个节点的下一个右侧节点指针:给定一个完美二叉树,其所有叶子节点都在同一层,每个父节点都有两个子节点。给定一个完美二叉树,其所有叶子节点都在同一层,每个父节点都有两个子节点。二叉树定义如下:struct Node { int val; Node *left; Node *right; Node *next;}填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。初始状态下,所有 next 指针都...
ProblemLeetCodeGiven a Binary Search Tree (BST) with the root node root, return the minimum difference between the values of any two different nodes in the tree.Example :Input: root = [4,2,6,1,3,null,null]
Output: 1
Explanation:
Note that root is a TreeNode object, not an array.The given tree [4,2,6,1,3,null,null] is represented by the following diagram:4/ 2 6/ \ 1 3 while the m...
问题描述:给出一个无重叠的 ,按照区间起始端点排序的区间列表。在列表中插入一个新的区间,你需要确保列表中的区间仍然有序且不重叠(如果有必要的话,可以合并区间)。示例 1:输入: intervals = [[1,3],[6,9]], newInterval = [2,5]输出: [[1,5],[6,9]]示例 2:输入: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]输出: [[1,2],[3,10],[12,16]]解释: 这是因为新的区间 [4,8] 与 [3,5],[6,7],[8,10] 重叠...
存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除链表中所有存在数字重复情况的节点,只保留原始链表中 没有重复出现 的数字。返回同样按升序排列的结果链表。提示:链表中节点数目在范围 [0, 300] 内-100 <= Node.val <= 100题目数据保证链表已经按升序排列# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next =...
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation of s1 = "great": great/ gr eat/ \ / g r e at/ a t
To scramble the string, we may choose any non-leaf node and swap its two children.For example, if we choose the node "gr" and swap its two children, it produces a scr...
题目来源: https://leetcode.com/problems/sudoku-solver/ 题意分析: 这次的题目就是上一题的进化版。填好一个数独。 题目思路: 这题直接用dfs暴力解决。把“*”用(1-9)直接填就行。时间复杂度比较高。要注意的是,题目要求没有返回值,所以要另外写一个函数用来判断填数之后是否满足可以填好。 代码(python): 1class Solution(object):2def isValue(self,board,x,y):3#列符合 4for i in range(9):5if i != x and...
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note:
Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
The solution set must not contain duplicate quadruplets.
For example, given array S = {1 0 -1 0 -2 2}, and target = 0.
...
?这是啥算法题。。逐个判断写个循环即可class Solution:def fizzBuzz(self, n: int) -> List[str]:result = []for i in range(1,n+1):if i % 3 == 0 and i % 5 == 0 :result.append(‘FizzBuzz‘)elif i%3==0:result.append(‘Fizz‘)elif i%5==0:result.append(‘Buzz‘)else:result.append(str(i))return result 原文:https://www.cnblogs.com/cbachen/p/14867481.html
84. 柱状图中最大的矩形题目来源:力扣(LeetCode)https://leetcode-cn.com/problems/largest-rectangle-in-histogram/题目给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。求在该柱状图中,能够勾勒出来的矩形的最大面积。以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高度为 [2,1,5,6,2,3]。图中阴影部分为所能勾勒出的最大矩形面积,其面积为 10 个单位。示例:输入: [2,1,5,6,2,3]...
#很简单,利用strip函数去除左右两边的空格,然后用split函数分割成列表class Solution(object): def lengthOfLastWord(self, s): """ :type s: str :rtype: int """ if s==None:return 0 slist=s.strip().split(‘ ‘) return len(slist[-1])原文:http://www.cnblogs.com/kwangeline/p/6016917.html
Majority Element Given an array of size n, find the majority element. The majority element is the element that appears more than ? n/2 ? times.You may assume that the array is non-empty and the majority element always exist in the array. python code:class Solution: # @param {integer[]} nums # @return {integer} def majorityElement(self, nums): b={} for eachnum in num...
题目来源: https://leetcode.com/problems/powx-n/ 题意分析: 实现一个整型的幂运算。 题目思路: 幂运算可以利用二分的方法来做。也就是x^n = x ^ (n /2) * x ^(n / 2) (n %2 == 0)或者x^n = x ^ (n /2) * x ^(n / 2) * x(n %2 == 1)。要注意的时候,当n < 0 的时候,x ^ n =1 / (x ^(-n))。 代码(python):class Solution(object):def myPow(self, x, n):""":type x: float:type n: int:rtype: float"""if n < 0:...
题目来源: https://leetcode.com/problems/trapping-rain-water/ 题意分析: 输入一组数组,代表一个宽度为1的高度地图。问,这个地图在雨后可以收集多少水。例如,输入一个数组[0,1,0,2,1,0,1,3,2,1,2,1],返回的是6.如图所示: 题目思路: 这道题目虽然说是hard难度的题目,但是其实不是很难。不难发现,水都是从最高那个数起和第二高数之间。那么这题可以分成两部。①找到数组的最大值。②计算最大值左边和右边分别可...