ARTS Week 21(ARTS Week 21)

Algorithm

本周的 LeetCode 题目为 33. 搜索旋转排序数组

整数数组 按升序排列,数组中的值 互不相同

nums

在传递给函数之前, 在预先未知的某个下标 ()上进行了 旋转,使数组变为 (下标从0 开始计数)。例如, 在下标 3 处经旋转后可能变为 。

nums
k
0 <= k < nums.length
[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]
[0,1,2,4,5,6,7]
[4,5,6,7,0,1,2]

给你 旋转后 的数组 和一个整数 ,如果 中存在这个目标值 ,则返回它的下标,否则返回 。

nums
target
nums
target
-1
输入:nums = [4,5,6,7,0,1,2], target = 0
输出:4
class Solution {
    public int search(int[] nums, int target) {
        if (nums.length == 1) {
            if (nums[0] == target) {
                return 0;
            } else {
                return -1;
            }
        }

        int left = 0;
        int right = nums.length - 1;
        int ans = -1;
        while (left < right) {
            if (nums[left] == target) {
                ans = left;
                break;
            }
            if (nums[right] == target) {
                ans = right;
                break;
            }

            int mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                ans = mid;
                break;
            }

            if (nums[mid] >= nums[0]) { // nums[0], NOT nums[mid]
                if (target >= nums[0] && target < nums[mid]) {
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            } else {
                if (target > nums[mid] && target < nums[right]) {
                    left = mid + 1;
                } else {
                    right = mid - 1;
                }
            }
        }
        return ans;
    }
}

Review

本周 Review 的英文文章为:我希望我早点儿知道的一些事情

作者在文中介绍了他自己常用的一些软件、工具等。下面是具体介绍:

  • Anki:Anki 是一个免费的开源抽认卡程序,你可以利用它来帮你记住任何东西。
  • Dextroamphetamine:是一种可以增强认知的兴奋剂
  • R:R语言可以有效地组织和可视化数据
  • sci-hub 和 Kindle、Z-Library、Calibre:sci-hub 可以免费获取到很多科学论文;Kindle 是阅读工具,你可以在 Z-Library 上找到很多想要的书,再通过 Calibre 来将这些书导入到 Kindle 中
  • 保持良好的开放判断:你可以通过购买、阅读大量书籍,来使得你更加理性并减少偏见认知的影响。

Tip

为什么 在调用后不会及时输出?因为在默认情况下,输出流 是有缓冲的,如果你想要立即输出,那么则需要刷新流(使用 )或在 中打印换行符:

printf
stdout
fflush
printf
// Method 1:
printf("Starting nets allocation...");
fflush(stdout);

// Method 2:
printf("Starting nets allocation...\n");

Share

看了上面的 Review 中的介绍,打算去试一试 这个软件。

Anki
————————

Algorithm

This week’s leetcode topic is 33 Search rotation sort array

Integer arrays are arranged in ascending order. The values in the array are < strong > different from each other < / strong >.

nums

Before passing to the function, the < strong > rotation < / strong > is performed on a previously unknown subscript (), so that the array becomes (subscript counts from 0). For example, after rotation at subscript 3, it may become.

nums
k
0 <= k < nums.length
[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]
[0,1,2,4,5,6,7]
[4,5,6,7,0,1,2]

Give you the array and an integer of < / strong > after < strong > rotation. If the target value exists in, return its subscript, otherwise return it.

nums
target
nums
target
-1
输入:nums = [4,5,6,7,0,1,2], target = 0
输出:4
class Solution {
    public int search(int[] nums, int target) {
        if (nums.length == 1) {
            if (nums[0] == target) {
                return 0;
            } else {
                return -1;
            }
        }

        int left = 0;
        int right = nums.length - 1;
        int ans = -1;
        while (left < right) {
            if (nums[left] == target) {
                ans = left;
                break;
            }
            if (nums[right] == target) {
                ans = right;
                break;
            }

            int mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                ans = mid;
                break;
            }

            if (nums[mid] >= nums[0]) { // nums[0], NOT nums[mid]
                if (target >= nums[0] && target < nums[mid]) {
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            } else {
                if (target > nums[mid] && target < nums[right]) {
                    left = mid + 1;
                } else {
                    right = mid - 1;
                }
            }
        }
        return ans;
    }
}

Review

The English article in this week’s review is: I hope I know something earlier

In this paper, the author introduces some commonly used software and tools. The following is a detailed introduction:

  • Anki: anki is a free open source flashcard program that you can use to help you remember anything.
  • Dextroamphetamine:是一种可以增强认知的兴奋剂
  • R: R language can effectively organize and visualize data
  • sci-hub 和 Kindle、Z-Library、Calibre:sci-hub 可以免费获取到很多科学论文;Kindle 是阅读工具,你可以在 Z-Library 上找到很多想要的书,再通过 Calibre 来将这些书导入到 Kindle 中
  • Maintain good open judgment: you can make you more rational and reduce the impact of biased cognition by buying and reading a large number of books.

Tip

Why is it not output in time after the call? Because the output stream is buffered by default, if you want to output immediately, you need to brush a new stream (use) or print line breaks in:

printf
stdout
fflush
printf
// Method 1:
printf("Starting nets allocation...");
fflush(stdout);

// Method 2:
printf("Starting nets allocation...\n");

Share

After reading the introduction in the review above, I intend to try this software.

Anki