leetcode热题100(2)

嚴華  论坛元老 | 2025-4-27 00:16:30 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 1852|帖子 1852|积分 5556

leetcode-73-矩阵置零
   法一:两个标记数组分别记录每一行和每一列是否有零出现
  时间复杂度O(mn)   空间复杂度O(m+n)
  1. void setZeroes(int** matrix, int matrixSize, int* matrixColSize) {
  2.     int m = matrixSize;
  3.     int n = matrixColSize[0];
  4.     int row[m], col[n];
  5.     memset(row, 0, sizeof(row));
  6.     memset(col, 0, sizeof(col));
  7.     for (int i = 0; i < m; i++) {
  8.         for (int j = 0; j < n; j++) {
  9.             if (!matrix[i][j]) {
  10.                 row[i] = col[j] = true;
  11.             }
  12.         }
  13.     }
  14.     for (int i = 0; i < m; i++) {
  15.         for (int j = 0; j < n; j++) {
  16.             if (row[i] || col[j]) {
  17.                 matrix[i][j] = 0;
  18.             }
  19.         }
  20.     }
  21. }
复制代码
  法二:使用两个标记量
  可以用矩阵的第一行和第一列取代方法一中的两个标记数组,以达到O(!)的额外空间。但这样会导致原数组的第一行和第一列被修改,无法记录它们是否原本包罗0,因此需要额外使用两个标记量分别记录第一行和第一列是否原本包罗0
  时间复杂度O(mn)    空间复杂度O(1)
  1. void setZeroes(int** matrix, int matrixSize, int* matrixColSize) {
  2.     int m = matrixSize;
  3.     int n = matrixColSize[0];
  4.     int flag_col0 = false, flag_row0 = false;
  5.     for(int i = 0 ; i < m ; i++){
  6.         if(!matrix[i][0])
  7.             flag_col0 = true;
  8.     }
  9.     for(int j = 0 ; j < n ; j++){
  10.         if(!matrix[0][j])
  11.             flag_row0 = true;
  12.     }
  13.     for(int i = 1 ; i < m ; i++){
  14.         for(int j = 1 ; j < n ; j++){
  15.             if(!matrix[i][j])
  16.                 matrix[i][0] = matrix[0][j] = 0;
  17.         }
  18.     }
  19.     for(int i = 1 ; i < m ; i++){
  20.         for(int j = 1 ; j < n ; j++){
  21.             if(!matrix[i][0] || !matrix[0][j])
  22.                 matrix[i][j] = 0;
  23.         }
  24.     }
  25.     if(flag_col0){
  26.         for(int i = 0 ; i < m ;i++){
  27.             matrix[i][0] = 0;
  28.         }
  29.     }
  30.     if(flag_row0){
  31.         for(int j = 0 ; j < n ; j++){
  32.             matrix[0][j] = 0;
  33.         }
  34.     }
  35. }
复制代码

leetcode-48-旋转图像
   转置+按列翻转
  1. void rotate(int** matrix, int matrixSize, int* matrixColSize) {
  2.     int temp;
  3.     int n = matrixSize;
  4.     for(int i = 0 ; i < n ; i++){
  5.         for(int j = i ; j < n ; j++){
  6.             if(i == j) continue;
  7.             temp = matrix[i][j];
  8.             matrix[i][j] = matrix[j][i];
  9.             matrix[j][i] = temp;
  10.         }
  11.     }
  12.     for(int j = 0 ; j < n/2 ; j++){
  13.         for(int i = 0 ; i< n ; i++){
  14.             temp = matrix[i][j];
  15.             matrix[i][j] = matrix[i][n-j-1];
  16.             matrix[i][n-j-1] = temp;
  17.         }
  18.     }
  19. }
复制代码

leetcode-240-搜刮二维矩阵II
   1.二分查找
  由于矩阵每行都是升序的,所以可以依次遍历每行进行二分查找
  时间O(mlogn)  空间O(1)
  1. int binarySearch(int* nums, int size, int target){
  2.     int l = 0, r = size-1;
  3.     while(l <= r){
  4.         int mid = (l+r)/2;
  5.         if(nums[mid] > target){
  6.             r = mid-1;
  7.         }
  8.         else if(nums[mid] < target){
  9.             l = mid+1;
  10.         }
  11.         else{
  12.             return mid;
  13.         }
  14.     }
  15.     return -1;
  16. }
  17. bool searchMatrix(int** matrix, int matrixSize, int* matrixColSize, int target){
  18.     int index;
  19.     for(int i = 0; i < matrixSize ; i++){
  20.         index = binarySearch(matrix[i],matrixColSize[i],target);
  21.         if(index >= 0)
  22.             return true;
  23.     }
  24.     return false;
  25. }
