Skip to content

Commit

Permalink
update algo_na
Browse files Browse the repository at this point in the history
  • Loading branch information
no5ix committed Aug 17, 2024
1 parent b890c5f commit 08cf27d
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 2 deletions.
11 changes: 9 additions & 2 deletions source/_posts/algo_na.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ tags:
- LeetCode
categories:
- Algo
password: '0622'
---


Expand All @@ -16,6 +15,14 @@ password: '0622'
推荐参考**本博客总结**的 {% post_link algo_newbie %}


# 本文完整参考代码

<https://github.com/no5ix/no5ix.github.io/blob/source/source/code/test_algo_na.java>


**. . .**<!-- more -->


# 概绍

本群的每日刷题打卡活动, 按照 GitHub 49k star的项目 https://github.com/youngyangyang04/leetcode-master 的刷题顺序.
Expand Down Expand Up @@ -172,7 +179,7 @@ public class test{
}
```

# lc59 - Spiral Matrix 2 - 20240917
## lc59 - Spiral Matrix 2 - 20240917

- https://programmercarl.com/0059.螺旋矩阵II.html#算法公开课
- https://leetcode.com/problems/spiral-matrix-ii/description/
Expand Down
Binary file added source/code/Solution.class
Binary file not shown.
Binary file added source/code/test_algo_na.class
Binary file not shown.
119 changes: 119 additions & 0 deletions source/code/test_algo_na.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// class Solution { // lc704
// public int search(int[] numbers, int targetNumber) {
// if (targetNumber < numbers[0] || targetNumber > numbers[numbers.length -1]) {
// return -1;
// }
// int leftIndex = 0;
// int rightIndex = numbers.length -1;
// while (leftIndex <= rightIndex) {
// int midIndex = leftIndex + ((rightIndex - leftIndex) >> 2);
// if (numbers[midIndex] == targetNumber) {
// return midIndex;
// } else if (numbers[midIndex] < targetNumber) {
// leftIndex = midIndex + 1;
// } else {
// rightIndex = midIndex - 1;
// }
// }
// return -1;
// }
// }

// class Solution { // lc27
// public int removeElement(int[] nums, int val) {
// int newArrayIndex = 0;
// for (int searchingIndex = 0; searchingIndex < nums.length; ++searchingIndex) {
// if (nums[searchingIndex] != val) {
// nums[newArrayIndex++] = nums[searchingIndex];
// }
// }
// return newArrayIndex;
// }
// }

// class Solution { // lc977
// public int[] sortedSquares(int[] nums) {
// int[] resultArray = new int[nums.length];
// int startIndex = 0;
// int endIndex = nums.length - 1;
// int resultIndex = nums.length - 1;
// while (startIndex <= endIndex) { // 这里是 <= , 因为最后要处理两个元素
// if (nums[startIndex] * nums[startIndex] > nums[endIndex] * nums[endIndex]) {
// resultArray[resultIndex--] = nums[startIndex] * nums[startIndex];
// startIndex++;
// } else {
// resultArray[resultIndex--] = nums[endIndex] * nums[endIndex];
// endIndex--;
// }
// }
// return resultArray;
// }
// }


// class Solution { // lc209
// public int minSubArrayLen(int target, int[] nums) {
// int left = 0;
// int sum = 0;
// int subLength = 0;
// int result = Integer.MAX_VALUE;
// for (int right = 0; right < nums.length; ++right) {
// sum += nums[right];
// while (sum >= target) {
// subLength = right - left + 1;
// result = subLength > result ? result : subLength;
// sum -= nums[left++];
// }
// }
// return result == Integer.MAX_VALUE ? 0 : result;
// }
// }

class Solution { // lc59
public int[][] generateMatrix(int n) {
int[][] result = new int[n][n];
int loop = n / 2;
int startX = 0;
int startY = 0;
int num = 1;
int offset = 1;
int i, j;
while (loop-- > 0) {
i = startX;
j = startY;
for (; j < n - offset; ++j) {
result[i][j] = num++;
}
for (; i < n - offset; ++i) {
result[i][j] = num++;
}
for (; j > startX; --j) {
result[i][j] = num++;
}
for (; i > startY; --i) {
result[i][j] = num++;
}
++startX;
++startY;
++offset;
}
if (n % 2 != 0) {
result[n/2][n/2] = n * n;
}
return result;
}
}

public class test_algo_na{
public static void main(String[] args){
Solution solution = new Solution();
// int[] myList = {1, 2, 3, 5, 6};
int[][] ret = solution.generateMatrix(3);
System.out.println(ret);
for (int i = 0; i < ret.length; ++i) {
for (int j = 0; j < ret.length; ++j) {
System.out.println(ret[i][j]);
}
}
}
}

0 comments on commit 08cf27d

Please sign in to comment.