_ Algorithm titles in this article are from LeetCode.
Links:Leetcode-cn.com/problems/ad…
_
1. Sum of two numbers
Given an integer array nums and an integer target value target, find the two integers in the array and the target values and return their array subscripts. You can assume that there is only one answer for each type of input. However, the same element in the array cannot be repeated in the answer. You can return the answers in any order.
Example 1:
Input: nums = [2,7,11,15], target = 9 output: [0,1]Copy the code
Example 2:
Input: nums = [3,2,4], target = 6Copy the code
Example 3:
Input: nums = [3,3], target = 6Copy the code
Answer:
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
for(var i = 0; i < nums.length; i++) {
for(var j = i + 1; j < nums.length; j++){
if(nums[j] + nums[i] === target) {
return [i, j]
}
}
}};
Copy the code
2. Add two numbers
You are given two non-empty linked lists representing two non-negative integers. Each digit is stored in reverse order, and only one digit can be stored per node. You add the two numbers and return a linked list representing the sum in the same form. You can assume that neither of these numbers will start with 0, except for the number 0.
Example 1:
支那
支那
Input: l1 = [2, 3], l2 =,6,4 [5] output:,0,8 [7] : 342 + 465 = 807.Copy the code
Example 2:
Input: L1 = [0], L2 = [0] Output: [0]Copy the code
Example 3:
Input: l1 =,9,9,9,9,9,9 [9], l2 =,9,9,9 [9] output:,9,9,9,0,0,0,1 [8]Copy the code
Answer:
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
var addTwoNumbers = function(l1, l2) {
l1 = (l1.reverse().join('')) * 1
l2 = (l2.reverse().join('')) * 1
return String(l1 + l2).split('').reverse()
};
Copy the code
I don’t know why, the reserve is not a function, but there is no problem with private verification
3. The oldest string without repeating characters
Given a string, find the length of the smallest string that does not contain repeating characters.
Example 1:
Input: s = "abcabcbb" Output: 3 Explanation: Since the oldest string without repeating characters is "ABC", its length is 3.Copy the code
Example 2:
Input: s = "BBBBB" Output: 1 Explanation: Since the oldest string without repeating characters is "b", its length is 1.Copy the code
Example 3:
Input: s = "pwwkew" Output: 3 Explanation: Since the oldest string without repeating characters is "wKE", its length is 3. Note that your answer must be the length of the substring, "pwke" is a subsequence, not a substring.Copy the code
Example 4:
Input: s = "output: 0Copy the code
Answer:
/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function (s) {
var map = [];
var resultLen = 0;
for (var i = 0; i < s.length; i++) {
var el = s[i];
if (map.length === 0) {
map[0] = el
} else {
var index = map[map.length - 1] ? map[map.length - 1].indexOf(el) : -1;
if (index === -1) {
map[map.length - 1] = map[map.length - 1] ? map[map.length - 1] + el : el;
} else {
map[map.length] = map[map.length - 1].slice(index + 1, map[map.length - 1].length) + el;
}
}
}
for (var i = 0; i < map.length; i++) {
var el = map[i];
if (el.length > resultLen) {
resultLen = el.length;
}
}
return resultLen;
};
Copy the code
If you have any questions, please leave a message.
I will also update some other front-end dry goods from time to time, or such algorithm contact, welcome to praise, attention, message. Thanks for watching!!