📂
LeetCode Note
  • Introduction
  • Tools
    • Clean Code
    • 英文小辭典
    • JS Reference
    • 常見 Edge Case
    • Array Method
    • Object Method
    • Function
    • Hashing
    • Prototype
    • 處理 Array 小撇步
    • String Method
    • Math / Date
    • loop
    • JSON.xx / localStorage
    • Date
    • Regex
    • Memorization
    • reduce condition
    • 命名
  • 筆記 Note
    • Promise
    • Walking the DOM
    • Element size and scrolling
    • CSS
  • Leetcode todo
    • ToDo
  • Array
    • # Select random poker without duplicates
    • # 最少替換達成不連續字串
    • # 724 Find Pivot Index
    • # 747. Largest Number At Least Twice of Others
    • # 01 getMaxProfit
    • # maxOfBiggestVal
    • # findSecondLargest
    • # 41 First Missing Positive
    • # 134 Gas Station (有圖)
    • # 202 Happy Number
    • # 344 Reverse String
    • # 412 Fizz Buzz
    • # 561 Array Partition I
    • # 804 Unique Morse Code Words
    • # 905 Sort Array By Parity
    • # 121. Best Time to Buy and Sell Stock.js
    • # 122 Best Time to Buy and Sell Stock II
    • # 189 Rotate Array
    • # 229 Majority Element II
    • # 268 Missing Number.
    • # 299 Bulls and Cows (有圖)
    • # 896 Monotonic Array
    • # 1002 Find Common Characters
    • # 1051 Height Checker
    • # 1185 Day of the Week
    • # 169 Majority Element
    • # 605. Can Place Flowers
    • # 350 Intersection of Two Arrays II (有圖)
    • # 482. License Key Formatting
  • Set / Map
    • # GetLengthOfLongestSubstring
    • #1 Two Sum
    • # 217 Contains Duplicate
    • # 1122 Relative Sort Array
    • # 1160 Find Words That Can Be Formed by Characters
    • #811 Subdomain Visit Count
    • # 349 Intersection of Two Arrays
    • # 819 Most Common Word
  • Two Pointer
    • #704. Binary Search
    • #26 Remove Duplicates from Sorted Array (有圖)
    • #27 Remove Element
    • # 66 Plus One
    • # 80 Remove Duplicates from Sorted Array II (有圖)
    • # 88 Merge Sorted Array (有圖)
    • # 125 Valid Palindrome
    • #167 Two Sum II - Input array is sorted (有圖)
    • # 283 Move Zeroes (有圖)
    • # 38 Count and Say
    • # 557. Reverse Words in a String III
    • #977 Squares of a Sorted Array
    • #209 Minimum Size Subarray Sum
  • String
    • # 13 Roman to Integer (有圖)
    • # 771 Jewels and Stones
    • # 937 Reorder Data in Log Files
    • # 929 Unique Email Addresses
    • # 1108 Defanging an IP Address
    • #14 Longest Common Prefix
    • # 387 First Unique Character in a String (有圖)
    • #193 Valid Phone Numbers
    • # 28 Implement strStr()
    • #383 Ransom Note
  • Stack
    • # 20 Valid Parentheses (有圖)
    • # 155 Min Stack
    • BF 165. remove characters
    • #1047 Remove All Adjacent Duplicates In String
  • Binary Search
    • # 1064 Fixed Point (有圖)
    • # 852 Peak Index in a Mountain Array
  • Recursion 遞迴
    • #2625. Flatten Deeply Nested Array
  • Math
    • # 7 Reverse Integer
    • # 9 Palindrome Number (有圖)
    • #53 Maximum Subarray (有圖)
    • # 1085 Sum of Digits in the Minimum Number.
    • # 136 Single Number
    • # 204 Count Primes (有圖)
    • #243 Shortest Word Distance
  • Dynamic Programing
    • # 322 Coin Change
    • # 509 Fibonacci Number (有圖)
    • # 70 Climbing Stairs
    • # 198 House Robber
    • # 168. Excel Sheet Column Title
  • Others
    • # 205. Isomorphic Strings
    • Implement js Array method
    • Flatten Array/Object
  • Matrix
    • 867. Transpose Matrix
  • Queue
    • DOM tree with queue
  • 排序
    • Different Sort
