李恒道 发表于 2025-1-27 11:56:43

https://leetcode.cn/problems/baseball-game/submissions/595585749/?envType=study-plan-v2&envId=programming-skills
go写的一团糟QAQ
```js
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
                if str == "+" {
                        total += listTest
                        total += listTest
                        insertList(int(listTest + listTest))
                } else if str == "D" {
                        total += listTest * 2
                        insertList(int(listTest * 2))
                } else if str == "C" {
                        total -= listTest
                        listTest = listTest[:len(listTest)-1]
                } else {
                        num, _ := strconv.ParseInt(str, 10, 32)
                        total += int(num)
                        insertList(int(num))
                }
        }
        return total
}
```

李恒道 发表于 2025-1-27 12:52:36

https://leetcode.cn/problems/robot-return-to-origin/submissions/595592122/?envType=study-plan-v2&envId=programming-skills
来个简单题爽一爽
```go
func judgeCircle(moves string) bool {
        pos := []int{0, 0}
        for i := 0; i < len(moves); i++ {
                char := string(moves)
                if char == "R" {
                        pos++
                }
                if char == "L" {
                        pos--
                }
                if char == "U" {
                        pos++
                }
                if char == "D" {
                        pos--
                }
        }
        return pos == 0 && pos == 0
}
```

李恒道 发表于 2025-1-27 13:14:40

https://leetcode.cn/problems/ipo/submissions/595594522/?envType=study-plan-v2&envId=top-interview-150
堆排序问题
错了一个边界一次过
爽了
```js
/**
* @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;
    this.heap = this.heap;
    this.heap = 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, this.heap) < 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;
    this.heap = 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, this.heap) < 0
    ) {
      smallest = leftChildIndex;
    }

    if (
      rightChildIndex < this.heap.length &&
      this.comparator(this.heap, this.heap) < 0
    ) {
      smallest = rightChildIndex;
    }

    if (smallest !== index) {
      this.swap(index, smallest);
      this.heapifyDown(smallest);
    }
}

peek() {
    return this.heap.length > 0 ? this.heap : 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,
      profit: profits,
    });
}
//可以执行的次数
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
};
```

李恒道 发表于 2025-1-28 01:11:31

https://leetcode.cn/submissions/detail/595688125/
秒了
```js
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;
    const cache = new Map();
    for (let indey = index + 1; indey < points.length; indey++) {
      const point2 = points;
      const a = point1 - point2;
      const b = point1 - point2;
      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
};
```

李恒道 发表于 2025-1-28 18:16:42

https://leetcode.cn/problems/add-two-numbers-ii/submissions/595756915/?envType=study-plan-v2&envId=programming-skills
连表问题
新年快乐!
```js
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
}
```

李恒道 发表于 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计算,收工!
```go
func checkStraightLine(coordinates [][]int) bool {
        c1 := coordinates
        c2 := coordinates
        calcLine := func(c1 []int, c2 []int) string {
                pos1 := c1 - c2
                pos2 := c2 - c1
                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
                c2=coordinates
                if calcLine(c1, c2) != cpare {
                        returnfalse
                }
        }
        return true

}
func gcd(a int, b int) int {
        if b == 0 {
                return a
        } else {
                return gcd(b, a%b)
        }
}

```

李恒道 发表于 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
神经刀了
```go
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
}
```

李恒道 发表于 2025-1-28 19:20:13

https://leetcode.cn/problems/lemonade-change/submissions/595760643/?envType=study-plan-v2&envId=programming-skills
神经刀了
```go
func lemonadeChange(bills []int) bool {
        var five, ten int
        for i := 0; i < len(bills); i++ {
                item := bills
                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
}
```

李恒道 发表于 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
模拟题,秒了
```go
func tictactoe(moves [][]int) string {
        var grid = int{}
        var status = 1
        check := func(x int, y int, checkStatus int) bool {
                var isAll = true
                for i := 0; i < 3; i++ {
                        if grid != checkStatus {
                                isAll = false
                        }
                }
                if isAll {
                        return true
                }
                isAll = true
                for i := 0; i < 3; i++ {
                        if grid != 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 != 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 != checkStatus {
                                        isAll = false
                                }
                        }
                        if isAll {
                                return true
                        }
                }
                return false
        }
        for i := 0; i < len(moves); i++ {
                move := moves
                grid]] = status
                if check(move, move, 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"
        }

}
```

李恒道 发表于 2025-1-28 23:28:02

https://leetcode.cn/problems/pOCWxh/submissions/595775680/
秒了
```go
func pruneTree(root *TreeNode) *TreeNode {
        var dfsfunc(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
}
```
页: 36 37 38 39 40 41 42 43 44 45 [46] 47 48 49 50 51 52 53 54
查看完整版本: 【当前排名42710】挑战leetcode进入前1w名