Some leetcode’s questions I have done recently

Two Sum

Given an array of integers, find two numbers such that they add up to a specific target number.

var twoSum = function(nums, target) {
    var str1 = '',
    	str2='',
    	len = nums.length,
    	obj = {},
    	temp,
    	tempnum,
    	tempobj;
    for(var i = 0;i < len;i++){
        tempnum = nums[i];
    	obj[tempnum] = i + 1;
    }
    for(i = 0;i < len;i++){
    	temp = target - nums[i];
    	tempobj = obj[temp];
    	if(tempobj && (i+1)!==tempobj){
    		str1 =i + 1;
    		str2 = tempobj;
    		break;
    	}
    }
    return [str1,str2];
};

基本思路就是遍历一遍数组建立一个hash表,然后再次遍历一次数组,看看 目标值减去数组当前循环的值得差是否在哈希表中。

Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

var addTwoNumbers = function(l1, l2) {
   var str1,str2,
            num1,num2,
            arr1 = [],arr2 = [],
            sum,sarr = [],
            temp,
            status = null,
            ns,
            nstemp,
            nshead,
            narr = [];
       
        while(l1){
            arr1.push(l1.val);
            l1 = l1.next;
        }
        while(l2){
            arr2.push(l2.val);
            l2 = l2.next;
        }
        num1 = +arr1.reverse().join('');
        num2 = +arr2.reverse().join('');
        sum = num1 + num2;
        sarr = (sum + '').split('').reverse();
        for(var i = 0,len = sarr.length;i < len;i++){
            narr.push(+sarr[i]);
        }   
        return narr;

};

其实就是两个链表,分别读出来每个节点的数字,存入数组,然后数组在逆序转为数字。之后再把和分离成数组,之后逆序。
不过不太明白leetcode的js规则,返回一个link的head会报错,反而返回一个数组是正确。

Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.
判断回文数不要申请额外空间

var isPalindrome = function(x) {
    x = x + '';
    var bol = true;
    for(var i = 0,len = x.length;i < len/2;i++){
    	if(x.charAt(i) !== x.charAt(len - i - 1)){
    		bol = false;
    		break;
    	}
    }
    return bol;
};

其实就是同时从前像中间判断,大一时候的作业题。

Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.

Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.

var swapPairs = function(head) {
        var rtemp = head,
            temp,
            vtemp;
        if(!head || !head.next){
            return head;
        }
        while(head && head.next){
                temp = head.next;
                vtemp = temp.val; 
                temp.val = head.val;
                head.val = vtemp;
                head = head.next.next;
        }
        return rtemp;

}

貌似做的和题意有点不搭,下次再说吧。应该只改变指针的指向的

Remove Linked List Elements

Remove all elements from a linked list of integers that have value val.

Example
Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6
Return: 1 –> 2 –> 3 –> 4 –> 5

var removeElements = function(head, val) {
   var i = 0,prevtemp = head,temp = head,arr = [];
    if(!head){
        return [];
    }
    while(temp){
        prevtemp = temp;
        if(temp.val === val){
            prevtemp.next = temp.next;
        }else{
            arr.push(temp.val);
        }
        temp = temp.next;
    }
    return arr;
};

the solution is if next is existed,go down the link list until the val is match the given target,then remove the node,and return the rest of linked list

Power of Two
Given an integer, write a function to determine if it is a power of two.
确定一个数是不是一个2的幂

var isPowerOfTwo = function(n) {
    var temp = true;
    if(n === 0){
        return false;
    }else if(n === 1){
        return true;
    }else if(n < 0){
        return false;
    }
    while(n >= 0){
        n = n/2;
        if(n === 0.5){
            break;
        }else if(n !== parseInt(n)){
            temp = false;
            break;
        }
    }
    return temp;
};

有点忘记幂的一些条件了,正数的幂都是正数,任何数除了0的0次方都是1等等,直接一个数不断除以2判断的,感觉这个方法不太好,后续有空了,再看看。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据