上一主题 下一主题
ScriptCat,新一代的脚本管理器脚本站,与全世界分享你的用户脚本油猴脚本开发指南教程目录
返回列表 发新帖
楼主: 李恒道 - 

【当前排名42710】挑战leetcode进入前1w名

[复制链接]
  • TA的每日心情
    无聊
    2025-1-31 20:04
  • 签到天数: 195 天

    [LV.7]常住居民III

    745

    主题

    6452

    回帖

    7151

    积分

    管理员

    非物质文化遗产社会摇传承人

    积分
    7151

    荣誉开发者喜迎中秋油中2周年生态建设者

    发表于 2025-1-27 11:56:43 | 显示全部楼层

    https://leetcode.cn/problems/baseball-game/submissions/595585749/?envType=study-plan-v2&envId=programming-skills
    go写的一团糟QAQ

    func calPoints(operations []string) int {
        listTest := make([]int, 0) // 创建list对象
        insertList := func(num int) {
            listTest = append(listTest, num)
        }
        var total int
        for i := 0; i < len(operations); i++ {
            str := operations[i]
            if str == "+" {
                total += listTest[len(listTest)-1]
                total += listTest[len(listTest)-2]
                insertList(int(listTest[len(listTest)-1] + listTest[len(listTest)-2]))
            } else if str == "D" {
                total += listTest[len(listTest)-1] * 2
                insertList(int(listTest[len(listTest)-1] * 2))
            } else if str == "C" {
                total -= listTest[len(listTest)-1]
                listTest = listTest[:len(listTest)-1]
            } else {
                num, _ := strconv.ParseInt(str, 10, 32)
                total += int(num)
                insertList(int(num))
            }
        }
        return total
    }
    混的人。
    ------------------------------------------
    進撃!永遠の帝国の破壊虎---李恒道

    入驻了爱发电https://afdian.com/a/lihengdao666
    回复
    订阅

    使用道具 举报

  • TA的每日心情
    无聊
    2025-1-31 20:04
  • 签到天数: 195 天

    [LV.7]常住居民III

    745

    主题

    6452

    回帖

    7151

    积分

    管理员

    非物质文化遗产社会摇传承人

    积分
    7151

    荣誉开发者喜迎中秋油中2周年生态建设者

    发表于 2025-1-27 12:52:36 | 显示全部楼层

    https://leetcode.cn/problems/robot-return-to-origin/submissions/595592122/?envType=study-plan-v2&envId=programming-skills
    来个简单题爽一爽

    func judgeCircle(moves string) bool {
        pos := []int{0, 0}
        for i := 0; i < len(moves); i++ {
            char := string(moves[i])
            if char == "R" {
                pos[1]++
            }
            if char == "L" {
                pos[1]--
            }
            if char == "U" {
                pos[0]++
            }
            if char == "D" {
                pos[0]--
            }
        }
        return pos[0] == 0 && pos[1] == 0
    }
    混的人。
    ------------------------------------------
    進撃!永遠の帝国の破壊虎---李恒道

    入驻了爱发电https://afdian.com/a/lihengdao666
    回复

    使用道具 举报

  • TA的每日心情
    无聊
    2025-1-31 20:04
  • 签到天数: 195 天

    [LV.7]常住居民III

    745

    主题

    6452

    回帖

    7151

    积分

    管理员

    非物质文化遗产社会摇传承人

    积分
    7151

    荣誉开发者喜迎中秋油中2周年生态建设者

    发表于 2025-1-27 13:14:40 | 显示全部楼层

    https://leetcode.cn/problems/ipo/submissions/595594522/?envType=study-plan-v2&envId=top-interview-150
    堆排序问题
    错了一个边界一次过
    爽了

    /**
     * @param {number} k
     * @param {number} w
     * @param {number[]} profits
     * @param {number[]} capital
     * @Return {number}
     */
    class Heap {
      constructor(comparator = (a, b) => a - b) {
        this.heap = [];
        this.comparator = comparator;
      }
    
      getParentIndex(index) {
        return Math.floor((index - 1) / 2);
      }
    
      getLeftChildIndex(index) {
        return 2 * index + 1;
      }
    
      getRightChildIndex(index) {
        return 2 * index + 2;
      }
    
      swap(index1, index2) {
        const temp = this.heap[index1];
        this.heap[index1] = this.heap[index2];
        this.heap[index2] = temp;
      }
    
      insert(value) {
        this.heap.push(value);
        this.heapifyUp();
      }
    
      heapifyUp() {
        let index = this.heap.length - 1;
        while (index > 0) {
          const parentIndex = this.getParentIndex(index);
          if (this.comparator(this.heap[index], this.heap[parentIndex]) < 0) {
            this.swap(index, parentIndex);
            index = parentIndex;
          } else {
            break;
          }
        }
      }
    
      extractMin() {
        if (this.heap.length === 0) {
          return null;
        }
        if (this.heap.length === 1) {
          return this.heap.pop();
        }
        const min = this.heap[0];
        this.heap[0] = this.heap.pop();
        this.heapifyDown(0);
        return min;
      }
    
      heapifyDown(index) {
        let smallest = index;
        const leftChildIndex = this.getLeftChildIndex(index);
        const rightChildIndex = this.getRightChildIndex(index);
    
        if (
          leftChildIndex < this.heap.length &&
          this.comparator(this.heap[leftChildIndex], this.heap[smallest]) < 0
        ) {
          smallest = leftChildIndex;
        }
    
        if (
          rightChildIndex < this.heap.length &&
          this.comparator(this.heap[rightChildIndex], this.heap[smallest]) < 0
        ) {
          smallest = rightChildIndex;
        }
    
        if (smallest !== index) {
          this.swap(index, smallest);
          this.heapifyDown(smallest);
        }
      }
    
      peek() {
        return this.heap.length > 0 ? this.heap[0] : null;
      }
    
      size() {
        return this.heap.length;
      }
    }
    var findMaximizedCapital = function (k, w, profits, capital) {
      const capHeap = new Heap((a, b) => a.capital - b.capital);
      const proHeap = new Heap((a, b) => b.profit - a.profit);
      for (let index = 0; index < capital.length; index++) {
        capHeap.insert({
          capital: capital[index],
          profit: profits[index],
        });
      }
      //可以执行的次数
      for (let index = 0; index < k; index++) {
        while (capHeap.peek() !== null && capHeap.peek().capital <= w) {
          const item = capHeap.extractMin();
          proHeap.insert(item);
        }
        w+=proHeap.peek() !== null?proHeap.extractMin().profit:0
      }
      return w
    };
    混的人。
    ------------------------------------------
    進撃!永遠の帝国の破壊虎---李恒道

    入驻了爱发电https://afdian.com/a/lihengdao666
    回复

    使用道具 举报

  • TA的每日心情
    无聊
    2025-1-31 20:04
  • 签到天数: 195 天

    [LV.7]常住居民III

    745

    主题

    6452

    回帖

    7151

    积分

    管理员

    非物质文化遗产社会摇传承人

    积分
    7151

    荣誉开发者喜迎中秋油中2周年生态建设者

    发表于 2025-1-28 01:11:31 | 显示全部楼层

    https://leetcode.cn/submissions/detail/595688125/
    秒了

    function gcd(a, b) {
      return b == 0 ? a : gcd(b, a % b);
    }
    var maxPoints = function (points) {
      let mx = 0;
      for (let index = 0; index < points.length; index++) {
        const point1 = points[index];
        const cache = new Map();
        for (let indey = index + 1; indey < points.length; indey++) {
          const point2 = points[indey];
          const a = point1[0] - point2[0];
          const b = point1[1] - point2[1];
          const k = gcd(a, b);
          const key = `${a / k} ${b / k}`;
          cache.set(key, (cache.get(key) ?? 0) + 1);
          mx = Math.max(mx, cache.get(key));
        }
      }
      return mx+1
    };
    混的人。
    ------------------------------------------
    進撃!永遠の帝国の破壊虎---李恒道

    入驻了爱发电https://afdian.com/a/lihengdao666
    回复

    使用道具 举报

  • TA的每日心情
    无聊
    2025-1-31 20:04
  • 签到天数: 195 天

    [LV.7]常住居民III

    745

    主题

    6452

    回帖

    7151

    积分

    管理员

    非物质文化遗产社会摇传承人

    积分
    7151

    荣誉开发者喜迎中秋油中2周年生态建设者

    发表于 2025-1-28 18:16:42 | 显示全部楼层

    https://leetcode.cn/problems/add-two-numbers-ii/submissions/595756915/?envType=study-plan-v2&envId=programming-skills
    连表问题
    新年快乐!

    func reverseList(node *ListNode) *ListNode {
        var pre *ListNode = nil
        for node != nil {
            next := node.Next
            node.Next = pre
            pre = node
            node = next
        }
        return pre
    }
    func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
        l1 = reverseList(l1)
        l2 = reverseList(l2)
        var ext int
        var result *ListNode = &ListNode{}
        var ret = result
        for l1 != nil && l2 != nil {
            cur := &ListNode{Val: l1.Val + l2.Val + ext}
            if cur.Val >= 10 {
                ext = 1
                cur.Val -= 10
            } else {
                ext = 0
            }
            result.Next = cur
            result = result.Next
            l1 = l1.Next
            l2 = l2.Next
        }
        for l1 != nil {
            cur := &ListNode{Val: l1.Val + ext}
            if cur.Val >= 10 {
                ext = 1
                cur.Val -= 10
            } else {
                ext = 0
            }
            result.Next = cur
            result = result.Next
            l1 = l1.Next
        }
        for l2 != nil {
            cur := &ListNode{Val: l2.Val + ext}
            if cur.Val >= 10 {
                ext = 1
                cur.Val -= 10
            } else {
                ext = 0
            }
            result.Next = cur
            result = result.Next
            l2 = l2.Next
        }
        if ext != 0 {
            result.Next = &ListNode{Val: 1}
        }
        ret = ret.Next
        ret = reverseList(ret)
        return ret
    }
    混的人。
    ------------------------------------------
    進撃!永遠の帝国の破壊虎---李恒道

    入驻了爱发电https://afdian.com/a/lihengdao666
    回复

    使用道具 举报

  • TA的每日心情
    无聊
    2025-1-31 20:04
  • 签到天数: 195 天

    [LV.7]常住居民III

    745

    主题

    6452

    回帖

    7151

    积分

    管理员

    非物质文化遗产社会摇传承人

    积分
    7151

    荣誉开发者喜迎中秋油中2周年生态建设者

    发表于 2025-1-28 18:41:22 | 显示全部楼层

    https://leetcode.cn/problems/check-if-it-is-a-straight-line/submissions/595758430/?envType=study-plan-v2&envId=programming-skills
    gcd计算,收工!

    func checkStraightLine(coordinates [][]int) bool {
        c1 := coordinates[0]
        c2 := coordinates[1]
        calcLine := func(c1 []int, c2 []int) string {
            pos1 := c1[0] - c2[0]
            pos2 := c2[1] - c1[1]
            k := gcd(pos1, pos2)
            pos1 = pos1 / k
            pos2 = pos2 / k
            ret := strconv.Itoa(pos1)
            ret = ret + " "
            ret = ret + strconv.Itoa(pos2)
            return ret
        }
        var cpare=calcLine(c1, c2)
        for i := 1; i < len(coordinates); i++ {
            c1=coordinates[i-1]
            c2=coordinates[i]
            if calcLine(c1, c2) != cpare {
                return  false
            }
        }
        return true
    
    }
    func gcd(a int, b int) int {
        if b == 0 {
            return a
        } else {
            return gcd(b, a%b)
        }
    }
    
    混的人。
    ------------------------------------------
    進撃!永遠の帝国の破壊虎---李恒道

    入驻了爱发电https://afdian.com/a/lihengdao666
    回复

    使用道具 举报

  • TA的每日心情
    无聊
    2025-1-31 20:04
  • 签到天数: 195 天

    [LV.7]常住居民III

    745

    主题

    6452

    回帖

    7151

    积分

    管理员

    非物质文化遗产社会摇传承人

    积分
    7151

    荣誉开发者喜迎中秋油中2周年生态建设者

    发表于 2025-1-28 18:51:55 | 显示全部楼层

    https://leetcode.cn/problems/count-odd-numbers-in-an-interval-range/submissions/595759048/?envType=study-plan-v2&envId=programming-skills
    神经刀了

    func countOdds(low int, high int) int {
        if low%2 == 0 && high%2 == 0 {
            return (high - low) / 2
        }
        if low%2 == 1 && high%2 == 1 {
            return (high-low)/2 + 1
        }
        return (high-low)/2 + 1
    }
    混的人。
    ------------------------------------------
    進撃!永遠の帝国の破壊虎---李恒道

    入驻了爱发电https://afdian.com/a/lihengdao666
    回复

    使用道具 举报

  • TA的每日心情
    无聊
    2025-1-31 20:04
  • 签到天数: 195 天

    [LV.7]常住居民III

    745

    主题

    6452

    回帖

    7151

    积分

    管理员

    非物质文化遗产社会摇传承人

    积分
    7151

    荣誉开发者喜迎中秋油中2周年生态建设者

    发表于 2025-1-28 19:20:13 | 显示全部楼层

    https://leetcode.cn/problems/lemonade-change/submissions/595760643/?envType=study-plan-v2&envId=programming-skills
    神经刀了

    func lemonadeChange(bills []int) bool {
        var five, ten int
        for i := 0; i < len(bills); i++ {
            item := bills[i]
            if item == 5 {
                five++
            } else if item == 10 {
                ten++
                five--
            } else {
                five--
                ten--
            }
            for ten < 0 && five > 0 {
                ten++
                five -= 2
            }
            if five < 0 || ten < 0 {
    
                return false
            }
        }
        return true
    }
    混的人。
    ------------------------------------------
    進撃!永遠の帝国の破壊虎---李恒道

    入驻了爱发电https://afdian.com/a/lihengdao666
    回复

    使用道具 举报

  • TA的每日心情
    无聊
    2025-1-31 20:04
  • 签到天数: 195 天

    [LV.7]常住居民III

    745

    主题

    6452

    回帖

    7151

    积分

    管理员

    非物质文化遗产社会摇传承人

    积分
    7151

    荣誉开发者喜迎中秋油中2周年生态建设者

    发表于 2025-1-28 23:23:20 | 显示全部楼层

    https://leetcode.cn/problems/find-winner-on-a-tic-tac-toe-game/submissions/595775402/?envType=study-plan-v2&envId=programming-skills
    模拟题,秒了

    func tictactoe(moves [][]int) string {
        var grid = [3][3]int{}
        var status = 1
        check := func(x int, y int, checkStatus int) bool {
            var isAll = true
            for i := 0; i < 3; i++ {
                if grid[x][i] != checkStatus {
                    isAll = false
                }
            }
            if isAll {
                return true
            }
            isAll = true
            for i := 0; i < 3; i++ {
                if grid[i][y] != checkStatus {
                    isAll = false
                }
            }
            if isAll {
                return true
            }
            //四个点检查斜,中间检查两边斜
            if (x == 1 && y == 1) || (x == 0 && y == 0) || (x == 2 && y == 2) {
                isAll = true
                for i := 0; i < 3; i++ {
                    if grid[i][i] != checkStatus {
                        isAll = false
                    }
                }
                if isAll {
                    return true
                }
            }
            if (x == 1 && y == 1) || (x == 0 && y == 2) || (x == 2 && y == 0) {
                isAll = true
                for i := 0; i < 3; i++ {
                    if grid[i][2-i] != checkStatus {
                        isAll = false
                    }
                }
                if isAll {
                    return true
                }
            }
            return false
        }
        for i := 0; i < len(moves); i++ {
            move := moves[i]
            grid[move[0]][move[1]] = status
            if check(move[0], move[1], status) == true {
                if status == 1 {
                    return "A"
                } else {
                    return "B"
                }
            }
            if status == 1 {
                status = 2
            } else {
                status = 1
            }
    
        }
        if len(moves) == 9 {
            return "Draw"
        } else {
            return "Pending"
        }
    
    }
    混的人。
    ------------------------------------------
    進撃!永遠の帝国の破壊虎---李恒道

    入驻了爱发电https://afdian.com/a/lihengdao666
    回复

    使用道具 举报

  • TA的每日心情
    无聊
    2025-1-31 20:04
  • 签到天数: 195 天

    [LV.7]常住居民III

    745

    主题

    6452

    回帖

    7151

    积分

    管理员

    非物质文化遗产社会摇传承人

    积分
    7151

    荣誉开发者喜迎中秋油中2周年生态建设者

    发表于 2025-1-28 23:28:02 | 显示全部楼层

    https://leetcode.cn/problems/pOCWxh/submissions/595775680/
    秒了

    func pruneTree(root *TreeNode) *TreeNode {
        var dfs  func(root *TreeNode) int
         dfs=func (node *TreeNode) int{
             if node==nil {
                 return 0
             }
            left:=dfs(node.Left);
            right:=dfs(node.Right);
            if left==0{
                node.Left=nil
            }
            if right==0{
                node.Right=nil
            }
            return node.Val+left+right
        }
        val:=dfs(root)
        if val==0{
            return nil
        }
        return root
    }
    混的人。
    ------------------------------------------
    進撃!永遠の帝国の破壊虎---李恒道

    入驻了爱发电https://afdian.com/a/lihengdao666
    回复

    使用道具 举报

    发表回复

    本版积分规则

    快速回复 返回顶部 返回列表