leetcode剑指offer75题

1 替换空格

leetcode剑指offer75题_第1张图片

var replaceSpace = function(s) {
    const str = s.split(' ');
    return str.join('%20')
};

2 左旋转字符串

leetcode剑指offer75题_第2张图片

var reverseLeftWords = function(s, n) {
    const s1 = s.slice(n)
    const s2 = s.slice(0,n)
    return s1+s2
};

3 表示数值的字符串

leetcode剑指offer75题_第3张图片leetcode剑指offer75题_第4张图片

//\d+ 匹配整数 1次或多次
//(\.\d*)? 满足小数条件12 ?匹配0次或1次, 0次即整数 1次即小数
// \.\d+ 满足小数条件3

var isNumber = function(s) {
    const str = s.trim();
    const reg = /^[\+\-]?((\d+(\.\d*)?)|(\.\d+))(e[\+\-]?\d+)?$/i;
    return reg.test(str)
};

4 把字符串转换成整数

leetcode剑指offer75题_第5张图片

var strToInt = function(str) {
    //首先判断开头是不是空白字符
    if(str.match(/[^\s]|[^\d]/)){
        const [INT_MIN, INT_MAX] = [Math.pow(-2,31),Math.pow(2,31)-1]
        const s = str.trim();
        const res = s.match(/^[\+\-]?\d+/g);
        if(res > INT_MAX) return INT_MAX;
        if (res < INT_MIN) return  INT_MIN;
        return res;
    } else {
        return 0;
    }
};

5 从尾到头打印链表

leetcode剑指offer75题_第6张图片

var reversePrint = function(head) {
    const arr = []
    while(head?.next) {
        arr.push(head.val);
        head = head.next
    }
    if(head?.val || head?.val===0) arr.push(head.val);
    return arr.reverse()
};

6 反转链表

leetcode剑指offer75题_第7张图片

var reverseList = function(head) {
    let prev = null
    let curr = head
    while(curr) {
        const next  = curr.next
        curr.next = prev
        prev=curr
        curr = next
    }
    return prev
};

7 复杂链表的复制

leetcode剑指offer75题_第8张图片

var copyRandomList = function(head) {
    if(!head) return null
    const map = new Map()
    let prev = head
    //创建一个新的节点
    while(prev){
        map.set(prev, new Node(prev.val))
        prev = prev.next
    }
    map.set(null,null)
    prev = head
    //将各个链表节点连接起来
    while(prev) {
        map.get(prev).next = map.get(prev.next)
        map.get(prev).random = map.get(prev.random)
        prev = prev.next
    }
    return map.get(head)
};

8 删除链表的节点

leetcode剑指offer75题_第9张图片

var deleteNode = function(head, val) {
    if(!head) return null
    let newNode = new ListNode(0,head)
    let prev = newNode
    let curr = head
    while(curr){
        if(curr.val === val) {
            prev.next = curr.next
        } else {
            prev = curr
        }
        curr = curr.next
    }
    return newNode.next
};

9 链表中倒数第k个节点

leetcode剑指offer75题_第10张图片

 let slow = head
    let fast = slow
    while(k--){
        fast = fast.next
    }
    if(!fast) return slow
    if(!fast.next) return slow.next
    while(fast?.next){
        fast = fast.next
        slow = slow.next
    }
    return slow.next

10 合并两个排序的链表

leetcode剑指offer75题_第11张图片

let prev = new ListNode(null)
    let curr = prev
    while(l1 && l2){
        if(l1?.val >= l2?.val){
            prev.next = l2
            l2 = l2?.next
            prev = prev.next
        } else {
            prev.next = l1
            l1 = l1?.next
            prev = prev.next
        }
    }
    if (l1) prev.next = l1
    if (l2) prev.next = l2
    return curr.next

11 两个链表的第一个公共节点

leetcode剑指offer75题_第12张图片

var getIntersectionNode = function(headA, headB) {
    let prevA = headA
    let prevB = headB
    while(prevA !== prevB) {
        prevA = prevA ? prevA.next : headB
        prevB = prevB ? prevB.next : headA
    }
    return prevA
};

12 调整数组顺序使奇数位于偶数前面

leetcode剑指offer75题_第13张图片

var exchange = function(nums) {
    const arr = []
    nums.forEach((num)=>{
        if(num%2 === 1) {
            arr.unshift(num)
        } else {
            arr.push(num)
        }
    })
    return arr
};

13 和为s的两个数字

leetcode剑指offer75题_第14张图片

var twoSum = function(nums, target) {
    let l = 0 
    let r = nums.length-1
    while(l!==r){
        const res = nums[l]+nums[r]
        if(res > target){
            r--
        } else if (res < target) {
            l++
        } else {
            return [nums[l],nums[r]]
        }
    }
};

14 翻转单词顺序

leetcode剑指offer75题_第15张图片

var reverseWords = function(s) {
    const arr = s.trim().split(' ').filter(item=>item)
    return arr.reverse().join(' ')
};