Powered by GitBook
On this page
  • sumOfDigit
  • Math
  • Bitwise operators 位元運算子
  • Date

Was this helpful?

  1. Tools

Math / Date

sumOfDigit

18 : 1+8 = 9 20: 2+0 = 2

方法
範例

笨方法

聰明

笨方法:

  • 先轉字串 "18"'

  • 轉 Array: ["1", "8"]

  • 變 Number [1, 8]

  • 再相加 9

let sumOfDigit = 
nums[i].toString()
.split('')
.map(num => Number(num))
.reduce((cur,acc) => cur + acc, 0)

較好方法

function getSumofDigit (value){
        let sum = 0
        while (value) {
            sum += value % 10;
            value = Math.floor(value / 10);
        }
        return sum
    }

Math

  • + , 字串變數字 ''

  • Math.pow() 平方,ex. Math.pow(5, 2) = 25

  • Math.abs(x) 絕對值, ex Math.abs(-3) = 3

  • Math.sqrt(x) 平方根 ex. Math.sqrt(9) = 3

  • Number.MIN_SAFE_INTEGER

  • Number.MAN_SAFE_INTEGER

  • Math.random(), ex. 1 - 5 , 1 + Math.floor(Math.random()*5)

Math.floor(Math.random()*4)// 0 - 3
Math.floor(Math.random()*5)// 0 - 4
  • toString(2) 十進位轉二進位

let number = 9;
number.toString(2);	// "1001"

Bitwise operators 位元運算子

看起來跟邏輯運算子很像 (&&、 ||)? 完全不一樣啊!!他是在處理二進位的事,有些題目真的會大大減少撰寫行數.倒是平常寫前端還真的從來沒用到過.

// 十轉二
(96).toString(2)
 
// 二轉十
parseInt('1100', 2)
  • & (AND)

    bit1 & bit2 : 當兩個位元都是1時,回傳1。不是的話就回傳 0

.    9 (base 10) = 1001 (base 2)
    14 (base 10) = 1110 (base 2)
                   --------------------------------
14 & 9 (base 10) = 1000 (base 2) = 8 (base 10)
  • | (OR)

bit1 | bit2 : 其中一個位元是1時,回傳1。

.    9 (base 10) = 1001 (base 2)
    14 (base 10) = 1110 (base 2)
                   --------------------------------
14 | 9 (base 10) = 1111 (base 2) = 15 (base 10)
  • ^ (XOR)

     9 (base 10) = 1001 (base 2)
    14 (base 10) = 1110 (base 2)
                --------------------------------
14 ^ 9 (base 10) = 0111 (base 2) = 7 (base 10)
  • 交换律:a ^ b ^ c <=> a ^ c ^ b

  • 任何数于0异或为任何数 0 ^ n => n

  • 相同的数可以等於 0: n ^ n => 0

所以  [4,1,2,1,2]
    4 ^ 1 ^ 2 ^ 1 ^ 2
==  1 ^ 1 ^ 2 ^ 2 ^ 4
==  0 ^ 0 ^ 4
==  4

Date

  • new Date() // Mon Oct 14 2019 15:40:54 GMT-0700 (太平洋夏令時間) {}

  • new Date('1990-10-27') // Fri Oct 26 1990 17:00:00 GMT-0700 (太平洋夏令時間) {}

Method

getDate()

14 幾號

getDay()

1 星期幾

getFullYear()

2019 幾年

getHours()

15 幾點

getMilliseconds()

977

getMinutes()

40 幾分

getMonth()

9 幾月

getSeconds()

40 幾秒

getTime()

Returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC (negative for prior times).

1571093188494

PreviousString MethodNextloop

Last updated 9 months ago

Was this helpful?

bit1 ^ bit2 : 當兩個位元一樣時回傳0,不一樣時回傳1。實務運用可以參考

let sumOfDigit = 
nums[i].toString()
.split('')
.map(num => Number(num))
.reduce((cur,acc) => cur + acc, 0)
let sum = 0
while (value) {
  sum += value % 10;
  value = Math.floor(value / 10);
  }
# 136