Redtongue

人生苦短,我用Python


  • 首页

  • 归档

  • 分类

  • 标签

  • 关于

  • 搜索

997.Find the Town Judge(找到小镇的法官)

发表于 2019-02-24 | 更新于: 2019-02-24 | 分类于 leetcode

Description

In a town, there are N people labelled from 1 to N. There is a rumor that one of these people is secretly the town judge.

If the town judge exists, then:

  1. The town judge trusts nobody.
  2. Everybody (except for the town judge) trusts the town judge.
  3. There is exactly one person that satisfies properties 1 and 2.

You are given trust, an array of pairs trust[i] = [a, b] representing that the person labelled a trusts the person labelled b.

If the town judge exists and can be identified, return the label of the town judge. Otherwise, return -1.


在一个小镇里,按从 1 到 N 标记了 N 个人。传言称,这些人中有一个是小镇上的秘密法官。

如果小镇的法官真的存在,那么:

  1. 小镇的法官不相信任何人。
  2. 每个人(除了小镇法官外)都信任小镇的法官。
  3. 只有一个人同时满足属性 1 和属性 2 。

给定数组 trust,该数组由信任对 trust[i] = [a, b] 组成,表示标记为 a 的人信任标记为 b 的人。

如果小镇存在秘密法官并且可以确定他的身份,请返回该法官的标记。否则,返回 -1。

题目链接:https://leetcode.com/problems/find-the-town-judge/

Difficulty: easy

阅读全文 »

996.Number of Sequareful Arrays(正方形数组的数目)

发表于 2019-02-17 | 更新于: 2019-02-18 | 分类于 leetcode

Description

Given an array A of non-negative integers, the array is squareful if for every pair of adjacent elements, their sum is a perfect square.

Return the number of permutations of A that are squareful. Two permutations A1 and A2 differ if and only if there is some index i such that A1[i] != A2[i].


给定一个非负整数数组 A,如果该数组每对相邻元素之和是一个完全平方数,则称这一数组为正方形数组。

返回 A 的正方形排列的数目。两个排列 A1 和 A2 不同的充要条件是存在某个索引 i,使得 A1[i] != A2[i]。

题目链接:https://leetcode.com/problems/number-of-squareful-arrays/

Difficulty: hard

阅读全文 »

995.Minimum Number of K Consecutive Bit Flips(K 连续位的最小翻转次数 )

发表于 2019-02-17 | 更新于: 2019-02-22 | 分类于 leetcode

Description

In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0.

Return the minimum number of K-bit flips required so that there is no 0 in the array. If it is not possible, return -1.


在仅包含 0 和 1 的数组 A 中,一次 K 位翻转包括选择一个长度为 K 的(连续)子数组,同时将子数组中的每个 0 更改为 1,而每个 1 更改为 0。

返回所需的 K 位翻转的次数,以便数组没有值为 0 的元素。如果不可能,返回 -1。

题目链接:https://leetcode.com/problems/minimum-number-of-k-consecutive-bit-flips/

Difficulty: hard

阅读全文 »

994.Rotting Oranges(腐烂的橘子 )

发表于 2019-02-17 | 更新于: 2019-02-22 | 分类于 leetcode

Description

In a given grid, each cell can have one of three values:

  • the value 0 representing an empty cell;
  • the value 1 representing a fresh orange;
  • the value 2 representing a rotten orange.

Every minute, any fresh orange that is adjacent (4-directionally) to a rotten orange becomes rotten.

Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, return -1 instead.


在给定的网格中,每个单元格可以有以下三个值之一:

  • 值 0 代表空单元格;

  • 值 1 代表新鲜橘子;

  • 值 2 代表腐烂的橘子。

每分钟,任何与腐烂的橘子(在 4 个正方向上)相邻的新鲜橘子都会腐烂。

返回直到单元格中没有新鲜橘子为止所必须经过的最小分钟数。如果不可能,返回 -1。

题目链接:https://leetcode.com/problems/rotting-oranges/

Difficulty: easy

阅读全文 »

993.Cousins in Binary Tree(二叉树的堂兄弟节点 )

发表于 2019-02-17 | 更新于: 2019-02-22 | 分类于 leetcode

Description

In a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1.

Two nodes of a binary tree are cousins if they have the same depth, but have different parents.

We are given the root of a binary tree with unique values, and the values x and y of two different nodes in the tree.
Return true if and only if the nodes corresponding to the values x and y are cousins.


