首页 > 其他分享 >LeetCode刷题记录——day10

LeetCode刷题记录——day10

时间:2024-04-08 09:22:25浏览次数:17  
标签:cn matrix temp int spring day10 size LeetCode 刷题

1、https://leetcode.cn/problems/rotate-image/description/?envType=study-plan-v2&envId=2024-spring-sprint-100

class Solution {
public:
    void rotate(vector<vector<int>>& matrix) {
        int n = matrix.size();
        for (int i = 0; i < n / 2; ++i) {
            for (int j = 0; j < (n + 1) / 2; ++j) {
                int temp = matrix[i][j];
                matrix[i][j] = matrix[n - j - 1][i];
                matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1];
                matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1];
                matrix[j][n - i - 1] = temp;
            }
        }
    }
};

2、https://leetcode.cn/problems/set-matrix-zeroes/?envType=study-plan-v2&envId=2024-spring-sprint-100

class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        int m=matrix.size(),n=matrix[0].size();
        int temp[m][n];
        memset(temp,-1,sizeof(temp));
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(matrix[i][j]==0){
                    temp[i][j]=1;
                }
            }
        }
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(temp[i][j]==1){
                    for(int k=0;k<m;k++)
                        matrix[k][j]=0;
                    for(int k=0;k<n;k++)
                        matrix[i][k]=0;
                }
            }
        }
    }
};

标签:cn,matrix,temp,int,spring,day10,size,LeetCode,刷题
From: https://www.cnblogs.com/humanplug/p/18120387

相关文章

  • 幽默刷题DAY1
    版权声明:本文为博主原创文章,遵循CC4.0BY-SA版权协议,转载请附上原文出处链接和本声明。原文链接:https://blog.csdn.net/weixin_43840280/article/details/119447204本文为《剑指offer》刷题笔记的总结,花费不到两个月的时间将力扣上《剑指offer》的75道题刷了一遍,遇到不会的知......
  • LeetCode题练习与总结:插入区间--57
    一、题目描述示例 1:输入:intervals=[[1,3],[6,9]],newInterval=[2,5]输出:[[1,5],[6,9]]示例2:输入:intervals=[[1,2],[3,5],[6,7],[8,10],[12,16]],newInterval=[4,8]输出:[[1,2],[3,10],[12,16]]解释:这是因为新的区间[4,8]与[3,5],[6,7],[8,10] 重叠。......
  • LeetCode题练习与总结:最后一个单词的长度--58
    一、题目描述给你一个字符串s,由若干单词组成,单词前后用一些空格字符隔开。返回字符串中最后一个单词的长度。单词是指仅由字母组成、不包含任何空格字符的最大子字符串。示例1:输入:s="HelloWorld"输出:5解释:最后一个单词是“World”,长度为5。示例2:输入:s="......
  • Leetcode 866. 回文质数
    https://leetcode.cn/problems/prime-palindrome/description/给你一个整数n,返回大于或等于n的最小回文质数。一个整数如果恰好有两个除数:1和它本身,那么它是质数。注意,1不是质数。例如,2、3、5、7、11和13都是质数。一个整数如果从左向右读和从右向左读是相同的,那......
  • 从数组创建二叉树-Leetcode测试用
    Leetcode里和二叉树相关的题目,都是用一个数组表示二叉树的,而这个数组是按照层次优先顺序给出的,连其中的空结点也表示了出来,刚好就是2^N-1个结点,N表示层数。但数组毕竟无法和二叉树一样具有链式结构,无法进行算法测试,因此尝试直接通过这样的数组构建二叉树。通过数组创建这样的二......
  • 【Web】纯萌新的CISCN刷题记录(1)
    目录[CISCN2019华东南]Web11[CISCN2019华北Day2]Web1[CISCN2019初赛]LoveMath[CISCN2022初赛]ezpop[CISCN2019华东南]DoubleSecret[CISCN2023华北]ez_date[CISCN2019华北Day1]Web1[CISCN2019华东南]Web4[CISCN2019华北Day1]Web2 [CISCN2023西南]do_y......
  • LeetCode 面试经典150题---002
    ###169.多数元素给定一个大小为n的数组nums,返回其中的多数元素。多数元素是指在数组中出现次数大于⌊n/2⌋的元素。你可以假设数组是非空的,并且给定的数组总是存在多数元素。n==nums.length1<=n<=5*104-109<=nums[i]<=109这题可太有意思了,意思就是找......
  • LeetCode 2468. Split Message Based on Limit
    原题链接在这里:https://leetcode.com/problems/split-message-based-on-limit/description/题目:Youaregivenastring, message,andapositiveinteger, limit.Youmust split message intooneormore parts basedon limit.Eachresultingpartshouldhaveth......
  • LeetCode 1439. Find the Kth Smallest Sum of a Matrix With Sorted Rows
    原题链接在这里:https://leetcode.com/problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows/description/题目:Youaregivenan mxn matrix mat thathasitsrowssortedinnon-decreasingorderandaninteger k.Youareallowedtochoose exactlyo......
  • LeetCode 1891. Cutting Ribbons
    原题链接在这里:https://leetcode.com/problems/cutting-ribbons/description/题目:Youaregivenanintegerarray ribbons,where ribbons[i] representsthelengthofthe ith ribbon,andaninteger k.Youmaycutanyoftheribbonsintoanynumberofsegments......