Redtongue

人生苦短,我用Python


  • 首页

  • 归档

  • 分类

  • 标签

  • 关于

  • 搜索

1016.Binary String With Substrings Representing 1 To N(子串能表示从 1 到 N 数字的二进制串)

发表于 2019-03-25 | 更新于: 2019-03-31 | 分类于 leetcode

Description

Given a binary string S (a string consisting only of ‘0’ and ‘1’s) and a positive integer N, return true if and only if for every integer X from 1 to N, the binary representation of X is a substring of S.


给定一个二进制字符串 S(一个仅由若干 ‘0’ 和 ‘1’ 构成的字符串)和一个正整数 N,如果对于从 1 到 N 的每个整数 X,其二进制表示都是 S 的子串,就返回 true,否则返回 false。

题目链接:https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/

Difficulty: medium

阅读全文 »

1015.Smallest Integer Divisible by K(可被 K 整除的最小整数)

发表于 2019-03-25 | 更新于: 2019-03-31 | 分类于 leetcode

Description

Given a positive integer K, you need find the smallest positive integer N such that N is divisible by K, and N only contains the digit 1.

Return the length of N. If there is no such N, return -1.


给定正整数 K,你需要找出可以被 K 整除的、仅包含数字 1 的最小正整数 N。

返回 N 的长度。如果不存在这样的 N,就返回 -1。

题目链接:https://leetcode.com/problems/smallest-integer-divisible-by-k/

Difficulty: medium

阅读全文 »

1014.Best Sightseeing Pair(最佳观光组合)

发表于 2019-03-25 | 更新于: 2019-03-31 | 分类于 leetcode

Description

Given an array A of positive integers, A[i] represents the value of the i-th sightseeing spot, and two sightseeing spots i and j have distance j - i between them.

The score of a pair (i < j) of sightseeing spots is (A[i] + A[j] + i - j) : the sum of the values of the sightseeing spots, minus the distance between them.

Return the maximum score of a pair of sightseeing spots.


给定正整数数组 A,A[i] 表示第 i 个观光景点的评分,并且两个景点 i 和 j 之间的距离为 j - i。

一对景点(i < j)组成的观光组合的得分为(A[i] + A[j] + i - j):景点的评分之和减去它们两者之间的距离。

返回一对观光景点能取得的最高分。

题目链接:https://leetcode.com/problems/best-sightseeing-pair/

Difficulty: medium

阅读全文 »

1013.Partition Array Into Three Parts With Equal Sum(将数组分成和相等的三个部分)

发表于 2019-03-25 | 更新于: 2019-03-31 | 分类于 leetcode

Description

Given an array A of integers, return true if and only if we can partition the array into three non-empty parts with equal sums.

Formally, we can partition the array if we can find indexes i+1 < j with (A[0] + A[1] + ... + A[i] == A[i+1] + A[i+2] + ... + A[j-1] == A[j] + A[j-1] + ... + A[A.length - 1])


给定一个整数数组 A,只有我们可以将其划分为三个和相等的非空部分时才返回 true,否则返回 false。

形式上,如果我们可以找出索引 i+1 < j 且满足 (A[0] + A[1] + ... + A[i] == A[i+1] + A[i+2] + ... + A[j-1] == A[j] + A[j-1] + ... + A[A.length - 1]) 就可以将数组三等分。

题目链接:https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/

Difficulty: easy

阅读全文 »

topk:数组中的k项最大值//二维数组中的k项最大值

发表于 2019-03-21 | 更新于: 2019-03-25 | 分类于 whatever

从n个数字中找出前top-k大的数字是常见的面试题,有如下几种解法:

暴力解法

这个基本思路类似于冒泡排序或者插入排序,以此遍历找到最大的数,最后返回k个最大的数,时间复杂度为 $O(NK)$

阅读全文 »

1012.Numbers With Repeated Digits

发表于 2019-03-17 | 更新于: 2019-03-31 | 分类于 leetcode

Description

Given a positive integer N, return the number of positive integers less than or equal to N that have at least 1 repeated digit.(test)


给定正整数 N,返回小于等于 N 且具有至少 1 位重复数字的正整数。

题目链接:https://leetcode.com/problems/numbers-with-repeated-digits/

