題目:
Given an integer array nums, return true if there exists a triple of indices (i, j, k) such that i < j < k and nums[i] < nums[j] < nums[k]. If no such indices exists, return false.
解題思考
沒邏輯的我先嘗試寫出一個連續三位數字遞增的版本只要比對 <u>當前位置+2</u> 是否符合即可
如果要修改成不連續的版本我該怎麼做?
將宣告兩個變數儲存最小值&次小值,並宣告為 Infinity
迭代 numsif < 最小值:替換掉num1
BirdBird changed a year agoView mode Like Bookmark
題目:
Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].
The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
You must write an algorithm that runs in O(n) time and without using the division operation.
解題思考
第一次嘗試
要有一個存放新數字的陣列
BirdBird changed a year agoView mode Like Bookmark
題目:
Given an input string s, reverse the order of the words.
A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space.
Return a string of the words in reverse order concatenated by a single space.
Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.
解題思考
BirdBird changed a year agoView mode Like Bookmark
題目:
Given a string s, reverse only all the vowels in the string and return it.
The vowels are 'a', 'e', 'i', 'o', and 'u', and they can appear in both lower and upper cases, more than once.
解題思考
遍歷 s 儲存原字串所有字母(aeiou)放入 arr
遍歷 s 當遇到字母時 放入 arr 的最後一個元素(pop())
程式碼
BirdBird changed a year agoView mode Like Bookmark
題目:
You have a long flowerbed in which some of the plots are planted, and some are not. However, flowers cannot be planted in adjacent plots.
Given an integer array flowerbed containing 0's and 1's, where 0 means empty and 1 means not empty, and an integer n, return true if n new flowers can be planted in the flowerbed without violating the no-adjacent-flowers rule and false otherwise.
解題思考
宣告一個計數器 count
遍歷陣列,檢查當前 flowerbed[i] === 0(=== 1跳過)
檢查當前 flowerbed[i] 的前後是否都等於 0flowerbed[i - 1] === 0 && flowerbed[i + 1] === 0
BirdBird changed a year agoView mode Like Bookmark
題目:
There are n kids with candies. You are given an integer array candies, where each candies[i] represents the number of candies the ith kid has, and an integer extraCandies, denoting the number of extra candies that you have.
Return a boolean array result of length n, where result[i] is true if, after giving the ith kid all the extraCandies, they will have the greatest number of candies among all the kids, or false otherwise.
Note that multiple kids can have the greatest number of candies.
解題思考
先找出原本candies最大的數字
迭代candies加上extraCandies是否為最大值
BirdBird changed a year agoView mode Like Bookmark
題目:1768. Merge Strings Alternately
You are given two strings word1 and word2. Merge the strings by adding letters in alternating order, starting with word1. If a string is longer than the other, append the additional letters onto the end of the merged string.
Return the merged string.
解題思考
需要合併兩個字串:宣告一個空變數或空陣列
需要判斷 word1 word2 的最大值:Math.max
依序從 i = 0將 word1 word2 的字元放入空變數中
直到 i = word1.length
BirdBird changed a year agoView mode Like Bookmark
題目:
For two strings s and t, we say "t divides s" if and only if s = t + t + t + ... + t + t (i.e., t is concatenated with itself one or more times).
Given two strings str1 and str2, return the largest string x such that x divides both str1 and str2.
解題思考
兩字串連結若不相等,直接返回空字串
取字串的最大公約數(GCD)
依照最大公約數來截取字串
BirdBird changed a year agoView mode Like Bookmark