15 用两个栈实现队列

leetcode剑指offer75题_第16张图片

var CQueue = function() {
    this.stackA = []
    this.stackB = []
};

/** 
 * @param {number} value
 * @return {void}
 */
CQueue.prototype.appendTail = function(value) {
    this.stackA.push(value)
};

/**
 * @return {number}
 */
CQueue.prototype.deleteHead = function() {
    if(this.stackB.length){
        return this.stackB.pop()
    } else {
        while(this.stackA.length) {
            this.stackB.push(this.stackA.pop())
        }
        if(!this.stackB.length){
            return -1
        } else {
            return this.stackB.pop()
        }
    }
};

16 包含min函数的栈

leetcode剑指offer75题_第17张图片

var MinStack = function() {
    this.stack = []
};

/** 
 * @param {number} x
 * @return {void}
 */
MinStack.prototype.push = function(x) {
    this.stack.push(x)
};

/**
 * @return {void}
 */
MinStack.prototype.pop = function() {
    this.stack.pop()
};

/**
 * @return {number}
 */
MinStack.prototype.top = function() {
    return this.stack[this.stack.length-1]
};

/**
 * @return {number}
 */
MinStack.prototype.min = function() {
    let min = Infinity
    this.stack.forEach((num)=>{
        if(num<min){
            min = num
        }
    })
    return min
};

17 滑动窗口的最大值

leetcode剑指offer75题_第18张图片

const maxSlidingWindow = (nums,k) => {
	const queue = []
	const res = []
	let len = nums.length
	for(let i = 0; i < len; i++){
		while(queue.length && nums[queue.at(-1)] <= nums[i]){
			queue.pop()
		}
		queue.push(i)
		if(queue.length && queue[0] = i-k) queue.shift()
		if(i>= k-1){
			res.push(nums[queue[0]])
		}
	}
}

18 队列的最大值

leetcode剑指offer75题_第19张图片

var MaxQueue = function() {
    this.stack = []
    this.max = -Infinity
};

/**
 * @return {number}
 */
MaxQueue.prototype.max_value = function() {
    if(this.stack.length) return this.max
    return -1
};

/** 
 * @param {number} value
 * @return {void}
 */
MaxQueue.prototype.push_back = function(value) {
    this.stack.push(value)
    if (value > this.max) this.max = value
};

/**
 * @return {number}
 */
MaxQueue.prototype.pop_front = function() {
    if (this.stack.length){
        const res = this.stack.shift()
        if(res === this.max) {
            this.max = Math.max(...this.stack)
        }
        return res
    }  
    return -1
};

19 顺时针打印矩阵

leetcode剑指offer75题_第20张图片

var spiralOrder = function(matrix) {
    if(!matrix.length) return []
    let row = matrix.length
    let col  = matrix[0].length
    const res = []
    size = row*col
    let t = 0, r = col-1, b = row-1, l =0
    while(res.length!==size) {
        //从左往右
        for(let i = l;i<=r;i++) res.push(matrix[t][i])
        t++
		//从左到右可能便利完的矩阵
		//[[1,2,3],[4,5,6],[7,8,9]]

        //从上往下
        for(let i = t; i<=b;i++) res.push(matrix[i][r])
        r--
        //从上到下可能遍历完的矩阵
		//[[7],[8],[9]]
        //到这里可能就遍历完了
        if(res.length===size) break

        //从左往右
        for(let i = r; i>=l; i--) res.push(matrix[b][i])
        b--;
        //从下往上
        for(let i = b;i>=t; i--) res.push(matrix[i][l])
        l++
    } 
    return res
};

20 栈的压入、弹出序列

leetcode剑指offer75题_第21张图片

var validateStackSequences = function(pushed, popped) {
    let stack = []
    let i = 0
    //模拟入栈出栈
    for(item of pushed) {
        stack.push(item)
        while(stack.length && stack.at(-1) == popped[i]){
            stack.pop()
            i++
        }
    }
    return !stack.length
};

21 数组中的重复数字

leetcode剑指offer75题_第22张图片

var findRepeatNumber = function(nums) {
    let map = new Map()
    let res
    for(num of nums){
        if (map.get(num)){
            res = num
            break
        } 
        map.set(num,1)
    }
    return res
};

22 在排序数组中查找数字I

leetcode剑指offer75题_第23张图片

//法一
var search = function(nums, target) {
    let res = 0
    for(num of nums){
        if(num===target)res++
    }
    return res
};

//法二
var search = function(nums, target) {
    return nums.filter(item => item === target).length
};

23 0~n-1中缺失的数字

leetcode剑指offer75题_第24张图片

//等差数列前n项和
var missingNumber = function(nums) {
    const n = nums.length+1
    let total = Math.floor(n*(n-1)/2)
    let res = nums.reduce((prev,curr) => prev+curr, 0)
    return total - res
};

你可能感兴趣的:(Leetcode,leetcode,算法,职场和发展)