复制代码
  2.Z字形查找
  从矩阵右上角matrix[j]开始探求
  若matrix[j] > target  列--
  若matrix[j] < target  行++
  时间O(m+n)  空间O(1)
  1. bool searchMatrix(int** matrix, int matrixSize, int* matrixColSize, int target) {
  2.     int m = matrixSize, n = matrixColSize[0];
  3.     int i = 0, j = n - 1;
  4.     while (i < m && j >= 0) {
  5.         if (matrix[i][j] == target) {
  6.             return true;
  7.         }
  8.         if (matrix[i][j] < target) {
  9.             i++;
  10.         } else {
  11.             j--;
  12.         }
  13.     }
  14.     return false;
  15. }
复制代码

leetcode-160-相交链表
   1.两个链表相交
  设headA和headB的长度分别是m和n。假设headA不相交部分有a个节点,headB不相交部分有b个节点,两个链表相交部分有c个节点,则 a+c = m, b+c = n
  (1)若a = b,则两个指针会同时到达两个链表相交部分,此时返回相交节点
  (2)若a 不等于 b,则指针pA遍历完headA,然后指向headB,指针pB遍历完headB,然后指向headA,然后两个指针继续移动,在指针pA移动了a+c+b次,指针pB移动了b+c+a次之后,两个指针会同时达到相交节点
  2.两个链表不相交
  设headA和headB的长度分别是m和n。考虑当m等于n 和 m不等于n
  (1)若m 等于 n,则两个指针会同时到达链尾,返回NULL
  (2)若m 不等于 n,由于链表没有公共节点,两个指针不会同时到达链尾,因此两个指针都会遍历完两个链表,在指针pA移动了m+n次,指针pB移动了n+m次后,两个指针都会同时变为NULL
  1. struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
  2.     if(headA == NULL || headB == NULL)
  3.         return NULL;
  4.     struct ListNode *pA = headA, *pB = headB;
  5.     while(pA != pB){
  6.         pA = pA == NULL ? headB : pA->next;
  7.         pB = pB == NULL ? headA : pB->next;
  8.     }
  9.     return pA;
  10. }
复制代码
  2.getLen求不带头结点的链表长度
  1. int getLen(struct ListNode* head){
  2.     int len = 1;
  3.     struct ListNode* p = head;
  4.     while(p != NULL){
  5.         p = p->next;
  6.         len++;
  7.     }
  8.     return len;
  9. }
  10. struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
  11.     int lenA = getLen(headA);
  12.     int lenB = getLen(headB);
  13.     struct ListNode *pA, *pB;
  14.     for(pA = headA ; lenA > lenB ; lenA--){
  15.         pA = pA->next;
  16.     }
  17.     for(pB = headB ; lenA < lenB ; lenB--){
  18.         pB = pB->next;
  19.     }
  20.     while(pA != NULL && pA != pB){
  21.         pA = pA->next;
  22.         pB = pB->next;
  23.     }
  24.     return pA;
  25. }
复制代码

leetcode-234-回文链表
   双指针:
  找到链表的中间结点,将链表的后半部分翻转后,再从链表中间结点开始,与链头两两相比较,若不相同,则返回false
  1. bool isPalindrome(struct ListNode* head) {
  2.     if(head == NULL)
  3.         return true;
  4.     struct ListNode* dummy = (struct ListNode*)malloc(sizeof(struct ListNode));
  5.     dummy->next = head;
  6.     struct ListNode *p = dummy, *q = dummy;
  7.     while(q != NULL && q->next != NULL){
  8.         p = p->next;
  9.         q = q->next->next;
  10.     }
  11.     //将链表从中间断开 q指向后半部分的头部 p指向前半部分尾部
  12.     q = p->next;  
  13.     p->next = NULL;
  14.    
  15.     //将后半部分翻转
  16.     while(q != NULL){
  17.         struct ListNode* tmp = q->next;
  18.         q->next = p->next;
  19.         p->next = q;
  20.         q = tmp;
  21.     }
  22.    
  23.     //前后开始两两比较
  24.     q = p->next;
  25.     p = dummy->next;
  26.     while(q != NULL){
  27.         if(p->val != q->val)
  28.             return false;
  29.         p = p->next;
  30.         q = q->next;
  31.     }
  32.     return true;
  33. }
复制代码

