-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
1,262 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# 奇数值单元格的数目 | ||
|
||
## 题目 | ||
|
||
https://leetcode-cn.com/problems/cells-with-odd-values-in-a-matrix/ | ||
|
||
## 描述 | ||
|
||
给你一个 n 行 m 列的矩阵,最开始的时候,每个单元格中的值都是 0。 | ||
|
||
另有一个索引数组 indices,indices[i] = [ri, ci] 中的 ri 和 ci 分别表示指定的行和列(从 0 开始编号)。 | ||
|
||
你需要将每对 [ri, ci] 指定的行和列上的所有单元格的值加 1。 | ||
|
||
请你在执行完所有 indices 指定的增量操作后,返回矩阵中 「奇数值单元格」 的数目。 | ||
|
||
 | ||
|
||
``` | ||
输入:n = 2, m = 3, indices = [[0,1],[1,1]] | ||
输出:6 | ||
解释:最开始的矩阵是 [[0,0,0],[0,0,0]]。 | ||
第一次增量操作后得到 [[1,2,1],[0,1,0]]。 | ||
最后的矩阵是 [[1,3,1],[1,3,1]],里面有 6 个奇数。 | ||
``` | ||
|
||
示例2: | ||
|
||
 | ||
|
||
``` | ||
输入:n = 2, m = 2, indices = [[1,1],[0,0]] | ||
输出:0 | ||
解释:最后的矩阵是 [[2,2],[2,2]],里面没有奇数。 | ||
``` | ||
|
||
## 代码 | ||
|
||
### 解法1 | ||
|
||
首先想到的就是暴力法 | ||
|
||
- 既然给我一个矩阵,那我就先构造一下呗 | ||
- 然后遍历indices里面每个元素,拆包ri和ci | ||
- 根据要求,对构造的矩阵进行行列+1操作 | ||
- 最后对矩阵的内容进行遍历,数奇数的个数 | ||
|
||
``` | ||
class Solution(object): | ||
def oddCells(self, n, m, indices): | ||
matrix = [[0 for i in range(m)] for j in range(n)] | ||
for i in indices: | ||
for j in range(len(matrix[i[0]])): | ||
matrix[i[0]][j] += 1 | ||
for j in range(len(matrix)): | ||
matrix[j][i[1]] += 1 | ||
count = 0 | ||
for i in range(len(matrix)): | ||
for j in range(len(matrix[0])): | ||
if matrix[i][j] % 2 == 1: | ||
count += 1 | ||
return count | ||
``` | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# 访问所有点的最小时间 | ||
|
||
## 题目 | ||
|
||
https://leetcode-cn.com/problems/minimum-time-visiting-all-points/ | ||
|
||
## 描述 | ||
|
||
平面上有 n 个点,点的位置用整数坐标表示 points[i] = [xi, yi]。请你计算访问所有这些点需要的最小时间(以秒为单位)。 | ||
|
||
你可以按照下面的规则在平面上移动: | ||
|
||
每一秒沿水平或者竖直方向移动一个单位长度,或者跨过对角线(可以看作在一秒内向水平和竖直方向各移动一个单位长度)。 | ||
必须按照数组中出现的顺序来访问这些点。 | ||
|
||
|
||
示例 1: | ||
|
||
 | ||
