Skip to content

Commit

Permalink
Add .prettierrc
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyslu committed Aug 17, 2020
1 parent 13baa74 commit f1b7187
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 198 deletions.
8 changes: 8 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"tabWidth": 2,
"singleQuote": true,
"printWidth": 180,
"semi": false,
"trailingComma": "none",
"htmlWhitespaceSensitivity": "ignore"
}
34 changes: 17 additions & 17 deletions LC-1/LC-1-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
* @return {number[]}
*/

var twoSum = function(nums, target) {
/**
* Concepts: Brute Force
* Find out each pair of element in the list that sum is equal to target
*/

if (nums == null || nums.length === 0) {
return nums;
}
for (let i = 0; i < nums.length - 1; ++i) {
for (let j = i + 1; j < nums.length; ++j) {
if (nums[i] + nums[j] == target) {
return [i, j];
}
}
var twoSum = function (nums, target) {
/**
* Concepts: Brute Force
* Find out each pair of element in the list that sum is equal to target
*/

if (nums == null || nums.length === 0) {
return nums
}
for (let i = 0; i < nums.length - 1; ++i) {
for (let j = i + 1; j < nums.length; ++j) {
if (nums[i] + nums[j] == target) {
return [i, j]
}
}
return {};
};
}
return {}
}
44 changes: 22 additions & 22 deletions LC-1/LC-1-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@
* @return {number[]}
*/

var twoSum = function(nums, target) {
/**
* Concepts: Two-pass Approach
* 1. Use hash table to store the value and the index in the list
* 2. Use "Map" to find out the satisfied value
*/
var twoSum = function (nums, target) {
/**
* Concepts: Two-pass Approach
* 1. Use hash table to store the value and the index in the list
* 2. Use "Map" to find out the satisfied value
*/

if (nums == null || nums.length === 0) {
return nums;
}

let hash = new Map();
for (let i = 0; i < nums.length; ++i) {
hash.set(nums[i], i);
}

for (let i = 0; i < nums.length; ++i) {
let diff = tagret - nums[i];
if (hash.has(diff) && hash.get(diff) !== i) {
return [hash.get(diff), i];
}
if (nums == null || nums.length === 0) {
return nums
}

let hash = new Map()
for (let i = 0; i < nums.length; ++i) {
hash.set(nums[i], i)
}

for (let i = 0; i < nums.length; ++i) {
let diff = tagret - nums[i]
if (hash.has(diff) && hash.get(diff) !== i) {
return [hash.get(diff), i]
}
return {};
};
}
return {}
}
42 changes: 21 additions & 21 deletions LC-1/LC-1-3.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@
* @return {number[]}
*/

var twoSum = function(nums, target) {
/**
* Concepts: One-pass Approach
* Modify from two-pass approach. Just find out the satisfied value in one-pass
* 1. Find out the satisfied value in each iteration
* 2. If not found, then insert into hash table
*/
var twoSum = function (nums, target) {
/**
* Concepts: One-pass Approach
* Modify from two-pass approach. Just find out the satisfied value in one-pass
* 1. Find out the satisfied value in each iteration
* 2. If not found, then insert into hash table
*/

if (nums == null || nums.length === 0) {
return nums;
}

let hash = new Map();
for (let i = 0; i < nums.length; ++i) {
let diff = target - nums[i];
if (hash.has(diff) && hash.get(diff) !== i) {
return [hash.get(diff), i];
} else {
hash.set(nums[i], i);
}
if (nums == null || nums.length === 0) {
return nums
}

let hash = new Map()
for (let i = 0; i < nums.length; ++i) {
let diff = target - nums[i]
if (hash.has(diff) && hash.get(diff) !== i) {
return [hash.get(diff), i]
} else {
hash.set(nums[i], i)
}
return {};
};
}
return {}
}
3 changes: 3 additions & 0 deletions LC-1470/LC-1470.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ using namespace std;

class Solution {
public:
/**
* Concepts: Use extra space for getting the result of shuffle
*/
vector<int> shuffle(vector<int>& nums, int n) {
vector<int> result(2 * n);
for (int i = 0, j = n, k = 0; i < n && j < 2 * n && k < 2 * n; ++i, ++j, k += 2) {
Expand Down
20 changes: 10 additions & 10 deletions LC-1470/LC-1470.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
* @return {number[]}
*/
var shuffle = function (nums, n) {
let result = [];
for (
let i = 0, j = n, k = 0;
i < n && j < 2 * n && k < 2 * n;
++i, ++j, k += 2
) {
result[k] = nums[i];
result[k + 1] = nums[j];
/**
* Concepts: Use extra space for getting the result of shuffle
*/

let result = []
for (let i = 0, j = n, k = 0; i < n && j < 2 * n && k < 2 * n; ++i, ++j, k += 2) {
result[k] = nums[i]
result[k + 1] = nums[j]
}
return result;
};
return result
}
12 changes: 8 additions & 4 deletions LC-1480/LC-1480.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
* @return {number[]}
*/
var runningSum = function (nums) {
let sums = [nums[0]];
/**
* Concepts: Get the sum from previous sum and current number
*/

let sums = [nums[0]]
for (let i = 1; i < nums.length; ++i) {
sums[i] = sums[i - 1] + nums[i];
sums[i] = sums[i - 1] + nums[i]
}
return sums;
};
return sums
}
80 changes: 41 additions & 39 deletions LC-2/LC-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,45 @@
* @return {ListNode}
*/

var addTwoNumbers = function(l1, l2) {
/**
* Concepts:
* Append the result of the sum of each digit at the end of list and notice the carry
*/

if (l1 == null || l2 == null) {
return {};
}

let curr1 = l1, curr2 = l2;
let result = new ListNode(null), head = result;
let carry = 0;

while (curr1 != null || curr2 != null) {
let sum = 0;
if (curr1 == null && curr2 != null) {
sum = curr2.val + carry;
} else if (curr2 == null && curr1 != null) {
sum = curr1.val + carry;
} else {
sum = curr1.val + curr2.val + carry;
}

carry = Math.floor(sum / 10);
let node = new ListNode(sum % 10);
head.next = node;
head = head.next;
curr1 = (curr1 == null) ? null : curr1.next;
curr2 = (curr2 == null) ? null : curr2.next;
}

// Append the last carry at the end of list
if (carry !== 0) {
let node = new ListNode(carry);
head.next = node;
var addTwoNumbers = function (l1, l2) {
/**
* Concepts:
* Append the result of the sum of each digit at the end of list and notice the carry
*/

if (l1 == null || l2 == null) {
return {}
}

let curr1 = l1,
curr2 = l2
let result = new ListNode(null),
head = result
let carry = 0

while (curr1 != null || curr2 != null) {
let sum = 0
if (curr1 == null && curr2 != null) {
sum = curr2.val + carry
} else if (curr2 == null && curr1 != null) {
sum = curr1.val + carry
} else {
sum = curr1.val + curr2.val + carry
}

return result.next;
};

carry = Math.floor(sum / 10)
let node = new ListNode(sum % 10)
head.next = node
head = head.next
curr1 = curr1 == null ? null : curr1.next
curr2 = curr2 == null ? null : curr2.next
}

// Append the last carry at the end of list
if (carry !== 0) {
let node = new ListNode(carry)
head.next = node
}

return result.next
}
77 changes: 39 additions & 38 deletions LC-5/LC-5-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,44 @@
* @return {string}
*/

var longestPalindrome = function(s) {
/**
* Concepts: Manacher's algorithm (DP)
* 1. Use a useless character to make the length of string be odd
* 2. Think of the concept of mirror to find the symmetry character for each character
* 3. Remember to remove the useless character before return the result
* [Ref] http://www.csie.ntnu.edu.tw/~u91029/Palindrome.html#3
*/

let strLength = s.length;
if (strLength < 2) {
return s;
}

// Preprocess for Manacher's algorithm
let ref = "#".repeat(2 * strLength + 1);
for (let i = 0; i < strLength; ++i) {
let index = 2 * i + 1;
ref = ref.substr(0, index) + s[i] + ref.substr(index + 1);
var longestPalindrome = function (s) {
/**
* Concepts: Manacher's algorithm (DP)
* 1. Use a useless character to make the length of string be odd
* 2. Think of the concept of mirror to find the symmetry character for each character
* 3. Remember to remove the useless character before return the result
* [Ref] http://www.csie.ntnu.edu.tw/~u91029/Palindrome.html#3
*/

let strLength = s.length
if (strLength < 2) {
return s
}

// Preprocess for Manacher's algorithm
let ref = '#'.repeat(2 * strLength + 1)
for (let i = 0; i < strLength; ++i) {
let index = 2 * i + 1
ref = ref.substr(0, index) + s[i] + ref.substr(index + 1)
}

// Manacher's algorithm
let maxRadius = 1,
maxIndex = 1
for (let i = 0; i < ref.length; ++i) {
let radius = 1
while (i - radius >= 0 && i + radius < ref.length && ref[i - radius] === ref[i + radius]) {
++radius
}

// Manacher's algorithm
let maxRadius = 1, maxIndex = 1;
for (let i = 0; i < ref.length; ++i) {
let radius = 1;
while (i - radius >= 0 && i + radius < ref.length && ref[i - radius] === ref[i + radius]) {
++radius;
}
if (radius > maxRadius) {
maxRadius = radius;
maxIndex = i;
}
if (radius > maxRadius) {
maxRadius = radius
maxIndex = i
}
--maxRadius;

// Remove the character '#'
let result = ref.substr(maxIndex - maxRadius, maxRadius * 2 + 1);
result = result.replace(/#/g, '');
return result;
};
}
--maxRadius

// Remove the character '#'
let result = ref.substr(maxIndex - maxRadius, maxRadius * 2 + 1)
result = result.replace(/#/g, '')
return result
}
Loading

0 comments on commit f1b7187

Please sign in to comment.