leetCode

计划

  1. 先刷完
  2. 优化

两数相加 (208 ms)

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

        //创建链表节点
        function ListNode(val) {
            this.val = val;
            this.next = null;
        }

        //创建链表
        function createNodeList(array) {
            let head = new ListNode(0);
            let list = head;
            for (var i = 0; i < array.length; i++) {
                list.next = new ListNode(array[i]);
                list = list.next;
            }
            //返回引用后的head
            return head;
        }

        let l1 = createNodeList([2, 4, 3])
        let l2 = createNodeList([5, 6, 4])

        var addTwoNumbers = function (l1, l2) {
            console.log(l1);
            console.log(l2);
            let tag = 0; //进位

            let head = new ListNode(0);
            let up1 = head;
            let num = 0;
            while (l1 || l2) {
                let sums = (l1 ? l1.val : 0) + (l2 ? l2.val : 0) + tag;
                if (sums >= 10) {
                    tag = Math.floor(sums / 10);
                } else {
                    tag = 0;
                }

                up1.next = new ListNode(sums % 10);
                up1 = up1.next;
                l1 = (l1 && l1.next) || null;
                l2 = (l2 && l2.next) || null
            }
            if (tag) {
                //5+5 相加进位1 有进位新增
                up1.next = new ListNode(tag);
                up1 = up1.next;
            }

            return head.next;
        };
        console.log(addTwoNumbers(l1, l2))

3. 无重复字符的最长子串 (684 ms)

       /**
 * @param {string} s
 * @return {number}
 *
 * museuwzbczdejn
 * " "
 * bbbbb
 */

        let s ="abcabcbb";
        var lengthOfLongestSubstring = function (s) {
            s = s.split("");

            let nowStrLength = 0;
            let nowIndex = 0;
            let nowStr = "";
            function forEachNowStr(s) {
                nowStr = "";
                let index = nowIndex++ ;

                for (let i = index; i < s.length; i++) {
                    if (nowStr.indexOf(s[i]) !== -1) {

                        if (index === s.length) {
                            break;
                        }
                        nowStrLength = nowStr.length > nowStrLength? nowStr.length :nowStrLength;

                        forEachNowStr(s);
                        break;
                    } else {
                        if (index === s.length) {
                            break;
                        }
                        nowStr += s[i]
                        nowStrLength = nowStr.length > nowStrLength? nowStr.length :nowStrLength;
                    }

                }
            }

            forEachNowStr(s);
            return nowStrLength
        };
        console.log(lengthOfLongestSubstring(s))

4. 寻找两个有序数组的中位数

   /**
         * @param {number[]} nums1
         * @param {number[]} nums2
         * @return {number}
         */
        var findMedianSortedArrays = function(nums1, nums2) {
          let totalLength =   nums1.length + nums2.length;
          let totalArray = nums1.concat(nums2).sort(function (a,b) {
              return a-b
          });
          if(totalLength%2 === 0){
              return (totalArray[totalLength/2 -1]+totalArray[(totalLength/2)])/2;
          }else{
              return totalArray[Math.ceil(totalLength/2)-1]
          }
        };
        // nums1 = [1, 3]
        // nums2 = [2]

        // nums1 = [1, 2]
        // nums2 = [3, 4]

        nums1 =  [3];
        nums2 =  [-2,-1];
        console.log( findMedianSortedArrays(nums1, nums2))

5. 最长回文子串

给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。

 /**
       * @param {string} s
       * @return {string}
       * 输入: "babad"
       * 输出: "bab"
       * 注意: "aba" 也是一个有效答案。
       * 输入: "cbbd"
       * 输出: "bb"
       */
      let s = 'babad'
      var longestPalindrome = function (s) {
        function isRightStr(str){
          // let num =(str.length%2 === 0) ? parseInt(str.length/2)+1 : parseInt(str.length/2);
          let num =(str.length%2 === 0) ? parseInt(str.length/2) : parseInt(str.length/2);
          let strArr =  str.split('')
          let leftStr = str.split('').splice(0,num).join('')
          let rigthStr = str.split('').splice((str.length%2 === 0)?num:num+1,num).reverse().join('')
          if(leftStr !== '' && leftStr === rigthStr)   return true
        }
        let maxLength =0;
        let maxStr=''
        for(let i=0;i<s.length;i++){
          let currentMaxStr=''
          let currentMaxLength = 0;
          for(let m=i;m<s.length-1; m++){
            currentMaxStr+=s[m]
            if(isRightStr(currentMaxStr)){
              currentMaxLength = currentMaxStr.length
              maxStr = currentMaxStr
            }
          }
          if(currentMaxLength!==0 && currentMaxLength>=maxLength) {
            maxLength = currentMaxLength
            console.log(maxLength)
            console.log(maxStr)
          }
        }
      }
      longestPalindrome(s)