当前位置:网站首页>LeetCode 04: T26. 删除排序数组中的重复项(简单); 剑指 Offer 67. 把字符串转换成整数(中等); 面试题 01.08. 零矩阵 (简单)
LeetCode 04: T26. 删除排序数组中的重复项(简单); 剑指 Offer 67. 把字符串转换成整数(中等); 面试题 01.08. 零矩阵 (简单)
2022-07-27 10:58:00 【无知小九】
文章目录
T10: 26. 删除排序数组中的重复项(简单)
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路:
解法: 双指针
class Solution {
public int removeDuplicates(int[] nums) {
int n = nums.length;
if (n == 0) {
return 0;
}
int fast = 1, slow = 1;
//为什么不能是slow = 0
while (fast < n) {
if (nums[fast] != nums[fast - 1]) {
nums[slow] = nums[fast];
++slow;
}
++fast;
}
return slow;
}
}
执行用时:1 ms, 在所有 Java 提交中击败了**81.21%**的用户
内存消耗:40.1 MB, 在所有 Java 提交中击败了**82.57%**的用户
T11: 剑指 Offer 67. 把字符串转换成整数(中等)
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/ba-zi-fu-chuan-zhuan-huan-cheng-zheng-shu-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路:
解法: 没搞懂
class Solution {
public int strToInt(String str) {
char[] c = str.trim().toCharArray();
if(c.length == 0) return 0;
int res = 0, bndry = Integer.MAX_VALUE / 10;
int i = 1, sign = 1;
if(c[0] == '-') sign = -1;
else if(c[0] != '+') i = 0;
for(int j = i; j < c.length; j++) {
if(c[j] < '0' || c[j] > '9') break;
if(res > bndry || res == bndry && c[j] > '7') return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
res = res * 10 + (c[j] - '0');
}
return sign * res;
}
}
执行用时:2 ms, 在所有 Java 提交中击败了**99.43%**的用户
内存消耗:38.5 MB, 在所有 Java 提交中击败了**29.82%**的用户
T12: 面试题 01.08. 零矩阵 (简单)
编写一种算法,若M × N矩阵中某个元素为0,则将其所在的行与列清零。
示例 1:
输入:
[
[1,1,1],
[1,0,1],
[1,1,1]
]
输出:
[
[1,0,1],
[0,0,0],
[1,0,1]
]
示例 2:
输入:
[
[0,1,2,0],
[3,4,5,2],
[1,3,1,5]
]
输出:
[
[0,0,0,0],
[0,4,5,0],
[0,3,1,0]
]
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/zero-matrix-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
思路
解法: boolean 标记
class Solution {
public void setZeroes(int[][] matrix) {
int m = matrix.length, n = matrix[0].length;
boolean[] row = new boolean[m];
boolean[] col = new boolean[n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == 0) {
row[i] = col[j] = true;
}
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (row[i] || col[j]) {
matrix[i][j] = 0;
}
}
}
}
}
执行用时:1 ms, 在所有 Java 提交中击败了**99.84%**的用户
内存消耗:40.1 MB, 在所有 Java 提交中击败了**52.76%**的用户
时间复杂度:O(mn),其中 m 是矩阵的行数,n 是矩阵的列数。我们至多只需要遍历该矩阵两次。
空间复杂度:O(m+n),其中 m 是矩阵的行数,n 是矩阵的列数。我们需要分别记录每一行或每一列是否有零出现。
边栏推荐
- The C programming language-2nd-- notes -- 4.11.3
- (3) Pass parameters
- Analysis of distributed database and cache double write consistency scheme (Reprint)
- Caused by:org.gradle.api.internal.plugins . PluginApplicationException: Failed to apply plugin
- 数字三角形模型 AcWing 1027. 方格取数
- 数字三角形模型 AcWing 275. 传纸条
- Caused by:org.gradle.api.internal. plugins . PluginApplicationException: Failed to apply plugin
- 为什么选择智能电视?
- Stm32f10x -- C Language-1
- 局域网SDN硬核技术内幕 25 展望未来——RDMA(下)
猜你喜欢
随机推荐
349 sum of intersection of two arrays and 01
1.Flume 简介及基本使用
2022 Niuke multi school (3) j.journey
局域网SDN技术硬核内幕 12 云网CP的日常恩爱——硬件VXLAN转发平面
Find the combination number acwing 888. find the combination number IV
【Unity入门计划】CreatorKitFPS:第一人称射击3D小游戏
开源flink有写holo的Datastream connector或者flink sql conn
When std:: bind meets this
makefile模板
(5) Printf (instead of echo)
C programming language (2nd Edition) -- Reading Notes -- 1.5.3
Maker Hongmeng application development training notes 02
Moveit2 - 5. Scenario Planning
(3) Pass parameters
Basic use of cmake
数字三角形模型 AcWing 1027. 方格取数
The C programming language (2nd) -- Notes -- 1.10
Game theory acwing 891. Nim game
properties文件
第10章 枚举类与注解









