当前位置:网站首页>Leetcode 04: T26. Delete duplicate items in the sorting array (simple); Sword finger offer 67. convert the string to an integer (medium); Interview question 01.08. zero matrix (simple)
Leetcode 04: T26. Delete duplicate items in the sorting array (simple); Sword finger offer 67. convert the string to an integer (medium); Interview question 01.08. zero matrix (simple)
2022-07-27 11:48:00 【Ignorant little nine】
List of articles
T10: 26. Delete duplicates in sort array ( Simple )
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas :
solution : Double pointer
class Solution {
public int removeDuplicates(int[] nums) {
int n = nums.length;
if (n == 0) {
return 0;
}
int fast = 1, slow = 1;
// Why not slow = 0
while (fast < n) {
if (nums[fast] != nums[fast - 1]) {
nums[slow] = nums[fast];
++slow;
}
++fast;
}
return slow;
}
}
Execution time :1 ms, In all Java Defeated in submission **81.21%** Users of
Memory consumption :40.1 MB, In all Java Defeated in submission **82.57%** Users of
T11: The finger of the sword Offer 67. Convert a string to an integer ( secondary )
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/ba-zi-fu-chuan-zhuan-huan-cheng-zheng-shu-lcof
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas :
solution : I don't understand
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;
}
}
Execution time :2 ms, In all Java Defeated in submission **99.43%** Users of
Memory consumption :38.5 MB, In all Java Defeated in submission **29.82%** Users of
T12: Interview questions 01.08. Zero matrix ( Simple )
Write an algorithm , if M × N An element in the matrix is 0, The row and column in which it is located are cleared .
Example 1:
Input :
[
[1,1,1],
[1,0,1],
[1,1,1]
]
Output :
[
[1,0,1],
[0,0,0],
[1,0,1]
]
Example 2:
Input :
[
[0,1,2,0],
[3,4,5,2],
[1,3,1,5]
]
Output :
[
[0,0,0,0],
[0,4,5,0],
[0,3,1,0]
]
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/zero-matrix-lcci
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Ideas
solution : boolean Mark
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;
}
}
}
}
}
Execution time :1 ms, In all Java Defeated in submission **99.84%** Users of
Memory consumption :40.1 MB, In all Java Defeated in submission **52.76%** Users of
Time complexity :O(mn), among m It's the number of rows in the matrix ,n It's the number of columns in a matrix . We only need to traverse the matrix twice at most .
Spatial complexity :O(m+n), among m It's the number of rows in the matrix ,n It's the number of columns in a matrix . We need to record whether there is zero in each row or column .
边栏推荐
猜你喜欢

TapNet: Multivariate Time Series Classification with Attentional Prototypical Network

N ¨UWA: Visual Synthesis Pre-training for Neural visUal World creAtionChenfei

【Unity入门计划】CreatorKitFPS:第一人称射击3D小游戏

Proteus8专业版破解后用数码管闪退的解决

JUC框架 从Runnable到Callable到FutureTask 使用浅析

C# 自定义集合

Finding the finite zero point of transfer function under different sampling periods
Synchronous use reference of the new version of data warehouse (for beginners)

C custom set

The C programming language (2nd) -- Notes -- 1.6
随机推荐
LNMP architecture setup (deploy discuz Forum)
LAN SDN hard core technology insider 24 outlook for the future - RDMA (middle)
多种进制之间的转换
Maker paper data search training notes
The C programming language 2nd -- Notes -- 6.7
微机和单片机的区别
Common power supply problems and solutions of Arduino
Difference quotient approximation of wechat quotient
LeetCode 01: T1. 两数之和 ; T1108. IP 地址无效化 ; T344. 反转字符串
Synchronous use reference of the new version of data warehouse (for beginners)
C programming language (2nd Edition) -- Reading Notes -- 1.5
Can you really write binary search - variant binary search
Detailed explanation of MATLAB S-function
检定和校准的区别
Principle of PWM and generation of PWM wave
Newton Raphson iterative method
Summary of leetcode SQL exercises (MySQL Implementation)
Moveit2 - 4. robot model and robot state
局域网SDN技术硬核内幕 11 云网融合CP的关键——层次化端口绑定
你真的会写二分查找吗——变种二分查找