在二叉树中,根节点位于深度 0 处,每个深度为 k 的节点的子节点位于深度 k+1 处。

如果二叉树的两个节点深度相同,但父节点不同,则它们是一对堂兄弟节点。

我们给出了具有唯一值的二叉树的根节点 root,以及树中两个不同节点的值 x 和 y。

只有与值 x 和 y 对应的节点是堂兄弟节点时,才返回 true。否则,返回 false。

题目链接:https://leetcode.com/problems/cousins-in-binary-tree/

Difficulty: easy

阅读全文 »

980.Unique Paths III(不同路径 III)

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

Description

On a 2-dimensional grid, there are 4 types of squares:

  • 1 represents the starting square. There is exactly one starting square.
  • 2 represents the ending square. There is exactly one ending square.
  • 0 represents empty squares we can walk over.
  • -1 represents obstacles that we cannot walk over.

Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square exactly once.


在二维网格 grid 上,有 4 种类型的方格:

  • 1 表示起始方格。且只有一个起始方格。
  • 2 表示结束方格,且只有一个结束方格。
  • 0 表示我们可以走过的空方格。
  • -1 表示我们无法跨越的障碍。

返回在四个方向(上、下、左、右)上行走时,从起始方格到结束方格的不同路径的数目,每一个无障碍方格都要通过一次。

题目链接:https://leetcode.com/problems/unique-paths-iii/

Difficulty: hard

阅读全文 »

979.Distribute Coins in Binary Tree(在二叉树中分配硬币)

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

Description

Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and there are N coins total.

In one move, we may choose two adjacent nodes and move one coin from one node to another. (The move may be from parent to child, or from child to parent.)

Return the number of moves required to make every node have exactly one coin.


给定一个有 N 个结点的二叉树的根结点 root,树中的每个结点上都对应有 node.val 枚硬币,并且总共有 N 枚硬币。

在一次移动中,我们可以选择两个相邻的结点,然后将一枚硬币从其中一个结点移动到另一个结点。(移动可以是从父结点到子结点,或者从子结点移动到父结点。)。

返回使每个结点上只有一枚硬币所需的移动次数。

题目链接:https://leetcode.com/problems/distribute-coins-in-binary-tree/

Difficulty: medium

阅读全文 »

978.Longest Turbulent Subarray(最长湍流子数组)

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

Description

A subarray A[i], A[i+1], ..., A[j] of A is said to be turbulent if and only if:

For i <= k < j, A[k] > A[k+1] when k is odd, and A[k] < A[k+1] when k is even;
OR, for i <= k < j, A[k] > A[k+1] when k is even, and A[k] < A[k+1]when k is odd.
That is, the subarray is turbulent if the comparison sign flips between each adjacent pair of elements in the subarray.

Return the length of a maximum size turbulent subarray of A.


当 A 的子数组 A[i], A[i+1], ..., A[j] 满足下列条件时,我们称其为湍流子数组:

若 i <= k < j,当 k 为奇数时, A[k] > A[k+1],且当 k 为偶数时,A[k] < A[k+1];
或 若 i <= k < j,当 k 为偶数时,A[k] > A[k+1] ,且当 k 为奇数时, A[k] < A[k+1]。
也就是说,如果比较符号在子数组中的每个相邻元素对之间翻转,则该子数组是湍流子数组。

返回 A 的最大湍流子数组的长度。

题目链接:https://leetcode.com/problems/longest-turbulent-subarray/

Difficulty: medium

阅读全文 »

977.Squares of a Sorted Array(有序数组的平方)

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

Description

Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order.


给定一个按非递减顺序排序的整数数组 A,返回每个数字的平方组成的新数组,要求也按非递减顺序排序。

题目链接:https://leetcode.com/problems/squares-of-a-sorted-array/

Difficulty: easy

阅读全文 »

976.Largest Perimeter Triangle(三角形的最大周长)

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

Description

Given an array A of positive lengths, return the largest perimeter of a triangle with non-zero area, formed from 3 of these lengths.

If it is impossible to form any triangle of non-zero area, return 0.


给定由一些正数(代表长度)组成的数组 A,返回由其中三个长度组成的、面积不为零的三角形的最大周长。

如果不能形成任何面积不为零的三角形,返回 0。

题目链接:https://leetcode.com/problems/largest-perimeter-triangle/

Difficulty: easy

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

yunxiang wang

记录点点

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