[Lc]面试题24反转链表

题目 链表定义: //Definition for singly-linked list. struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; 题解 1. 迭代法(双指针) 实际上是三个指针,一个指向当前节点之前pre,一个指向当前节点cur

[Lc]面试题22链表中倒数第k个节点

题目 题解 快慢指针 时间复杂度$O(n)$ 空间复杂度$O(1)$ class Solution { public: ListNode* getKthFromEnd(ListNode* head, int k) { if(k==0 || !head) return nullptr; ListNode *fast = head, *slow = head; int n = 0; while(fast){ if(n >= k) slow = slow->next; fast = fast->next; n++; } if(n<k) return

[Lc]10正则表达式匹配

题目 题解 1. 普通递归 class Solution { public: bool isMatch(string s, string p) { if(p.empty()) return s.empty();//若p为空,则s为空返回false。否则返回true //若s不为空且当前字符

[Lc]面试题18删除链表的节点

题目 链表定义: //Definition for singly-linked list. struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; 题解 这题简单,两个指针,一个指向cur,一个是cur之前pre。当cur的值为val时,p

[Lc]191位1的个数

题目 题解 1. 位掩码 设置一个位掩码mask,初值为1,每次1向左移动一位,mask与n做与运算之后只有mask中为1的那一位为保留,其余归0。因