力扣-探索数组和字符串

数组简介

题目:寻找数组的中心索引

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public int pivotIndex(int[] nums) {
if(nums == null) return -1;
int sum = 0;
int sumOfLeft = 0;
int sumOfRight = 0;
for(int x : nums){
sum += x;
}
for(int i = 0;i < nums.length;++i){
if(i==0) sumOfLeft = 0;
else {
sumOfLeft = sumOfLeft + nums[i - 1];
}
sumOfRight = sum - sumOfLeft - nums[i];
if(sumOfLeft == sumOfRight) return i;
}

return -1;
}

题目:至少是其他数字两倍的最大数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public int dominantIndex(int[] nums) {
if(nums == null) return -1;
//最大元素
int max = nums[0];
//第二大元素
int min = Integer.MIN_VALUE;
//最大元素位置
int pos = 0;

for(int i = 0;i<nums.length;i++){
if(nums[i] > max){
min = max;
max = nums[i];
pos = i;
}else if(nums[i] > min && nums[i] != max){
min = nums[i];
}
}
//最大元素是否是第二大元素的两倍
if(max >= 2 * min) return pos;
return -1;

//https://blog.csdn.net/gx864102252/article/details/82596911
}

题目:加一
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public int[] plusOne(int[] digits) {
int length = digits.length;
for(int i = length - 1;i>=0;--i){
if(digits[i] < 9){
digits[i]++;
return digits;
}
digits[i] = 0;
}

int[] result = new int[length+1];
result[0] = 1;
return result;
}

//https://blog.csdn.net/whdAlive/article/details/80356481


二维数组简介

题目:对角线遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Solution {
public int[] findDiagonalOrder(int[][] matrix) {
if(matrix == null ) return null;
if(matrix.length == 0) return new int[]{};
int rowLen = matrix.length,colLen = matrix[0].length;
int size = rowLen * colLen;
int[] result = new int[size];
boolean up = true;//斜向上
int x = 0, y = 0;

for(int i = 0; i < size; ++i){
result[i] = matrix[x][y];

if(up){
x--;
y++;
if(y > colLen - 1){//斜向右上的最右上角,超过矩阵规模
y = colLen - 1;
x += 2;
up = false;
}
if(x < 0){
x = 0;
up = false;
}

}else{
x++;
y--;

if(x > rowLen - 1){//斜向左下的最左下角,超过矩阵规模
x = rowLen - 1;
y += 2;
up = true;
}
if(y < 0){
y = 0;
up = true;
}

}
}
return result;

}
}

题目:螺旋矩阵

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public List<Integer> spiralOrder(int[][] matrix) {
if(matrix == null) return null;
if(matrix.length == 0) return new ArrayList<Integer>();
int rowLen = matrix.length;
int colLen = matrix[0].length;

List<Integer> result = new ArrayList<>();
int count = 0;
int sum = (Math.min(rowLen,colLen) + 1) / 2;
while(count < sum){
for(int i = count;i < colLen - count;++i){//从左往右
result.add(matrix[count][i]);
}
for(int i = count + 1;i < rowLen - count;++i){//从上往下
result.add(matrix[i][colLen - 1 - count]);
}
for(int i = (colLen - 1) - (count + 1);i>=count && (rowLen - 1 - count) != count;--i){//从右往左
result.add(matrix[rowLen - 1 - count][i]);
}
for(int i= (rowLen - 1) - (count + 1);i >= count+1 &&(colLen - 1 - count) != count ;--i){//从下往上
result.add(matrix[i][count]);
}
count ++;
}


return result;
}
//https://leetcode-cn.com/problems/spiral-matrix/solution/luo-xuan-ju-zhen-by-liao-tian-yi-jian/

题目:杨辉三角
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public List<List<Integer>> generate(int numRows) { 
List<List<Integer> > result = new ArrayList<List<Integer> >();
if(numRows == 0) return result;

result.add(new ArrayList<>());
result.get(0).add(1);

for(int rowNum = 1;rowNum < numRows;++rowNum){
List<Integer> row = new ArrayList<>();
List<Integer> prevRow = result.get(rowNum - 1);

row.add(1);

for(int j = 1;j < rowNum;j++){
row.add(prevRow.get(j-1) + prevRow.get(j));
}

row.add(1);

result.add(row);
}
return result;
}
//https://leetcode-cn.com/problems/pascals-triangle/solution/yang-hui-san-jiao-by-leetcode/


字符串简介


双指针技巧


小结