ARTS Week 8
阿新 • • 發佈:2021-10-17
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>
標頭檔案的解決方法。該標頭檔案是萬能標頭檔案,但它並不是標準庫中的一部分,如果找不到那就需要手動新增。下面是解決步驟:
- 找到
<iostream>
標頭檔案所在的目錄 - 在
bits
子目錄或當前目錄下,建立一個名為stdc++.h
的檔案 - 將下面內容拷貝到
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 部分中,也是提醒自己需要學會更好的休息!