Difficulty: hard

阅读全文 »

1011.Capacity To Ship Packages Within D Days(在 D 天内送达包裹的能力)

发表于 2019-03-17 | 更新于: 2019-03-31 | 分类于 leetcode

Description

A conveyor belt has packages that must be shipped from one port to another within D days.

The i-th package on the conveyor belt has a weight of weights[i]. Each day, we load the ship with packages on the conveyor belt (in the order given by weights). We may not load more weight than the maximum weight capacity of the ship.

Return the least weight capacity of the ship that will result in all the packages on the conveyor belt being shipped within D days.


传送带上的包裹必须在 D 天内从一个港口运送到另一个港口。

传送带上的第 i 个包裹的重量为 weights[i]。每一天,我们都会按给出重量的顺序往传送带上装载包裹。我们装载的重量不会超过船的最大运载重量。

返回能在 D 天内将传送带上的所有包裹送达的船的最低运载能力。

题目链接:https://leetcode.com/problems/capacity-to-ship-packages-within-d-days/

Difficulty: medium

阅读全文 »

1010.Pairs of Songs With Total Durations Divisible by 60(总持续时间可被 60 整除的歌曲)

发表于 2019-03-17 | 更新于: 2019-03-31 | 分类于 leetcode

Description

In a list of songs, the i-th song has a duration of time[i] seconds.

Return the number of pairs of songs for which their total duration in seconds is divisible by 60. Formally, we want the number of indices i < j with (time[i] + time[j]) % 60 == 0.


在歌曲列表中,第 i 首歌曲的持续时间为 time[i] 秒。

返回其总持续时间(以秒为单位)可被 60 整除的歌曲对的数量。形式上,我们希望索引的数字 i < j 且有 (time[i] + time[j]) % 60 == 0。

题目链接:https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/

Difficulty: easy

阅读全文 »

1009.Complement of Base 10 Integer(十进制整数的补码)

发表于 2019-03-17 | 更新于: 2019-03-31 | 分类于 leetcode

Description

Every non-negative integer N has a binary representation. For example, 5 can be represented as "101" in binary, 11 as "1011" in binary, and so on. Note that except for N = 0, there are no leading zeroes in any binary representation.

The complement of a binary representation is the number in binary you get when changing every 1 to a 0 and 0 to a 1. For example, the complement of “101” in binary is “010” in binary.

For a given number N in base-10, return the complement of it’s binary representation as a base-10 integer.


每个非负整数 N 都有其二进制表示。例如, 5 可以被表示为二进制 "101",11 可以用二进制 "1011" 表示,依此类推。注意,除 N = 0 外,任何二进制表示中都不含前导零。

二进制的补码表示是将每个 1 改为 0 且每个 0 变为 1。例如,二进制数 "101" 的二进制补码为 "010"。

给定十进制数 N,返回其二进制表示的补码所对应的十进制整数。

题目链接:https://leetcode.com/problems/complement-of-base-10-integer/

Difficulty: easy

阅读全文 »

1008.Construct Binary Search Tree From Preorder Traversal(先序遍历构造二叉树)

发表于 2019-03-10 | 更新于: 2019-03-10 | 分类于 leetcode

Description

Return the root node of a binary search tree that matches the given preorder traversal.

(Recall that a binary search tree is a binary tree where for every node, any descendant of node.left has a value < node.val, and any descendant of node.right has a value > node.val. Also recall that a preorder traversal displays the value of the node first, then traverses node.left, then traverses node.right.)


返回与给定先序遍历 preorder 相匹配的二叉搜索树(binary search tree)的根结点。

(回想一下,二叉搜索树是二叉树的一种,其每个节点都满足以下规则,对于 node.left 的任何后代,值总 < node.val,而 node.right 的任何后代,值总 > node.val。此外,先序遍历首先显示节点的值,然后遍历 node.left,接着遍历 node.right。)

题目链接:https://leetcode.com/problems/construct-binary-search-tree-from-preorder-traversal/

Difficulty: medium

阅读全文 »
123…21
yunxiang wang

yunxiang wang

记录点点

202 日志
4 分类
41 标签
GitHub E-Mail
© 2016 — 2020 yunxiang wang