Redtongue

人生苦短,我用Python


  • 首页

  • 归档

  • 分类

  • 标签

  • 关于

  • 搜索

975.Odd Even Jump(奇偶跳)

发表于 2019-01-14 | 更新于: 2019-01-18 | 分类于 leetcode

Description

You are given an integer array A. From some starting index, you can make a series of jumps. The (1st, 3rd, 5th, …) jumps in the series are called odd numbered jumps, and the (2nd, 4th, 6th, …) jumps in the series are called even numbered jumps.

You may from index i jump forward to index j (with i < j) in the following way:

  • During odd numbered jumps (ie. jumps 1, 3, 5, …), you jump to the index j such that A[i] <= A[j] and A[j] is the smallest possible value. If there are multiple such indexes j, you can only jump to the smallest such index j.
  • During even numbered jumps (ie. jumps 2, 4, 6, …), you jump to the index j such that A[i] >= A[j] and A[j] is the largest possible value. If there are multiple such indexes j, you can only jump to the smallest such index j.
  • (It may be the case that for some index i, there are no legal jumps.)

A starting index is good if, starting from that index, you can reach the end of the array (index A.length - 1) by jumping some number of times (possibly 0 or more than once.)

Return the number of good starting indexes.


给定一个整数数组 A,你可以从某一起始索引出发,跳跃一定次数。在你跳跃的过程中,第 1、3、5… 次跳跃称为奇数跳跃,而第 2、4、6… 次跳跃称为偶数跳跃。

你可以按以下方式从索引 i 向后跳转到索引 j(其中 i < j):

  • 在进行奇数跳跃时(如,第 1,3,5… 次跳跃),你将会跳到索引 j,使得 A[i] <= A[j],A[j] 是可能的最小值。如果存在多个这样的索引 j,你只能跳到满足要求的最小索引 j 上。
  • 在进行偶数跳跃时(如,第 2,4,6… 次跳跃),你将会跳到索引 j,使得 A[i] => A[j],A[j] 是可能的最大值。如果存在多个这样的索引 j,你只能跳到满足要求的最小索引 j 上。
  • (对于某些索引 i,可能无法进行合乎要求的跳跃。)

如果从某一索引开始跳跃一定次数(可能是 0 次或多次),就可以到达数组的末尾(索引 A.length - 1),那么该索引就会被认为是好的起始索引。

返回好的起始索引的数量。

题目链接:https://leetcode.com/problems/odd-even-jump/

Difficulty: hard

阅读全文 »

974.Subarray Sums Divisible by K(和可被 K 整除的子数组)

发表于 2019-01-14 | 更新于: 2019-01-18 | 分类于 leetcode

Description

Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.


给定一个整数数组 A,返回其中元素之和可被 K 整除的(连续、非空)子数组的数目。

题目链接:https://leetcode.com/problems/subarray-sums-divisible-by-k/

Difficulty: medium

阅读全文 »

973.K Cloest Points to Origin(最接近原点的 K 个点)

发表于 2019-01-14 | 更新于: 2019-01-18 | 分类于 leetcode

Description

We have a list of points on the plane. Find the K closest points to the origin (0, 0).

(Here, the distance between two points on a plane is the Euclidean distance.)

You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.)


我们有一个由平面上的点组成的列表 points。需要从中找出 K 个距离原点 (0, 0) 最近的点。

(这里,平面上两点之间的距离是欧几里德距离。)

你可以按任何顺序返回答案。除了点坐标的顺序之外,答案确保是唯一的。

题目链接:https://leetcode.com/problems/k-closest-points-to-origin/

Difficulty: easy

阅读全文 »

964.Least Operators to Express Number(表示数字的最少运算符)

发表于 2018-12-23 | 更新于: 2018-12-26 | 分类于 leetcode

Description

Given a single positive integer x, we will write an expression of the form x (op1) x (op2) x (op3) x ... where each operator op1, op2, etc. is either addition, subtraction, multiplication, or division (+, -, *, or /). For example, with x = 3, we might write 3 * 3 / 3 + 3 - 3 which is a value of 3.

When writing such an expression, we adhere to the following conventions:

  1. The division operator (/) returns rational numbers.
  2. There are no parentheses placed anywhere.
  3. We use the usual order of operations: multiplication and division happens before addition and subtraction.
  4. It’s not allowed to use the unary negation operator (-). For example, “x - x“ is a valid expression as it only uses subtraction, but “-x + x“ is not because it uses negation.

We would like to write an expression with the least number of operators such that the expression equals the given target. Return the least number of operators used.


给定一个正整数 x,我们将会写出一个形如 x (op1) x (op2) x (op3) x … 的表达式,其中每个运算符 op1,op2,… 可以是加、减、乘、除(+,-,,或是 /)之一。例如,对于 x = 3,我们可以写出表达式 3 3 / 3 + 3 - 3,该式的值为 3 。

在写这样的表达式时,我们需要遵守下面的惯例:

  1. 除运算符(/)返回有理数。
  2. 任何地方都没有括号。
  3. 我们使用通常的操作顺序:乘法和除法发生在加法和减法之前。
  4. 不允许使用一元否定运算符(-)。例如,“x - x” 是一个有效的表达式,因为它只使用减法,但是 “-x + x” 不是,因为它使用了否定运算符。

