LeetCode算法刷题记录

LeetCode 回文串系列

剑指 Offer II 018. 有效的回文 剑指 Offer II 086. 分割回文子字符串 剑指 Offer II 020. 回文子字符串的个数 336. 回文对 125. 验证回文串 647. 回文子串 680. 验证回文字符串 Ⅱ 409. 最长回文串 131. 分割回文串 https://leetcode-cn.com/submissions/detail/152376203/ 132. 分割回文串 II 1278. 分割回文串 III 1745. 回文串分割 IV 5. 最长回文子串 1616. 分割两个字符串得到回文串 42. 接雨水 268. 丢失的数字 688. 骑士在棋盘上的概率 1005. K 次取反后最大化的数组和 1380. 矩阵中的幸运数

LeetCode每日一题

(03-25) 892. 三维形体的表面积 class Solution { /** * @param Integer[][] $grid * @return Integer */ function surfaceArea($grid) { $n = count($grid); $area = 0; for ($i = 0; $i < $n; $i++) { for ($j = 0; $j < $n; $j++) { $level = $grid[$i][$j]; if ($level > 0) { //贡献的面积 << 2 相当于 * 4 $area += 2 + ($level << 2); //减去重合的面积 << 1 相当于 * 2 $area -= $i > 0 ? min($level, $grid[$i - 1][$j]) << 1 : 0; //减去重合的面积 $area -= $j > 0 ?

2022年02月LeetCode每日一题

20220216 1719. 重构一棵树的方案数 困难 func checkWays(pairs [][]int) int { adj := map[int]map[int]bool{} for _, p := range pairs { x, y := p[0], p[1] if adj[x] == nil { adj[x] = map[int]bool{} } adj[x][y] = true if adj[y] == nil { adj[y] = map[int]bool{} } adj[y][x] = true } // 检测是否存在根节点 root := -1 for node, neighbours := range adj { if len(neighbours) == len(adj)-1 { root = node break } } if root == -1 { return 0 } ans := 1 for node, neighbours := range adj { if node == root { continue } currDegree := len(neighbours) parent := -1 parentDegree := math.

LeetCode

数组和链表 206. 反转链表 24. 两两交换链表中的节点 141. 环形链表 142. 环形链表 II 24. 两两交换链表中的节点 21. 合并两个有序链表 25.K 个一组翻转链表 86.分隔链表 92.反转链表 II /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* reverseBetween(ListNode* head, int m, int n) { int change_len = n - m + 1; ListNode *pre_head = NULL; ListNode *result = head; while (head && --m) { pre_head = head; head = head->next; } ListNode *modify_list_tail = head; ListNode *new_head = NULL; while (head && change_len--) { ListNode *next = head->next; head->next = new_head; new_head = head; head = next; } //连接为翻转部分 modify_list_tail->next = head; if (pre_head) { pre_head->next = new_head; } else { result = new_head; } return result; } }; /** * Definition for a singly-linked list.