ARTS Week 8

Algorithm

本周的 LeetCode 题目为 283. 移动零

题目简介:给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。要求:必须在原数组上操作,不能拷贝额外的数组,同时尽量减少操作次数。

示例:

输入: [0,1,0,3,12]
输出: [1,3,12,0,0]

题目思路:数组最后的结果会是两部分,第一部分是非零数(按照原本位置顺序),第二部分全是零。因此可以设置两个指针,左指针和右指针,左指针左边都是非0数,左指针到右指针之间都是0。因此每次交换,都是将左指针的零与右指针的非零数交换。

最终代码

class Solution {
    public void moveZeroes(int[] nums) {
        int left = 0;
        int right = 0;
        while (right < nums.length) {
            if (nums[right] != 0) {
                int tmp = nums[right];
                nums[right] = nums[left];
                nums[left] = tmp;
                left++;
            }
            right++;
        }
        return ;
    }
}

Review

本周 Review 的英文文章为:追求高质量的休闲。 现如今,大家无论选择什么做什么都希望自己能有所收获,因此即使是休息时间,也会安排得满满的,但这并不是高质量休息。作者在这篇文章中,分享了一些如何进行高质量休息的建议。

基本选择

  • 心烦意乱?无法集中注意力?——出去走走
  • 累了?打个盹,如果是晚上,就早点儿睡觉
  • 感到孤独或悲伤?给朋友打个电话
  • 不知该干什么,拿笔写下来你近一段时间要做的任务或对某事的感受

运动。你可选择简单的散步、骑自行车,尝试不同种类的运动,瑜伽、游泳等

学习。在专业领域内提高自己,阅读自己感兴趣的书籍,学习一个新的技能(设计、编程、或吉他等)

饮食。学习烹饪、提前做餐前准备,制作泡菜、烘烤饼干等。如果允许,喊上你的朋友

消耗。所有的一切都需要我们消耗自己的精神来娱乐,如果只认为花了钱就可以娱乐得很好,这个观点是大错就错。简单的说,就是玩的时候要好好玩。

找到另一个方法来填补真空。去寻找最适合你的方法吧!

Tip

C++ 找不到 <bits/stdc++.h> 头文件的解决方法。该头文件是万能头文件,但它并不是标准库中的一部分,如果找不到那就需要手动添加。下面是解决步骤:

  1. 找到 <iostream> 头文件所在的目录
  2. bits 子目录或当前目录下,创建一个名为 stdc++.h 的文件
  3. 将下面内容拷贝到 stdc++.h 文件中
// C++ includes used for precompiling -*- C++ -*-
// Copyright (C) 2003-2014 Free Software Foundation, Inc. This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <http://www.gnu.org/licenses/>.
/** @file stdc++.h *  This is an implementation file for a precompiled header. */
// 17.4.1.2 Headers
// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

Share

最近发现自己总是不能休息得太好,因此觉得自己没有做到太好的放松,正好看到了那篇英文文章,于是放到了 Review 部分中,也是提醒自己需要学会更好的休息!

posted @ 2021-10-17 18:27  永远是萌新的阿岩  阅读(37)  评论(0编辑  收藏  举报