我们希望编写一个能使表达式等于给定的目标值 target 且运算符最少的表达式。返回所用运算符的最少数量。

题目链接:https://leetcode.com/problems/least-operators-to-express-number/

Difficulty: hard

阅读全文 »

963.Minimum Area Rectangle II(最小面积矩形 II)

发表于 2018-12-23 | 更新于: 2018-12-26 | 分类于 leetcode

Description

Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from these points, with sides not necessarily parallel to the x and y axes.

If there isn’t any rectangle, return 0.


给定在 xy 平面上的一组点,确定由这些点组成的任何矩形的最小面积,其中矩形的边不一定平行于 x 轴和 y 轴。

如果没有任何矩形,就返回 0。

题目链接:https://leetcode.com/problems/minimum-area-rectangle-ii/

Difficulty: medium

阅读全文 »

962.Maximum Width Ramp(最大宽度坡)

发表于 2018-12-23 | 更新于: 2018-12-26 | 分类于 leetcode

Description

Given an array A of integers, a ramp is a tuple (i, j) for which i < j and A[i] <= A[j]. The width of such a ramp is j - i.

Find the maximum width of a ramp in A. If one doesn’t exist, return 0.


给定一个整数数组 A,坡是元组 (i, j),其中 i < j 且 A[i] <= A[j]。这样的坡的宽度为 j - i。

找出 A 中的坡的最大宽度,如果不存在,返回 0 。

题目链接:https://leetcode.com/problems/maximum-width-ramp/

Difficulty: medium

阅读全文 »

961.N-Repeated Element in Size 2N Array(重复 N 次的元素)

发表于 2018-12-23 | 更新于: 2018-12-26 | 分类于 leetcode

Description

In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeated N times.

Return the element repeated N times.


在大小为 2N 的数组 A 中有 N+1 个不同的元素,其中有一个元素重复了 N 次。

返回重复了 N 次的那个元素。

题目链接:https://leetcode.com/problems/n-repeated-element-in-size-2n-array

Difficulty: easy

阅读全文 »

960.Delete Columns to Make Sorted III

发表于 2018-12-19 | 更新于: 2018-12-19 | 分类于 leetcode

Description

We are given an array A of N lowercase letter strings, all of the same length.

Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices.

For example, if we have an array A = ["babca","bbazb"] and deletion indices {0, 1, 4}, then the final array after deletions is ["bc","az"].

Suppose we chose a set of deletion indices D such that after deletions, the final array has every element (row) in lexicographic order.

For clarity, A[0] is in lexicographic order (ie. A[0][0] <= A[0][1] <= ... <= A[0][A[0].length - 1]), A[1] is in lexicographic order (ie. A[1][0] <= A[1][1] <= ... <= A[1][A[1].length - 1]), and so on.

Return the minimum possible value of D.length.


给定由 N 个小写字母字符串组成的数组 A,其中每个字符串长度相等。

选取一个删除索引序列,对于 A 中的每个字符串,删除对应每个索引处的字符。

比如,有 A = ["babca","bbazb"],删除索引序列 {0, 1, 4},删除后 A 为["bc","az"]。

假设,我们选择了一组删除索引 D,那么在执行删除操作之后,最终得到的数组的行中的每个元素都是按字典序排列的。

清楚起见,A[0] 是按字典序排列的(即,A[0][0] <= A[0][1] <= ... <= A[0][A[0].length - 1]),A[1] 是按字典序排列的(即,A[1][0] <= A[1][1] <= ... <= A[1][A[1].length - 1]),依此类推。

请你返回 D.length 的最小可能值。

题目链接:https://leetcode.com/problems/delete-columns-to-make-sorted-iii/

Difficulty: hard

阅读全文 »

959.Regions Cut By Slashes

发表于 2018-12-19 | 更新于: 2018-12-19 | 分类于 leetcode

Description

In a N x N grid composed of 1 x 1 squares, each 1 x 1 square consists of a
/, \, or blank space. These characters divide the square into contiguous regions.

(Note that backslash characters are escaped, so a \ is represented as “\“.)

Return the number of regions.


在由 1 x 1 方格组成的 N x N 网格 grid 中,每个 1 x 1 方块由 /、\ 或空格构成。这些字符会将方块划分为一些共边的区域。

(请注意,反斜杠字符是转义的,因此 \ 用 “\“ 表示。)。

题目链接:https://leetcode.com/problems/regions-cut-by-slashes/

Difficulty: medium

阅读全文 »

958.Check Completeness of a Binary Tree

发表于 2018-12-19 | 更新于: 2018-12-19 | 分类于 leetcode

Description

Given a binary tree, determine if it is a complete binary tree.

Definition of a complete binary tree from Wikipedia:

In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.


给定一个二叉树,确定它是否是一个完全二叉树。

百度百科中对完全二叉树的定义如下:

若设二叉树的深度为 h,除第 h 层外,其它各层 (1~h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树。(注:第 h 层可能包含 1~ 2h 个节点。)

题目链接:https://leetcode.com/problems/check-completeness-of-a-binary-tree/

Difficulty: medium

阅读全文 »
1…456…21
yunxiang wang

yunxiang wang

记录点点

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