leetcode-21-归并两个有序链表
  1. struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
  2.     if (list1 == NULL || list2 == NULL)
  3.     return list1 != NULL ? list1 : list2;
  4.     struct ListNode* dummy = (struct ListNode*)malloc(sizeof(struct ListNode));
  5.     struct ListNode *tmp, *tmp1;
  6.     struct ListNode *p, *q;
  7.     if(list1->val > list2->val){
  8.         tmp = list1;
  9.         list1 = list2;
  10.         list2 = tmp;
  11.     }
  12.     dummy->next = list1;
  13.     p = dummy;
  14.     q = list2;
  15.     while(p != NULL && q != NULL){
  16.         while(p->next != NULL && p->next->val < q->val){
  17.             p = p->next;
  18.         }
  19.         tmp = p->next;
  20.         p->next =q;
  21.         p = q;
  22.         q = tmp;
  23.     }
  24.     return dummy->next;
  25. }
复制代码
  2.
  1. struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
  2.     if(list1 == NULL || list2 == NULL)
  3.         return list1 != NULL ? list1 : list2;
  4.     struct ListNode* dummy = (struct ListNode*)malloc(sizeof(struct ListNode));
  5.     struct ListNode *pre, *p, *q;
  6.     pre = dummy;
  7.     p = list1;
  8.     q = list2;
  9.     while(p != NULL && q != NULL){
  10.         if(p->val <= q->val){
  11.             pre->next =p;
  12.             pre =pre->next;
  13.             p = p->next;
  14.         }
  15.         else if(p->val > q->val){
  16.             pre->next = q;
  17.             pre = pre->next;
  18.             q = q->next;
  19.         }
  20.         
  21.     }
  22.     if(p != NULL){
  23.         pre->next = p;
  24.     }
  25.     if(q != NULL){
  26.         pre->next = q;
  27.     }
  28.     return dummy->next;
  29. }
复制代码

leetcode-25-k个一组翻转链表
  1. struct ListNode* move(struct ListNode* node, int n){
  2.     while(node != NULL && n > 0){
  3.         node = node->next;
  4.         n--;
  5.     }
  6.     return node;
  7. }
  8. struct ListNode* reverseKGroup(struct ListNode* head, int k) {
  9.     struct ListNode *p, *q, *pre, *tmp, *link;
  10.     bool flag = true;
  11.     struct ListNode* dummy = (struct ListNode*)malloc(sizeof(struct ListNode));
  12.     dummy->next = head;
  13.     p = q = link = dummy->next;
  14.     while(q != NULL){
  15.         q = move(q,k-1);
  16.         if(q == NULL)
  17.             break;
  18.         pre = q->next;
  19.         while(pre != q){
  20.             tmp = p->next;
  21.             p->next = pre;
  22.             pre = p;
  23.             p = tmp;
  24.         }
  25.         if(flag){
  26.             dummy->next = q;
  27.             flag = false;
  28.         }else{
  29.             link->next = q;
  30.         }
  31.         link = q;
  32.         link = move(link,k-1);
  33.         q = p;
  34.     }
  35.     return dummy->next;
  36. }
复制代码
  2.k一组翻转
  3.用栈
  
leetcode-2-两数相加
   考虑以下环境:
  1.list1 比 list2 长  list2相加完后仍有进位
  2.list2 比 list1 长  list1相加完后仍有进位
  3.list1 和 list2 一样长,相加完后仍有进位
  1. struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
  2.     int count;
  3.     struct ListNode *p, *q, *pre;
  4.     struct ListNode *dummy = (struct ListNode*)malloc(sizeof(struct ListNode));
  5.     count = 0;
  6.     p = l1, q = l2, pre = dummy;
  7.     while(p != NULL || q != NULL){
  8.         struct ListNode *node = (struct ListNode*)malloc(sizeof(struct ListNode));
  9.         node->next = NULL;
  10.         int n1 = p != NULL ? p->val : 0;
  11.         int n2 = q != NULL ? q->val : 0;
  12.         node->val = (n1 + n2 + count)%10;
  13.         count = (n1 + n2 + count)/10;
  14.         pre->next = node;
  15.         pre = node;
  16.         if(p != NULL){
  17.             p = p->next;
  18.         }
  19.         if(q != NULL){
  20.             q = q->next;
  21.         }
  22.     }
  23.     if(count != 0){
  24.         struct ListNode *node = (struct ListNode*)malloc(sizeof(struct ListNode));
  25.         node->next = NULL;
  26.         node->val = count;
  27.         pre->next = node;
  28.     }
  29.     return dummy->next;
  30. }
复制代码

leetcode-138-随机链表的复制

