每天一点LeetCode

  1. add-two-numbers
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    // 自己写的
    var addTwoNumbers = function(l1, l2) {
    let numberL1 = ''
    let numberL2 = ''
    let add=[]
    for(let i =0;i<l1.length;i++){
    l1[i]= l1[i].toString()
    numberL1=numberL1+l1[i]
    }
    console.log('numberL1',numberL1)
    for(let i =0;i<l2.length;i++){
    l2[i]= l2[i].toString()
    numberL2=numberL2+l2[i]
    }
    numberL1=Number(numberL1)
    numberL2=Number(numberL2)
    numberL1 =(numberL1+numberL2).toString()

    for(let i = 0;i < numberL1.length;i++){
    console.log('numberL1.charAt(i)',numberL1)
    add.push(numberL1.charAt(i))
    }
    console.log('add',add)
    // return add.reverse()
    };
    let l1 = [2,4,3]
    let l2 = [5,6,4]
    addTwoNumbers(l1,l2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//题解里luobo大佬提供的思路 :https://leetcode-cn.com/problems/add-two-numbers/solution/liang-shu-xiang-jia-jian-dan-yi-dong-by-luobo-3/
var addTwoNumbers = function(l1, l2) {
let node = new ListNode('head')
let temp = node , sum , n = 0
while( l1 || l2 ){
const n1 = l1 ? l1.val : 0
const n2 = l2 ? l2.val : 0
sum = n1 + n2 + n
temp.next = new ListNode( sum % 10 )
temp = temp.next
n = parseInt( sum / 10 )
if(l1) l1 = l1.next
if(l2) l2 = l2.next
}
if( n > 0 ) temp.next = new ListNode(n)
return node.next
};
  1. longest-substring-without-repeating-characters
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    // 复杂度较高,且有BUG
    var lengthOfLongestSubstring = function(s) {
    let tempObject = {}
    let tempArray = []
    // console.log('11111111')
    for(let i = 0;i<s.length-1;i++){
    let temp = ''
    temp = s.substr(i,1) // 手里拿着对比
    nextTemp = s.indexOf(temp, i+1)
    // console.log(nextTemp - i)// 长度
    s.substr(i,nextTemp-i)
    tempObject[nextTemp]=s.substr(i,nextTemp-i)
    tempArray.push(nextTemp - i)
    console.log(tempObject)

    // if(temp === nextTemp){

    // }
    }
    console.log(s)
    };
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    // 作者:LeetCode-Solution
// 链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/solution/wu-zhong-fu-zi-fu-de-zui-chang-zi-chuan-by-leetc-2/
var lengthOfLongestSubstring = function(s) {
// 哈希集合,记录每个字符是否出现过
const occ = new Set();
const n = s.length;
// 右指针,初始值为 -1,相当于我们在字符串的左边界的左侧,还没有开始移动
let rk = -1, ans = 0;
for (let i = 0; i < n; ++i) {
if (i != 0) {
// 左指针向右移动一格,移除一个字符
occ.delete(s.charAt(i - 1));
}
while (rk + 1 < n && !occ.has(s.charAt(rk + 1))) {
// 不断地移动右指针
occ.add(s.charAt(rk + 1));
++rk;
}
// 第 i 到 rk 个字符是一个极长的无重复字符子串
ans = Math.max(ans, rk - i + 1);
}
return ans;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var lengthOfLongestSubstring = function(s) {
let arr = [], max = 0
for(let i = 0; i < s.length; i++) {
let index = arr.indexOf(s[i])
if(index !== -1) {
arr.splice(0, index+1);
}
arr.push(s.charAt(i))
max = Math.max(arr.length, max)
}
return max
};

// 作者:user7746o
// 链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/solution/zi-jie-leetcode3wu-zhong-fu-zi-fu-de-zui-chang-zi-/
// 来源:力扣(LeetCode)
// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  1. median-of-two-sorted-arrays
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    var findMedianSortedArrays = (nums1, nums2) => {
    let len1 = nums1.length, len2 = nums2.length
    if (len1 > len2) return findMedianSortedArrays(nums2, nums1)
    let len = len1 + len2
    let start = 0, end = len1
    let partLen1, partLen2

    while (start <= end) {
    partLen1 = (start + end) >> 1
    partLen2 = ((len + 1) >> 1) - partLen1

    let L1 = partLen1 === 0 ? -Infinity : nums1[partLen1 - 1]
    let L2 = partLen2 === 0 ? -Infinity : nums2[partLen2 - 1]
    let R1 = partLen1 === len1 ? Infinity : nums1[partLen1]
    let R2 = partLen2 === len2 ? Infinity : nums2[partLen2]

    if (L1 > R2) {
    end = partLen1 - 1
    } else if (L2 > R1) {
    start = partLen1 + 1
    } else { // L1 <= R2 && L2 <= R1
    return len % 2 === 0 ?
    (Math.max(L1, L2) + Math.min(R1, R2)) / 2 :
    Math.max(L1, L2)
    }
    }
    }

    //作者:hyj8
    //链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/solution/fen-duan-jie-fa-zhen-shi-miao-a-by-hyj8/
    //来源:力扣(LeetCode)
    //著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
var findMedianSortedArrays = (nums1, nums2) => {
let len1 = nums1.length, len2 = nums2.length
if (len1 > len2) return findMedianSortedArrays(nums2, nums1)
let len = len1 + len2
let start = 0, end = len1
let partLen1, partLen2

while (start <= end) {
partLen1 = (start + end) >> 1
partLen2 = ((len + 1) >> 1) - partLen1

let L1 = partLen1 === 0 ? -Infinity : nums1[partLen1 - 1]
let L2 = partLen2 === 0 ? -Infinity : nums2[partLen2 - 1]
let R1 = partLen1 === len1 ? Infinity : nums1[partLen1]
let R2 = partLen2 === len2 ? Infinity : nums2[partLen2]

if (L1 > R2) {
end = partLen1 - 1
} else if (L2 > R1) {
start = partLen1 + 1
} else { // L1 <= R2 && L2 <= R1
return len % 2 === 0 ?
(Math.max(L1, L2) + Math.min(R1, R2)) / 2 :
Math.max(L1, L2)
}
}
}

//作者:hyj8
//链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/solution/fen-duan-jie-fa-zhen-shi-miao-a-by-hyj8/
// 来源:力扣(LeetCode)
//著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  1. longest-palindromic-substring
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    var longestPalindrome = function(s) {
    // babad
    // tag : dp
    if (!s || s.length === 0) return "";
    let res = s[0];

    const dp = [];

    // 倒着遍历简化操作, 这么做的原因是dp[i][..]依赖于dp[i + 1][..]
    for (let i = s.length - 1; i >= 0; i--) {
    dp[i] = [];
    for (let j = i; j < s.length; j++) {
    if (j - i === 0) dp[i][j] = true;
    // specail case 1
    else if (j - i === 1 && s[i] === s[j]) dp[i][j] = true;
    // specail case 2
    else if (s[i] === s[j] && dp[i + 1][j - 1]) {
    // state transition
    dp[i][j] = true;
    }

    if (dp[i][j] && j - i + 1 > res.length) {
    // update res
    res = s.slice(i, j + 1);
    }
    }
    }

    //作者:fe-lucifer
    //链接:https://leetcode-cn.com/problems/longest-palindromic-substring/solution/hui-wen-wen-ti-dong-tai-gui-hua-jspython5-zui-chan/
    //来源:力扣(LeetCode)
    //著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
var longestPalindrome = function (s) {
if (!s || !s.trim()) return '';
if (s.length === 1) return s;
if (s.length === 2) return s[0] === s[1] ? s[0] + s[1] : s[0];

var sArray = s.split('');
var x = [...new Set(sArray)];
var result;
var result1;
var result2;

if (x.length === 1) return s;

// 以item为对称点 找到回文串
result1 = sArray.reduce(function(memo, item, idx, self) {
var ret;

if (idx === 0 ) return memo;
if (typeof self[idx - 1] !== 'string' || self[idx - 1] !== self[idx + 1]) return memo;

memo = (function () {
var middle = idx;
var i = 1;
var result = item;

while (typeof self[middle + i] === 'string' && typeof self[middle - i] === 'string' && self[middle + i] === self[middle - i]) {
++i;
}

if (i === 1) {
return memo;
}

i = i -1;
result = self.slice(middle - i, middle + i+1).join('');

return result.length > (memo && memo.length || 1) ? result : memo;
})();

return memo;
}, sArray[0]);

// 以item 和 next 两个一起为对称点 找到回文串
result2 = sArray.reduce(function(memo, item, idx, self) {
// axxa 中心轴是两个字母
if (item !== self[idx + 1]) return memo;
memo = (function () {
var left = idx;
var right = idx + 1;
var i = 1;
var result = item + item;

while (typeof self[right + i] === 'string' &&typeof self[left - i] === 'string' && self[left - i] === self[right + i]) {
++i;
}

if (i === 1) return result.length > (memo && memo.length || 1) ? result : memo;

i = i - 1;
result = self.slice(left - i, right + i + 1).join('');

return result.length > (memo && memo.length || 1)? result : memo;
})();

return memo;

}, sArray[0]);

return result1.length > result2.length ? result1 : result2;
}

//作者:chubby-2
//链接:https://leetcode-cn.com/problems/longest-palindromic-substring/solution/zui-chang-hui-wen-zi-chuan-by-chubby-2/
//来源:力扣(LeetCode)
//著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

感谢您的阅读。 🙏