1

## 30-Day LeetCode Challenge - Week1

30Day Leecodeing Callenge ,第一週挑戰 ！！

### 1.Hashmap

javascript 的 hashmap 就是應用Object的索引值存跨索引值得值

``````let arr = ['a','b','c']
let hashmap = {}
for(let i = 0; i < arr.length ; i++){
if(arr[i] = 'a') arr[arr[i]] = 1
}
// hashmap = { {'a': 1} }
``````

### 2.two pointers

4.Move Zeroes

``````let i = 0
let j = arr.length
while(i < j){
if(condition){
dosomething
}
i++
j--
}
``````

## 題目

### 1.Single Number

``````Example 1:
Input: [2,2,1]
Output: 1
``````
``````Example 2:
Input: [4,1,2,1,2]
Output: 4
``````

``````var singleNumber = function(nums) {
let temp = {}

nums.map(num => {
if(!temp[num]){
temp[num] = 1
}else{
temp[num] +=1
}
})
return Object.keys(temp).find(key => 1 === temp[key])
};
``````

### 2. Happy Number

``````Example:
Input: 19
Output: true
Explanation:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
``````

``````var isHappy = function(n) {
let notHappy = [4,,16,37,58,89,145,42,20]
let num = n
while(notHappy.indexOf(num) === -1){
if(num ===1){
break
}else{
let list =  num.toString().split("")
let sum = 0
let newNum = list.map((a)=>{
return sum += Math.pow(a,2)
})
num = sum
}
}

};
``````

### 3.Maximum Subarray

``````Example:
Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
``````

``````var maxSubArray = function(nums) {
let max = Math.max(...nums)
let num = 0

for(let i = 0 ; i < nums.length ; i++){
num += nums[i]
if(num > max) max = num
if(num < 0) num = 0
}

return max
};
``````

### 4.Move Zeroes

``````Example:
Input: [0,1,0,3,12]
Output: [1,3,12,0,0]
``````

``````var moveZeroes = function(nums) {
let i = 0
let j = nums.length - 1

while(i < j ){
if(nums[j] === 0) j--
if(nums[i] === 0) {
nums.splice(i,1)
nums.push(0)
}else{
i++
}
}

};
``````

### 5.Best Time to Buy and Sell Stock II

``````Example 1:
Input: [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
``````
``````Example 2:
Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
engaging multiple transactions at the same time. You must sell before buying again.
``````
``````Example 3:
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
``````

``````var maxProfit = function(prices) {
let i = 0
let j = prices.length
let sumProfit = 0

while(i < j-1 ){
let profit = 0
if(prices[i] < prices[i+1] ){
profit = prices[i+1]- prices[i]
}
sumProfit += profit

i++
}

return sumProfit
};
``````

### 6.Group Anagrams

``````Example:
Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
Output:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
``````

string.split('').sort()則是用來判斷是否同類

``````var groupAnagrams = function(strs) {

let hash = {
}

strs.map(s =>{
let temp = ""
let math = s.split('').sort()
if(temp !== math) temp = math
if(temp === math){
if(hash[math]){
hash[math].push(s)
}else{
hash[math] = [s]
}
}
})

return (Object.values(hash))

};
``````

### 7.Counting Elements

``````Example 1:
Input: arr = [1,2,3]
Output: 2
Explanation: 1 and 2 are counted cause 2 and 3 are in arr.
``````
``````
Example 2:
Input: arr = [1,1,3,3,5,5,7,7]
Output: 0
Explanation: No numbers are counted, cause there's no 2, 4, 6, or 8 in arr.
``````
``````Example 3:
Input: arr = [1,3,2,3,5,0]
Output: 3
Explanation: 0, 1 and 2 are counted cause 1, 2 and 3 are in arr.
``````
``````Example 4:
Input: arr = [1,1,2,2]
Output: 2
Explanation: Two 1s are counted cause 2 is in arr.
``````

hashmap的變形應用

``````var countElements = function(arr) {
let hash = {}
arr.map(num => {
if(!hash[num +1 ]){
hash[num +1 ] = 1
} else{
hash[num +1 ] += 1
}
})
let index =  Object.keys(hash).filter((h,i) =>{
return arr.some(a =>{
return  a == h
})
})