基础编程题JS解法(二)

  1. 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.(剑指offer)
function printMatrix(matrix)
{
    var left = 0,
        right = matrix[0].length-1,
        top = 0,
        bottom = matrix.length-1,
        result = [],
        arr = []
    while(left <= right && top <= bottom) {
        //从左向右
        for(var i = left; i<=right; i++) {
            result.push(matrix[top][i]);
             
        }   
        //从上到下
       for(var j = top+1; j<=bottom; j++){
            result.push(matrix[j][right]);
 
        }    
        //判断横扫是否结束
        if(top != bottom) {
            for(var i = right-1; i>=left; i--){
                result.push(matrix[bottom][i]);
            }
        }
        if(left != right) {
            for(var i = bottom-1; i>top; i--){
                result.push(matrix[i][left]);
            }     
        }
        left++;
        top++;
        right--;
        bottom--;
    }
    return result;
}
  1. 对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。(剑指offer)
function LeftRotateString(str, n)
{
    if(str==null||str.length==0){
        return "";
    }
    var res = "";
    var s = str.slice(0, n);
    res = str.slice(n) + s;
    return res;
}
  1. 写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。(剑指offer)
function Add(num1, num2)
{
    var temp = num1;
    while(num2){
        temp = num1 ^ num2;   //异或,得到二进制的加运算
        num2 = (num1 & num2) << 1; //有进位的向左移1位,再循环做二进制加法
        num1 = temp;
    }
    return num1;
}
  1. 给定一个数组A[0,1,…,n-1],请构建一个数组B[0,1,…,n-1],其中B中的元素B[i]=A[0]A[1]…*A[i-1]A[i+1]…*A[n-1]。不能使用除法。(剑指offer)
    输入:
    [1,2,3,4,5]
    输出应该为:
    [120,60,40,30,24]
function multiply(array)
{
    if(array==null||array.length==0){
        return [];
    }
    var res = [];
    var len = array.length;
    for(var i = 0; i < len; i++){
        res[i] = 1;
        for(var j = 0; j < len; j++){
            if(j !== i){
                res[i] = res[i] * array[j];
            }
        }
    }
    return res;
}
  1. 二分查找。
 // 非递归算法
        function binary_search(arr, key) {
            var low = 0,
                high = arr.length - 1;
            while(low <= high){
                var mid = parseInt((high + low) / 2);
                if(key == arr[mid]){
                    return  mid;
                }else if(key > arr[mid]){
                    low = mid + 1;
                }else if(key < arr[mid]){
                    high = mid -1;
                }else{
                    return -1;
                }
            }
        };
        var arr = [1,2,3,4,5,6,7,8,9,10,11,23,44,86];
        var result = binary_search(arr,10);
        alert(result); // 9 返回目标元素的索引值       

    // 递归算法
        function binary_search(arr,low, high, key) {
            if (low > high){
                return -1;
            }
            var mid = parseInt((high + low) / 2);
            if(arr[mid] == key){
                return mid;
            }else if (arr[mid] > key){
                high = mid - 1;
                return binary_search(arr, low, high, key);
            }else if (arr[mid] < key){
                low = mid + 1;
                return binary_search(arr, low, high, key);
            }
        };
        var arr = [1,2,3,4,5,6,7,8,9,10,11,23,44,86];
        var result = binary_search(arr, 0, 13, 10);
        alert(result); // 9 返回目标元素的索引值  
  1. 输入两个链表,找出它们的第一个公共结点。
/*function ListNode(x){
    this.val = x;
    this.next = null;
}*/
function FindFirstCommonNode(pHead1, pHead2)
{
    var p1 = pHead1;
    var p2 = pHead2;
    while(p1!=p2){
        p1=p1==null?pHead2:p1.next;
        p2=p2==null?pHead1:p2.next;
    }
    return p1;
}

你可能感兴趣的:(自我总结)