First problems from LeetCode

- 7 mins

During this month I have been doing some research about the interview process in Apple, and from Glassdoor answers I discovered LeetCode.

As we can read from this Quora answer:

The purpose of LeetCode is to provide you hands-on training on real coding interview questions. The Online Judge gives you immediate feedback on the correctness and efficiency of your algorithm which facilitates a great learning experience.

So, for the few next days I did a couple of the problems tagged as easy and medium in the platform. Here the ones I did until April 30th.

771. Jewels and Stones

/**
 * @param {string} J
 * @param {string} S
 * @return {number}
 */
var numJewelsInStones = function(J, S) {
    return S.split("").filter( elm => J.split("").includes(elm) ).length;
};

977. Squares of a Sorted Array

/**
 * @param {number[]} A
 * @return {number[]}
 */
var sortedSquares = function(A) {
    return A.map(  (elm => elm * elm) ).sort( (n0, n1) => n0 - n1 );
};

709. To Lower Case

 /**
 * @param {string} str
 * @return {string}
 */
var toLowerCase = function(str) {
    var cast_lower = function(value) {
        if(value >= 65 && value <= 90) { 
            value = value + 32;
        }
        return(String.fromCharCode(value));
    }
    return str.split("").map( elm => cast_lower(elm.charCodeAt(0)) ).join("")
};

344. Reverse String

/**
 * @param {character[]} s
 * @return {void} Do not return anything, modify s in-place instead.
 */
var reverseString = function(s) {
    let lim = Math.floor(s.length / 2);
    for(ii = 0; ii < lim; ++ii) {
        let replace = s[ii];
        s[ii] = s[s.length - 1 - ii];
        s[s.length - 1 - ii] = replace;
    }
};

2. Add Two Numbers

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
var addTwoNumbers = function(l1, l2) {
    let rst = new ListNode(0);
    let cur = rst;
    let car = 0;
    while (l1 != null || l2 != null) {
        let v1 = l1 == null ? 0 : l1.val
        let v2 = l2 == null ? 0 : l2.val
        let val = v1 + v2 + car;
        car = Math.floor(val / 10);
        cur.next = new ListNode(val % 10);
        cur = cur.next;
        l1 = l1 == null ? null : l1.next;
        l2 = l2 == null ? null : l2.next;
    }
    if(car != 0) {
        cur.next = new ListNode(car);
    }
    return(rst.next);
};

8. String to Integer (atoi)

/**
 * @param {string} str
 * @return {number}
 */
var myAtoi = function(str) {
    let values = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
    str = str.replace(/^\s+|\s+$/g, '').split('');
    if(!values.includes(str[0]) && str[0] != '-' && str[0] != '+') {
        return(0);
    } else {
        let str2 = [];
        for(ii = 0; ii < str.length; ++ii) {
            if(values.includes(str[ii]) || (ii == 0 && str[ii] == '-')) {
                str2.push(str[ii])
            } else if (ii == 0 && str[ii] == '+') {
            } else {
                break;
            }
        }
        if(str2.length == 0 | (str2.length == 1 && !values.includes(str2[0]))) {
            return(0);
        }
        str2 = parseInt(str2.join(''));
        if(str2 > 2147483647) {
            return(2147483647);
        } else if(str2 < -2147483648) {
           return(-2147483648);
        } else {
            return(str2);
        }
    }
};
Carles Hernandez-Ferrer

Carles Hernandez-Ferrer

Bioinformatics, data analysis and software development

rss facebook twitter github gitlab youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora quora