|
||
``` | ||
输入:points = [[1,1],[3,4],[-1,0]] | ||
输出:7 | ||
解释:一条最佳的访问路径是: [1,1] -> [2,2] -> [3,3] -> [3,4] -> [2,3] -> [1,2] -> [0,1] -> [-1,0] | ||
从 [1,1] 到 [3,4] 需要 3 秒 | ||
从 [3,4] 到 [-1,0] 需要 4 秒 | ||
一共需要 7 秒 | ||
示例 2: | ||
输入:points = [[3,2],[-2,2]] | ||
输出:5 | ||
``` | ||
|
||
## 代码 | ||
|
||
``` | ||
class Solution(object): | ||
def minTimeToVisitAllPoints(self, points): | ||
time = 0 | ||
for i in range(len(points) - 1): | ||
time1 = abs(points[i][0] - points[i+1][0]) | ||
time2 = abs(points[i][1] - points[i+1][1]) | ||
if time1 > time2: | ||
time += time1 | ||
else: | ||
time += time2 | ||
return time | ||
``` | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# 统计位数为偶数的数组 | ||
|
||
## 题目 | ||
|
||
https://leetcode-cn.com/problems/find-numbers-with-even-number-of-digits/ | ||
|
||
## 描述 | ||
|
||
给你一个整数数组 nums,请你返回其中位数为 偶数 的数字的个数。 | ||
|
||
``` | ||
示例 1: | ||
输入:nums = [12,345,2,6,7896] | ||
输出:2 | ||
解释: | ||
12 是 2 位数字(位数为偶数) | ||
345 是 3 位数字(位数为奇数) | ||
2 是 1 位数字(位数为奇数) | ||
6 是 1 位数字 位数为奇数) | ||
7896 是 4 位数字(位数为偶数) | ||
因此只有 12 和 7896 是位数为偶数的数字 | ||
示例 2: | ||
输入:nums = [555,901,482,1771] | ||
输出:1 | ||
解释: | ||
只有 1771 是位数为偶数的数字。 | ||
``` | ||
|
||
## 代码 | ||
|
||
用到了转换成字符串求长度的方法 | ||
|
||
``` | ||
class Solution(object): | ||
def findNumbers(self, nums): | ||
ret = 0 | ||
for i in nums: | ||
if len(str(i)) % 2 ==0: | ||
ret += 1 | ||
return ret | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# 将每个元素替换为右侧最大的元素 | ||
|
||
## 来源 | ||
|
||
https://leetcode-cn.com/problems/replace-elements-with-greatest-element-on-right-side/ | ||
|
||
## 描述 | ||
|
||
给你一个数组 arr ,请你将每个元素用它右边最大的元素替换,如果是最后一个元素,用 -1 替换。 | ||
|
||
完成所有替换操作后,请你返回这个数组。 | ||
|
||
``` | ||
示例: | ||
输入:arr = [17,18,5,4,6,1] | ||
输出:[18,6,6,6,1,-1] | ||
``` | ||
|
||
## 代码 | ||
|
||
我们就是从右到左遍历数组,同时使用 | ||
|
||
```python | ||
class Solution(object): | ||
def replaceElements(self, arr): | ||
max = -1 | ||
arrLen = len(arr) | ||
ret = [0 for i in range(arrLen)] | ||
ret[-1] = -1 | ||
for i in range(arrLen - 1, 0, -1): | ||
if arr[i] > max: | ||
max = arr[i] | ||
ret[i-1] = max | ||
return ret | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# 解压编码列表 | ||
|
||
## 来源 | ||
|
||
https://leetcode-cn.com/problems/decompress-run-length-encoded-list/ | ||
|
||
## 描述 | ||
|
||
给你一个以行程长度编码压缩的整数列表 nums 。 | ||
|
||
考虑每对相邻的两个元素 [freq, val] = [nums[2*i], nums[2*i+1]] (其中 i >= 0 ),每一对都表示解压后子列表中有 freq 个值为 val 的元素,你需要从左到右连接所有子列表以生成解压后的列表。 | ||
|
||
请你返回解压后的列表。 | ||
|
||
``` | ||
示例: | ||
输入:nums = [1,2,3,4] | ||
输出:[2,4,4,4] | ||
解释:第一对 [1,2] 代表着 2 的出现频次为 1,所以生成数组 [2]。 | ||
第二对 [3,4] 代表着 4 的出现频次为 3,所以生成数组 [4,4,4]。 | ||
最后将它们串联到一起 [2] + [4,4,4] = [2,4,4,4]。 | ||
示例 2: | ||
输入:nums = [1,1,2,3] | ||
输出:[1,3,3] | ||
``` | ||
|
||
## 代码 | ||
|
||
```python | ||
class Solution(object): | ||
def decompressRLElist(self, nums): | ||
ret = [] | ||
for i in range(1, len(nums), 2): | ||
for j in range(nums[i-1]): | ||
ret.append(nums[i]) | ||
return ret | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# 统计有序矩阵中的负数 | ||
|
||
## 来源 | ||
|
||
## 描述 | ||
|
||
给你一个 `m * n` 的矩阵 `grid`,矩阵中的元素无论是按行还是按列,都以非递增顺序排列。 | ||
|
||
请你统计并返回 `grid` 中 **负数** 的数目 | ||
|
||
``` | ||
示例 1: | ||
输入:grid = [[4,3,2,-1],[3,2,1,-1],[1,1,-1,-2],[-1,-1,-2,-3]] | ||
输出:8 | ||
解释:矩阵中共有 8 个负数。 | ||
示例 2: | ||
输入:grid = [[3,2],[1,0]] | ||
输出:0 | ||
示例 3: | ||
输入:grid = [[1,-1],[-1,-1]] | ||
输出:3 | ||
示例 4: | ||
输入:grid = [[-1]] | ||
输出:1 | ||
``` | ||
|
||
|
||
|
||
## 代码 | ||
|
||
最简单的就是暴力破解,但是这个因为有序,所以当我们找到是负数的时候,直接后面可以不判断了 | ||
|
||
``` | ||
class Solution(object): | ||
def countNegatives(self, grid): | ||
count = 0 | ||
for i in range(len(grid)): | ||
for j in range(len(grid[0])): | ||
if grid[i][j] < 0: | ||
count += len(grid[0]) - j | ||
break | ||
return count | ||
``` | ||
|
||
当然因为是不递增的,我们还可以想到使用二分查找法,时间复杂度是 O(logn) | ||
|
||
``` | ||
class Solution(object): | ||
def getIndex(self, line): | ||
lineLen = len(line) | ||
left = 0 | ||
right = lineLen - 1 | ||
while left <= right: | ||
mid = (left + right) // 2 | ||
if line[mid] < 0 and ((mid != 0 and line[mid -1] >= 0) or (mid == 0)): | ||
return lineLen - mid | ||
elif line[mid] < 0: | ||
right = mid -1 | ||
else: | ||
left = mid + 1 | ||
return 0 | ||
def countNegatives(self, grid): | ||
count = 0 | ||
for i in range(len(grid)): | ||
count += self.getIndex(grid[i]) | ||
return count | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# 有多少小于当前数字的数字 | ||
|
||
## 来源 | ||
|
||
https://leetcode-cn.com/problems/how-many-numbers-are-smaller-than-the-current-number/ | ||
|
||
## 描述 | ||
|
||
给你一个数组 nums,对于其中每个元素 nums[i],请你统计数组中比它小的所有数字的数目。 | ||
|
||
换而言之,对于每个 nums[i] 你必须计算出有效的 j 的数量,其中 j 满足 j != i 且 nums[j] < nums[i] 。 | ||
|
||
以数组形式返回答案。 | ||
|
||
```java | ||
示例 1: | ||
输入:nums = [8,1,2,2,3] | ||
输出:[4,0,1,1,3] | ||
解释: | ||
对于 nums[0]=8 存在四个比它小的数字:(1,2,2 和 3)。 | ||
对于 nums[1]=1 不存在比它小的数字。 | ||
对于 nums[2]=2 存在一个比它小的数字:(1)。 | ||
对于 nums[3]=2 存在一个比它小的数字:(1)。 | ||
对于 nums[4]=3 存在三个比它小的数字:(1,2 和 2) | ||
|
||
示例 2: | ||
输入:nums = [6,5,4,8] | ||
输出:[2,1,0,3] | ||
|
||
示例 3: | ||
输入:nums = [7,7,7,7] | ||
输出:[0,0,0,0] | ||
``` | ||
|
||
## 代码 | ||
|
||
### 方法1 | ||
|
||
暴力解法,两层for循环完事 | ||
|
||
``` | ||
class Solution(object): | ||
def smallerNumbersThanCurrent(self, nums): | ||
ret = [] | ||
for i in range(len(nums)): | ||
sum = 0 | ||
for j in range(len(nums)): | ||
if i == j: | ||
continue | ||
if nums[i] > nums[j]: | ||
sum += 1 | ||
ret.append(sum) | ||
return ret | ||
``` | ||
|
||
### 方法2 | ||
|
||
使用排序算法,先进行排序,完成后在统计小于的即可,排序后时间复杂度就可以由原来的 O(n^2) 变成 O(n logN)。 | ||
|
||
```java | ||
class Solution(object): | ||
def smallerNumbersThanCurrent(self, nums): | ||
ret = [] | ||
nums2 = sorted(nums) | ||
for i in nums: | ||
ret.append(nums2.index(i)) | ||
return ret | ||
``` | ||
|
Oops, something went wrong.