当前位置:网站首页>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 是矩阵的列数。我们需要分别记录每一行或每一列是否有零出现。
边栏推荐
- ACM warm-up Exercise 1 in 2022 summer vacation (summary)
- Local virtual machine initialization script
- The C programming language-2nd-- notes -- 4.11.3
- C programming language (2nd Edition) -- Reading Notes -- 1.5.1
- 【着色器实现Shake随机摇动效果_Shader效果第十篇】
- Open source Flink has datastream connector written with holo or Flink SQL Conn
- 多家银行调整现金管理类理财产品申赎规则:申赎确认时效“T+0”变“T+1”
- Maker paper data search training notes
- Kepserver configuration
- Remember an experience of using canvas to make the banner streamer effect of Tencent cloud homepage
猜你喜欢

Inclusion exclusion principle acwing 890. divisible numbers

State compression DP acwing 91. shortest Hamilton path

最长上升子序列模型 AcWing 482. 合唱队形

Everything cannot be searched for startup_ Lpc11x.s file

82. (cesium home) cesium points move on 3D models

Maker harmony OS application development training notes 01

Backpack model acwing 1022. Collection of pet elves

Pat (Grade B) 2022 summer exam

Tree DP acwing 285. dance without boss

Find the combination number acwing 886. find the combination number II
随机推荐
博弈论 AcWing 893. 集合-Nim游戏
局域网SDN技术硬核内幕 12 云网CP的日常恩爱——硬件VXLAN转发平面
(3) Pass parameters
第13章 IO流
最长上升子序列模型 AcWing 1014. 登山
2022 Niuke multi school (3) j.journey
区间问题 AcWing 906. 区间分组
Redis simple to use
C programming language (2nd Edition) -- Reading Notes -- 1.5.1
数据包传输:应用层-内核-硬件
Memory search acwing 901. Skiing
数字三角形模型 AcWing 1015. 摘花生
2022 Niuke multi school training (3) a-ancestor topic translation
力扣——10. 正则表达式匹配
Find the combinatorial number acwing 889. 01 sequence satisfying the condition
Installation and use of GTEST and gmock
C programming language (2nd Edition) -- Reading Notes -- 1.5
Tensorflow tensor operation function set
局域网SDN技术硬核内幕 13 从局域网到互联网
求组合数 AcWing 889. 满足条件的01序列