題目
1 | Input |
解法思維
1 | var ParkingSystem = function(big, medium, small) { |
1 | var ParkingSystem = function(big, medium, small) { |
1 | Input |
1 | var ParkingSystem = function(big, medium, small) { |
1 | var ParkingSystem = function(big, medium, small) { |
1 | <canvas id="cvs" width="800" height="600" style="border: 1px solid #000000;"></canvas> |
1 | const cvs=document.querySelector('#cvs') |
1 | // 填滿顏色 |
1 | // 描邊顏色 |
1 | // 開啟路徑 |
1 | ctx.beginPath() |
1 | let img=new Image() |
1 | // Html |
1 | // Html |
1 | let painting=false |
創建一個物件,每次畫完就擷取圖片存在物件裡,物件[步驟]讀取畫面。
1 | let restore_array=[] |
1 | Input: jewels = "aA", stones = "aAAbbbb" |
跑迴圈比對
1 | var numJewelsInStones = function(jewels, stones) { |
先解構再用filter()
篩選
1 | var numJewelsInStones = function(jewels, stones) { |
1 | Input: nums = [1,2,3,1,1,3] |
想到跑雙迴圈判斷
1 | var numIdenticalPairs = function(nums) { |
利用for of
1 | var numIdenticalPairs = function(nums) { |
1 | Input: nums = [2,5,1,3,4,7], n = 3 |
跑迴圈再依序加入
1 | var shuffle = function(nums, n) { |
先用slice()
拆分物件,再跑迴圈,依序加回去
1 | var shuffle = function(nums, n) { |
1 | arr.slice() |
1 |
|
1 | Input: candies = [2,3,5,1,3], extraCandies = 3 |
用map()
相加,在跟 Math.max()
比較,回傳結果
1 | var kidsWithCandies = (candies, extraCandies) =>candies.map(v => (v + extraCandies) >= Math.max(...candies)); |
1 | Input: accounts = [[1,2,3],[3,2,1]] |
用foreach()
取出物件,再用reduce()
計算,最後用 Math.max()
抓出最大值
1 | var maximumWealth = function(accounts) { |
1 | Input: nums = [1,2,3,4] |
用foreach()
依序相加,再回傳
1 | var runningSum = function(nums) { |
用reduce()依序相加,再回傳
1 | /** |
1 | Input: nums = [2,7,11,15], target = 9 |
剛開始是想跑兩個迴圈,跑完再判斷相加是否等於目標值
1 | var twoSum = function(nums, target) { |
別人的想法:跑迴圈,在判斷目標數值與迴圈數值相減,是否有在obj={}
裡
1 | var twoSum = function(nums, target) { |