ARTS Week 8

ARTS Week 8,第1张

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++ 找不到 头文件的解决方法。该头文件是万能头文件,但它并不是标准库中的一部分,如果找不到那就需要手动添加。下面是解决步骤:

  1. 找到 头文件所在的目录
  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
// .

// 17.4.1.2 Headers
// C
#ifndef _GLIBCXX_NO_ASSERT
#include 
#endif
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#if __cplusplus >= 201103L
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#endif

// C++
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#if __cplusplus >= 201103L
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#endif
Share

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zaji/3970401.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-10-21
下一篇 2022-10-21

发表评论

登录后才能评论

评论列表(0条)

保存