Skip to main content

· One min read

https://leetcode.com/problems/merge-two-sorted-lists/

Solution 1

/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} list1
* @param {ListNode} list2
* @return {ListNode}
*/
var mergeTwoLists = function (l1, l2) {
let merge = new ListNode();
let head = merge;

while (l1 && l2) {
if (l1.val < l2.val) {
merge.next = l1;
l1 = l1.next;
} else {
merge.next = l2;
l2 = l2.next;
}
merge = merge.next;
}

if (!l1) merge.next = l2;
else if (!l2) merge.next = l1;

return head.next;
};

Runtime: 89 ms

Memory Usage: 44.4 MB

· One min read

https://leetcode.com/problems/minimum-sum-of-four-digit-number-after-splitting-digits/

Solution 1

/**
* @param {number} num
* @return {number}
*/
var minimumSum = function (num) {
const numToArr = num.toString().split('');

for (let i = 1; i < numToArr.length; i++) {
for (let j = 0; j < i; j++) {
if (numToArr[i] < numToArr[j]) {
const smallNum = numToArr[i];
numToArr[i] = numToArr[j];
numToArr[j] = smallNum;
}
}
}

const new1 = numToArr[0] + numToArr[2];
const new2 = numToArr[1] + numToArr[3];

return Number(new1) + Number(new2);
};

· One min read

https://leetcode.com/problems/valid-anagram/

Solution 1

/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isAnagram = function (s, t) {
if (s.length !== t.length) {
return false;
}

const hashMapS = {};
const hashMapT = {};

for (let i = 0; i < s.length; i++) {
if (hashMapS[s[i]]) {
hashMapS[s[i]] += 1;
} else {
hashMapS[s[i]] = 1;
}

if (hashMapT[t[i]]) {
hashMapT[t[i]] += 1;
} else {
hashMapT[t[i]] = 1;
}
}

const compareST = Object.entries(hashMapS).map(([key, value]) => {
console.log(value, hashMapT[key]);
console.log(value === hashMapT[key]);
if (value === hashMapT[key]) {
return true;
}
return false;
});

return compareST.every((b) => b);
};

· One min read

https://leetcode.com/problems/remove-element/

Solution 1

/**
* @param {number[]} nums
* @param {number} val
* @return {number}
*/
var removeElement = function (nums, val) {
let remainElement = 0;

for (let i = 0; i < nums.length; i++) {
if (nums[i] === val) {
nums[i] = '_';
} else {
remainElement++;
}
}

nums.sort();

return remainElement;
};

Runtime: 76 ms

Memory Usage: 41.8 MB

Solution 2

/**
* @param {number[]} nums
* @param {number} val
* @return {number}
*/
var removeElement = function (nums, val) {
let samecount = 0;

for (let i = 0; i < nums.length; i++) {
if (nums[i] === val) {
samecount++;
} else {
nums[i - samecount] = nums[i];
}
}
return nums.length - samecount;
};

Runtime: 105 ms

Memory Usage: 42.5 MB

· One min read

https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/

Solution 1

/**
* @param {string} haystack
* @param {string} needle
* @return {number}
*/
var strStr = function (haystack, needle) {
if (haystack.length === 0) {
if (needle.length === 0) return 0;
return -1;
}
if (haystack.length !== 0) {
if (needle.length === 0) return 0;
}

let index;
const needleFirstStr = needle[0];

for (let i = 0; i < haystack.length; i++) {
if (haystack[i] === needleFirstStr) {
if (haystack.substring(i, i + needle.length) === needle) {
index = i;
break;
} else {
index = -1;
}
} else {
index = -1;
}
}

return index;
};

· One min read

https://leetcode.com/problems/word-pattern/

Solution 1

/**
* @param {string} pattern
* @param {string} s
* @return {boolean}
*/
var wordPattern = function (pattern, s) {
const splitS = s.split(' ');

if (splitS.length !== pattern.length) {
return false;
}

if (new Set(pattern.split('')).size !== new Set(splitS).size) {
return false;
}

const hashMap = {};
for (let i = 0; i < pattern.length; i++) {
if (hashMap[pattern[i]]) {
if (hashMap[pattern[i]] !== splitS[i]) {
return false;
}
} else {
hashMap[pattern[i]] = splitS[i];
}
}

return true;
};

· One min read

https://leetcode.com/problems/first-unique-character-in-a-string/

Solution 1

/**
* @param {string} s
* @return {number}
*/
var firstUniqChar = function (s) {
const hashMap = {};
for (let i = 0; i < s.length; i++) {
if (hashMap[s[i]]) {
hashMap[s[i]] = {
...hashMap[s[i]],
count: hashMap[s[i]].count + 1,
};
} else {
hashMap[s[i]] = {
index: i,
count: 1,
};
}
}

const filteredResult = Object.entries(hashMap)
.map(([key, value]) => {
if (value.count === 1) {
return value.index;
}
})
.filter((value) => value !== false && value !== undefined);

return filteredResult.length === 0 ? -1 : filteredResult[0];
};