Skip to main content

13 posts tagged with "twoPointers"

View All Tags

· One min read

https://leetcode.com/problems/reverse-only-letters/description/

/**
* @param {string} s
* @return {string}
*/
var reverseOnlyLetters = function (s) {
if (s.length === 1) {
return s;
}

let result = s.split('');
let leftIndex = 0;
let rightIndex = s.length - 1;

while (leftIndex <= rightIndex) {
const isLeftEngString = isEngString(s[leftIndex]);
const isRightEngString = isEngString(s[rightIndex]);

if (isLeftEngString && isRightEngString) {
result[leftIndex] = s[rightIndex];
result[rightIndex] = s[leftIndex];
leftIndex++;
rightIndex--;
} else {
if (!isLeftEngString) {
leftIndex++;
}
if (!isRightEngString) {
rightIndex--;
}
}
}
return result.join('');
};

const isEngString = (letter) => {
const engLettersRegex = /[a-zA-Z]/;
return engLettersRegex.test(letter);
};

Runtime 59 ms Beats 97.4%

Memory 42.6 MB Beats 30.37%

· One min read

https://leetcode.com/problems/shortest-distance-to-a-character/description/

Solution 1.

/**
* @param {string} s
* @param {character} c
* @return {number[]}
*/
var shortestToChar = function (s, c) {
const targets = [];
for (let i = 0; i < s.length; i++) {
if (s[i] === c) {
targets.push(i);
}
}

let targetIndex = 0;
const result = [];
for (let j = 0; j < s.length; j++) {
const nextIndex = targetIndex + 1;
if (!isNaN(targets[nextIndex])) {
result.push(
Math.min(
Math.abs(j - targets[targetIndex]),
Math.abs(j - targets[nextIndex]),
),
);

if (j - targets[nextIndex] === 0) {
targetIndex++;
}
} else {
if (s[j] === c) {
result.push(0);
} else {
result.push(Math.abs(j - targets[targetIndex]));
}
}
}

return result;
};

Runtime 131 ms Beats 21.28%

Memory 44.5 MB Beats 54.4%

· One min read

https://leetcode.com/problems/move-zeroes/description/

Solution 1

/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var moveZeroes = function (nums) {
let zeroCount = 0;

for (let i = 0; i < nums.length; i++) {
if (nums[i] !== 0) {
const zero = nums[zeroCount];
nums[zeroCount] = nums[i];
nums[i] = zero;
zeroCount++;
}
}

return nums;
};

Runtime 147 ms Beats 64.1%

Memory 47 MB Beats 27.90%

· One min read

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

Solution 1

/**
* @param {string} s
* @return {boolean}
*/
var isPalindrome = function (s) {
const lowerCaseString = s.toLowerCase();
let onlyLowerCase = '';

for (let i = 0; i < lowerCaseString.length; i++) {
const regex = /^[a-z0-9]/;
if (regex.test(lowerCaseString[i])) {
onlyLowerCase += lowerCaseString[i];
}
}

let reverseOnlyLowerCase = '';

for (let j = onlyLowerCase.length - 1; j >= 0; j--) {
reverseOnlyLowerCase += onlyLowerCase[j];
}

console.log(onlyLowerCase, reverseOnlyLowerCase);

return onlyLowerCase === reverseOnlyLowerCase;
};

· One min read

https://leetcode.com/problems/linked-list-cycle/

Solution 1

/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/

/**
* @param {ListNode} head
* @return {boolean}
*/
var hasCycle = function (head) {
if (head === null) {
return false;
}

let curr = head;
let next = head.next;

while (next && next.next) {
curr = curr.next;
next = next.next.next;

if (curr === next) {
return true;
}
}

return false;
};

· One min read

https://leetcode.com/problems/intersection-of-two-linked-lists/

Solution 1

/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/

/**
* @param {ListNode} headA
* @param {ListNode} headB
* @return {ListNode}
*/
var getIntersectionNode = function (headA, headB) {
if (!headA || !headB) {
return null;
}

let nodeA = headA;
let nodeB = headB;

while (nodeA !== nodeB) {
nodeA = nodeA.next;
nodeB = nodeB.next;

console.log(nodeA, nodeB);

if (nodeA === nodeB) {
return nodeA;
}

if (!nodeA) {
nodeA = headB;
}
if (!nodeB) {
nodeB = headA;
}
}

return nodeA;
};

· One min read

https://leetcode.com/problems/happy-number/

Solution 1

/**
* @param {number} n
* @return {boolean}
*/
var isHappy = function (n) {
let set = new Set();

while (!set.has(n)) {
set.add(n);
let s = n.toString();
n = 0;
for (let i = 0; i < s.length; ++i) {
n += squares[s[i]];
}
if (n === 1) return true;
}

return false;
};

const squares = {
0: 0,
1: 1,
2: 4,
3: 9,
4: 16,
5: 25,
6: 36,
7: 49,
8: 64,
9: 81,
};

· 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;
};