Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You must write an algorithm with O(log n) runtime complexity.
Example:
Input: nums = [1,3,5,6], target = 5
Output: 2
Input: nums = [1,3,5,6], target = 2
Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in nums.
Consider the number of unique elements of nums to be k, to get accepted, you need to do the following things:
Change the array nums such that the first k elements of nums contain the unique elements in the order they were present in nums initially. The remaining elements of nums are not important as well as the size of nums.
Return k.
Example 1:
Input: nums = [1,1,2]
Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.
Example :
Input: nums = [1,2,3,1]
Output: true
Input: nums = [1,2,3,4]
Output: false
Request :
Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.
You must implement a solution with a linear runtime complexity and use only constant extra space.
Example :
Input: nums = [2,2,1]
Output: 1
Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements.
Note that you must do this in-place without making a copy of the array.
給定一個整數數組 nums,將所有 0 移至其末尾,同時保持非零元素的相對順序。
請注意,您必須就地執行此操作,而不複製陣列。
解答:
第一段for...of,如果數字不等於0,就依照順序把數字放入,所以先建立一個變數儲存index值,從0開始,有放入數字就將index + 1
An ugly number is a positive integer whose prime factors are limited to 2, 3, and 5.
Given an integer n, return true if n is an ugly number.
題目要求ugly number 會是一個正整數
沒有頭緒,只想到要判斷一個數字是否可以被2 、 3 或 5整除,查solutions提到可以用遞迴的方式,
var isUgly = function(n) {
if (n<=0) return false
Given an integer n, return true if it is a power of two. Otherwise, return false.
An integer n is a power of two, if there exists an integer x such that n == 2x.
第一次寫:
var isPowerOfTwo = function(n) {
for (let i = 0; i<=31 ; i++ ) {
console.log(i)
if ( n === Math.pow(2,i) ) {
<font color="#3733FF">Objects And The Dot</font>
物件本身會有一個記憶體位址,它可以參考到其他屬性、物件或方法的所在位址
了解JavaScript如何找出物件 屬性 和 方法 的記憶體位置。
let person = new Object()
person["firstname"] = " Tony" //創造屬性firstname
let firstNameProperty = "firstname"
console.log(person[firstNameProperty]) //Tony
函數就是物件,有自己的屬性和方法,所有的函數都可以使用Call()、Apply() and Bind()
這三個方法都可以控制this變數要指向誰
:::warning
bind 創造函數的拷貝,讓我們設定this關鍵字
apply 和call 呼叫函數,然後設定this,接著傳入其他參數
:::
digraph graphname{
T [label="Function(a special type of object)"]