leetcode-148-排序链表
    归并法
  1. struct ListNode* middleNode(struct ListNode* head) {
  2.     struct ListNode* pre = head;
  3.     struct ListNode* slow = head;
  4.     struct ListNode* fast = head;
  5.     while (fast && fast->next) {
  6.         pre = slow;
  7.         slow = slow->next;
  8.         fast = fast->next->next;
  9.     }
  10.     pre->next = NULL;
  11.     return slow;
  12. }
  13. struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
  14.     struct ListNode dummy;
  15.     struct ListNode* cur = &dummy;
  16.     while (list1 && list2) {
  17.         if (list1->val < list2->val) {
  18.             cur->next = list1;
  19.             list1 = list1->next;
  20.         } else {
  21.             cur->next = list2;
  22.             list2 = list2->next;
  23.         }
  24.         cur = cur->next;
  25.     }
  26.     cur->next = list1 ? list1 : list2;
  27.     return dummy.next;
  28. }
  29. struct ListNode* sortList(struct ListNode* head) {
  30.     if (head == NULL || head->next == NULL) {
  31.         return head;
  32.     }
  33.     struct ListNode* head2 = middleNode(head);
  34.     head = sortList(head);
  35.     head2 = sortList(head2);
  36.     return mergeTwoLists(head, head2);
  37. }
复制代码

leetcode-23-归并k个升序链表

leetcode-146-LRU缓存

leetcode-543-二叉树的直径
   一条路径的长度为该路径经过的节点数减一,所以求直径(即求路径长度的最大值)等效于求路径经过节点数的最大值减一。
  而恣意一条路径均可以被看作由某个节点为出发点,从其左儿子和右儿子向下遍历的路径拼接得到 
  假设我们知道对于该节点的左儿子向下遍历经过最多的节点数 L (即以左儿子为根的子树的深度) 和其右儿子向下遍历经过最多的节点数 R (即以右儿子为根的子树的深度),那么以该节点为出发点的路径经过节点数的最大值即为 L+R+1 。
  二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。
  

  1. int getDeep(struct TreeNode* root, int* ans){
  2.     if(root == NULL)
  3.         return 0;
  4.     int left = getDeep(root->left,ans);
  5.     int right = getDeep(root->right,ans);
  6.     *ans = fmax(*ans,left+right+1);
  7.     return fmax(left,right)+1;
  8. }
  9. int diameterOfBinaryTree(struct TreeNode* root) {
  10.     int ans = 1;
  11.     getDeep(root,&ans);
  12.     return ans-1;
  13. }
复制代码

leetcode-662-二叉树最大宽度
leetcode-124-二叉树最大路径和
 leetcode-437-路径总和III

leetcode-230-二叉搜刮树第k小元素
  1. int kthSmallest(struct TreeNode* root, int k) {
  2.     struct TreeNode** stk = (struct TreeNode**)malloc(sizeof(struct TreeNode*)*10000);
  3.     int* res = (int*)malloc(sizeof(int)*10000);
  4.     struct TreeNode* node = root;
  5.     int stk_top = 0;
  6.     int index = 0;
  7.     while(node != NULL || stk_top > 0){
  8.         while(node != NULL){
  9.             stk[stk_top++] = node;
  10.             node = node->left;
  11.         }
  12.         node = stk[--stk_top];
  13.         res[index++] = node->val;
  14.         node = node->right;
  15.     }
  16.     return res[k-1];
  17. }
复制代码

leetcode-144-二叉树睁开为链表
  1. void flatten(struct TreeNode* root) {
  2.     struct TreeNode** stk = (struct TreeNode**)malloc(sizeof(struct TreeNode*)*10000);
  3.     struct TreeNode** res = (struct TreeNode**)malloc(sizeof(struct TreeNode*)*10000);
  4.     int stk_top, index;
  5.     stk_top = 0, index = 0;
  6.     struct TreeNode* node = root;
  7.     while(node != NULL || stk_top > 0){
  8.         while(node != NULL){
  9.             stk[stk_top++] = node;
  10.             res[index++] = node;
  11.             node = node->left;
  12.         }
  13.         node = stk[--stk_top];
  14.         node = node->right;
  15.     }
  16.     for(int i = 0 ; i < index ; i++){
  17.         res[i]->left = NULL;
  18.         res[i]->right = NULL;
  19.     }
  20.     for(int i = 0 ; i < index-1 ; i++){
  21.         res[i]->right = res[i+1];
  22.     }
  23. }
复制代码

leetcode-124-二叉树中最大路径和


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

嚴華

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表