Day 1
Hello friends. I am going to start to share my coding challenge on LinkedIn today. I started it 3 days before on my LeetCode and GitHub pages. But after a while, I decided to share it using LinkedIn to show more people and to give more information.
On the first day, I solved 3 problems on LeetCode. The list of problems is below:
/*
* @param {string} haystack
* @param {string} needle
* @return {number}
*/
var strStr = function (haystack, needle) {
// checks full string named needle exists in haystack
if (haystack.includes(needle)) {
// if it returns true we will get the firs occurance using
// .indexOf() method
return haystack.indexOf(needle);
} else {
return -1;
}
};
2. Problem 58. Length of the last word. The solution is below.
/*
* @param {string} s
* @return {number}
*/
var lengthOfLastWord = function (s) {
s = s.trim().split(" ");
let lastWord = s[s.length - 1].length;
return lastWord;
};
3. Problem 67. Add Binary
领英推荐
var addBinary = function (a, b)
let len1 = a.length,
len2 = b.length,
max = Math.max(len1, len2),
res = "",
carry = 0,
val = 0;
for (var i = 0; i < max; i++) {
val = Number(a[len1 - 1 - i] || 0) + Number(b[len2 - 1 - i] || 0) + carry;
carry = Math.floor(val / 2);
res = (val % 2) + res;
}
if (carry) res = 1 + res;
return res;
}
Thank you for your time investing in my post. If you find it useful, share it with your friends.
My repository for challenges:
My LeetCode profile:
Full Stack Engineer at AIFU university
1 年assalamu aleykum, juda ajoyib izlanishdan to'xtamang