A+ Leetcode 討論整理 week1

本週主要針對三種資料結構String、Array、Object來進行相關練習,並搭配幾題Leetcode上的題目作為範例,重點為如何解析題目背後的邏輯思維,故將相關的知識整理成筆記呈現。

Javascript Data Type

String(字串) — Immutable(不可變)、Primitive Type(基本型別):Number, String, Boolean, Null, Undefined, Symbol、Call by value(傳值參考):指向記憶體的值

#412 Fizz Buzz

題目述:Write a program that outputs the string representation of numbers from 1 to n.

/**
* @param {number} n
* @return {string[]}
*/
var fizzBuzz = function(n) {
const result = [];
for(let i = 1; i <= n; i++) {
if(i % 3 === 0 && i % 5 === 0) {
result.push("FizzBuzz");
} else {
if(i % 5 === 0) {
result.push("Buzz");
} else if(i % 3 === 0) {
result.push("Fizz");
} else {
result.push(i.toString());
}
}
}

return result;
};
/**
* @param {number} n
* @return {string[]}
*/
var fizzBuzz = function(n) {
let ans = [];
for (let i = 1; i <= n; i++) {
let str = '';
if (i % 3 === 0) {
str += 'Fizz';
}
if (i % 5 === 0) {
str += 'Buzz';
}
if (!str) {
str = i + '';
}
ans.push(str);
}
return ans;
};
/**
* @param {number} n
* @return {string[]}
*/
var fizzBuzz = function(n) {
const result = [];
const mappings = {
'3': 'Fizz',
'5': 'Buzz'
};

for(let i = 1; i <= n; i++) {
let str = '';
for(let num in mappings) {
if(i % parseInt(num, 10) === 0) {
str += mappings[num];
}
}
if(!str) {
str += i + '';
}
result.push(str);
}

return result;
};
/**
* @param {number} n
* @return {string[]}
*/
var fizzBuzz = function(n) {
const result = [];
let str = '';

for(let i = 1; i <= n; i++) {
str = ((i % 3 === 0 ? 'Fizz' : '') + (i % 5 === 0 ? 'Buzz' : '')) || i + '';
result.push(str);
}

return result;
};

#1 Two Sum

題目敘述:Given an array of integers nums and and integer target, return the indices of the two numbers such that they add up to target.

/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
for(var i = 0; i < nums.length; i++) {
for(var j = i + 1; j < nums.length; j++) {
if(nums[i] + nums[j] === target) {
return [i, j];
}
}
}
};
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
var map = {};

for(var i = 0; i < nums.length; i++) {
var val = nums[i];

if(map[target - val] >= 0) {
return [map[target - val], i];
} else {
map[val] = i;
}
}
};

#371 Sum of Two Integers

題目敘述:Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.

/**
* @param {number} a
* @param {number} b
* @return {number}
*/
var getSum = function(a, b) {
for (let i = 1; i <= Math.abs(b); i++) {
b >=0 ? a++ : a--;
}
return a;
};
  0  00        1  10
+ 1 10 ^ + 1 10 &
---------- ------------
1 10 10 << 1
2 100
/**
* @param {number} a
* @param {number} b
* @return {number}
*/
var getSum = function(a, b) {
if(b === 0) return a;
let sum = a ^ b;
let carry = (a & b) << 1;
return getSum(sum, carry);
};

#344 Reverse String

題目敘述:Write a function that reverses a string. The input string is given as an array of characters char[].

/**
* @param {character[]} s
* @return {void} Do not return anything, modify s in-place instead.
*/
var reverseString = function(s) {
var result = [];
for(var i = s.length-1; i >= 0; i--){
result.push(s[i]);
}
return result;
};
/**
* @param {character[]} s
* @return {void} Do not return anything, modify s in-place instead.
*/
var reverseString = function(s) {
let str = s.join('');
for(let i = 0; i < s.length; i++){
s[i] = str[s.length-i-1];
}
return;
};
/**
* @param {character[]} s
* @return {void} Do not return anything, modify s in-place instead.
*/
var reverseString = function(s) {
let len = s.length-1;
for(let i = 0; i < len / 2; i++){
const tmp = s[i];
s[i] = s[len-i];
s[len-i] = tmp;
}

return s;
};
var a = 1, b = 2, tmp;
tmp = a;
a = b;
b = tmp;
let a = 1, b = 2;
[a, b] = [b, a]
--------------------------
//下方兩個僅適用兩數為正的
var a = 1, b = 2;
a = a + b; // a = 3, b = 2
b = a - b; // a = 3, b = 1
a = a - b; // a = 2, b = 1
var a = 1, b = 2; // 二進位制:a = 0001, b = 0010
a = a ^ b; // 計算結果:a = 0011, b = 0010
b = a ^ b; // 計算結果:a = 0011, b = 0001
a = a ^ b; // 計算結果:a = 0010, b = 0001
/**
* @param {character[]} s
* @return {void} Do not return anything, modify s in-place instead.
*/
var reverseString = function(s) {
let low = 0;
let high = s.length - 1;

while(low < high){
[s[low], s[high]] = [s[high], s[low]]
high--;
low++;
}
return;
};
/**
* @param {character[]} s
* @return {void} Do not return anything, modify s in-place instead.
*/
var reverseString = function(s) {
var reverseStringHelper = function(left, right) {
if(left >= right) return;
[s[left], s[right]] = [s[right], s[left]];
return reverseStringHelper(left + 1, right - 1);
};
return reverseStringHelper(0, s.length - 1);
};