發表文章

LeetCode 224. Basic Calculator(Hard)#Stack

LeetCode 224 題的問題是實現一個基本計算器,可以處理加法、減法、括號和空格。 範例 1: 輸入: s = "1 + 1" 輸出: 2 範例 2: 輸入: s = " 2-1 + 2 " 輸出: 3 範例 3: 輸入: s = "(1+(4+5+2)-3)+(6+8)" 輸出: 23 這個問題可以通過使用棧(Stack)來處理。 棧是一種先進後出的數據結構,對於處理嵌套結構(比如括號)很有用。 可將遍歷輸入字元串並使用兩個棧:一個棧用於存儲操作數,另一個棧用於存儲操作符。 Step1.初始化變數和Stack。 Step2.遍歷字元串中的每個字元: 如果是數字字元,構建當前操作數。 如果是 '+' 或 '-',更新當前操作符。 如果是 '(',將當前結果和操作符分別入棧,並重置結果和操作符。 如果是 ')',計算括號內的結果,並從棧中取出之前存儲的結果和操作符。 Step3.計算最後一個操作數。 Step4.返回最終結果。 ans1. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 class Solution { public: int calculate(string s) { stack< int > nums; // 存儲操作數 stack< int > ops; // 存儲操作符 int num = 0 ; // 當前操作數 int result = 0 ; // 最終結果 int sign = 1 ; // 符號,默認為正 for ( char c : s) { if (isdigit(c)) { num = num * 10 + (c - '0' ); // 構建操作數 ...

LeetCode資料結構_LeetCode287

圖片
 LeetCode287 要求找到給定的整數內存中重複的數字 二分查找: 時間複雜度:O(n log n),其中n是索引的長度。每次二分查找都需要遍歷整個索引 空間複雜度:O(1),只需要幾個額外的變量來保存狀態,不需要額外的存儲空間。 優點:時間複雜度為 O(n log n),比線性時間複雜度要好。不需要額外的空間。 缺點:可能需要修改集群本身,破壞原始數據。 1.初始範圍設定:我們將搜索的範圍初始化為 [1, n-1],其中 n 是備份的長度。因為根據問題描述,備份中的元素範圍是 [1, n]。 2.在每一步中計算中間值mid,並統計集群中小於等於mid的元素個數(記為count)。根據抽屜原理,如果沒有重複數字,count應該小於等於mid。如果count大於mid,說明重複數字一定在左半部分,否則在右半部分。 3.根據上一步的判斷,我們不斷調整搜索範圍,縮小範圍直到找到重複數字。 如果沒有重複數字,那麼每個倉庫中最多只有一個物體(即倉庫中的一個物體)元素),而如果倉庫中有兩個物體,那麼就說明至少有一個倉庫中放了兩個元素,那麼倉庫中存在重複數字。 因為我們在搜索重複數字的時候可以,將集群分割成不同的區間來進行二分查找,而根據抽屜原理,如果有n個抽屜,放入了n+1個物體,那麼至少有一個抽屜中放了兩個物體。 1.程式碼 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 class Solution { public: int findDuplicate(vector< int >& nums) { int left = 1 ; // 最小可能的重複數字 int right = nums.size() - 1 ; // 最大可能的重複數字 while (left < right) { int mid = left + (right - left) / 2 ; // 計算中間值 int count = 0 ; // 統計數組中小於等於 mid 的元素個數 ...

React常用命令筆記

  初始專案 法1.(不汙染環境方式,每次都會暫時下載create-react-app cli用完就刪除) 新增一個專案folder後cd到該層並下 npx create-react-app . 法2.(需要全域性安裝影響環境方式) 一開始環境要記得先全域性下創建專案用的CLI npm install -g create-react-app create-react-app 專案(目錄)名稱 再cd 到專案(目錄)名稱下也可 運行專案 npm start 或 npm run start 依賴套件安裝 npm i 套件名 或 yarn add 套件名 比如 npm i react-router-dom yarn add react-router-dom Ref: Difference between npm start and npm run start https://stackoverflow.com/questions/51358235/difference-between-npm-start-and-npm-run-start What is the --save option for npm install? https://stackoverflow.com/questions/19578796/what-is-the-save-option-for-npm-install https://stackoverflow.com/questions/40868494/what-is-the-s-in-npm-i-s

React一些常用的套件(前端特效_路由切換)

圖片
  1. react-particle-animation(MIT 授權) https://www.npmjs.com/package/react-particle-animation Particle效果 套用後效果 簡單使用方式 Step1.引入組件 import ParticleAnimation from 'react-particle-animation'; Step2.使用ParticleAnimation 組件Tag設置相應屬性 function App() { return ( <div className="App"> <ParticleAnimation style={{ height: '96vh' }} background={{ r: 21, g: 22, b: 23, a: 1 }} particleSpeed={0.5} particleRadius={1.5} /> </div> ); } export default App; 2. react-typed(MIT 授權) https://www.npmjs.com/package/react-typed 文字輸入動畫效果 簡單使用方式 Step1.引入組件 import Typed from 'react-typed'; Step2.使用Typed組件設置相應屬性進行要顯示的輸入動畫文字內容跟顯示、移除速度,是否重複 function Header() { return ( <div className="main-info"> <h1>Who Am I?</h1> <Typed strings={[ "", "A software engineer", ...

React配置Bootstrap

圖片
  下指令 npm i bootstrap react-bootstrap 在App.js中 引入bootstrap css style 這裡要套用react-bootstrap 當中的 navbar 組件 import Navbar from 'react-bootstrap/Navbar' 當然還有常用到的 nav組件 import Nav from 'react-bootstrap/Nav' 套用效果 Ref: https://app.pluralsight.com/guides/how-to-use-react-bootstrap-with-npm https://react-bootstrap.github.io/components/alerts

vscode中好用的react套件_加速開發

圖片
  ES7 React/Redux/GraphQL/React-Native snippets https://marketplace.visualstudio.com/items?itemName=dsznajder.es7-react-js-snippets 經常使用的快捷 rfce Basic Methods Prefix Method imp→ import moduleName from 'module' imn→ import 'module' imd→ import { destructuredModule } from 'module' ime→ import * as alias from 'module' ima→ import { originalName as aliasName} from 'module' exp→ export default moduleName exd→ export { destructuredModule } from 'module' exa→ export { originalName as aliasName} from 'module' enf→ export const functionName = (params) => { } edf→ export default (params) => { } met→ methodName = (params) => { } fre→ arrayName.forEach(element => { } fof→ for(let itemName of objectName { } fin→ for(let itemName in objectName { } anfn→ (params) => { } nfn→ const functionName = (params) => { } dob→ const {propName} = objectToDescruct dar→ const [propName] = arrayToDescruct sti→ setInterval(() => { }, interv...