首页 > 编程语言 >代码随想录算法训练营第二天 | Leetcode 209、Leetcode 59、kama 44、kama 58

代码随想录算法训练营第二天 | Leetcode 209、Leetcode 59、kama 44、kama 58

时间:2025-01-12 15:34:29浏览次数:1  
标签:kama area int sum 随想录 ++ vector include Leetcode

Leetcode 209

#include "iostream"
#include "vector"

using namespace std;

int minSubArrayLen(int target, vector<int> &nums) {
    int len = INT32_MAX;
    int sum = 0;
    for (int j = 0, i = 0; j < nums.size(); j++) {
        sum += nums[j];
        while (sum >= target) {
            len = len > j - i ? j - i : len;
            sum -= nums[i];
            i++;
        }
    }
    return len == INT32_MAX ? 0 : len + 1;
}

int main() {
    vector<int> nums = {2, 3, 1, 2, 4, 3};
    int target = 7;
    int ans = minSubArrayLen(target, nums);
    cout << ans << endl;
}

Leetcode 59

#include "iostream"
#include "vector"
#include "math.h"

using namespace std;

vector<vector<int>> generateMatrix(int n) {
    vector<vector<int>> matrix(n, vector<int>(n));
    int num = 1;
    int i = 0, j = 0;
    for (int circle = 1; circle <= ceil(n / 2.0); circle++) {
        while (j <= n - circle) {
            matrix[i][j++] = num++;
        }
        i++, j--;
        while (i <= n - circle) {
            matrix[i++][j] = num++;
        }
        i--, j--;
        while (j >= circle - 1) {
            matrix[i][j--] = num++;
        }
        i--, j++;
        while (i > circle - 1) {
            matrix[i--][j] = num++;
        }
        i++, j++;
    }
    return matrix;
}

int main() {
    int n = 1;
    vector<vector<int>> matrix = generateMatrix(n);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }
}

kama 44

#include "iostream"
#include "math.h"

using namespace std;

int main() {
    int n, m;
    cin >> n >> m;
    int area[n + 1][m + 1];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> area[i][j];
        }
    }
    int horizon = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            horizon += area[i][j];
        }
        area[i][m] = horizon;
    }

    int vertical = 0;
    for (int j = 0; j < m; j++) {
        for (int i = 0; i < n; i++) {
            vertical += area[i][j];
        }
        area[n][j] = vertical;
    }

    int minn = INT32_MAX;
    int sum = area[n][m - 1];
    for (int i = 0; i < n; i++) {
        minn = min(minn, abs(sum - area[i][m] * 2));
    }
    for (int j = 0; j < m; j++) {
        minn = min(minn, abs(sum - area[n][j] * 2));
    }

//    for (int i = 0; i < n + 1; i++) {
//        for (int j = 0; j < m + 1; j++) {
//            cout << area[i][j] << " ";
//        }
//        cout << endl;
//    }
    cout << minn << endl;
}

kama 58

#include "iostream"

using namespace std;

int main() {
    int n;
    cin >> n;
    int num[n];
    int sum = 0;
    for (int i = 0; i < n; i++) {
        int a;
        cin >> a;
        sum += a;
        num[i] = sum;
    }
    int a, b;
    while (cin >> a >> b) {
        if (a == 0) {
            cout << num[b] << endl;
        } else {
            cout << num[b] - num[a - 1] << endl;
        }
    }
}

标签:kama,area,int,sum,随想录,++,vector,include,Leetcode
From: https://www.cnblogs.com/sz-maomao/p/18666983

相关文章

  • leetcode2585 获得分数的方法数
    考试中有n种类型的题目,给定整数target和二维数组types,其中types[i]=[count[i],marks[i]],表示第i种类型的题目有count[i]道,每道分值为marks[i]。求在考试中恰好得到target分的方法数,答案对1E9+7取模。注意,同类型题目无法区分。1<=target<=1000;1<=n<=50;1<=count[i],marks[i]<=5......
  • 数据结构与算法之二叉树: LeetCode 108. 将有序数组转换为二叉搜索树 (Ts版)
    将有序数组转换为二叉搜索树https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree/description/描述给你一个整数数组nums,其中元素已经按升序排列请你将其转换为一棵平衡二叉搜索树示例1输入:nums=[-10,-3,0,5,9]输出:[0,-3,9,-10,nul......
  • 数据结构与算法之二叉树: LeetCode 110. 平衡二叉树 (Ts版)
    平衡二叉树https://leetcode.cn/problems/balanced-binary-tree/description/描述给定一个二叉树,判断它是否是平衡二叉树示例1输入:root=[3,9,20,null,null,15,7]输出:true示例2输入:root=[1,2,2,3,3,null,null,4,4]输出:false示例3输入:root=[]输......
  • 数据结构与算法之二叉树: LeetCode 117. 填充每个节点的下一个右侧节点指针 II (Ts版)
    填充每个节点的下一个右侧节点指针IIhttps://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii/description/描述给定一个二叉树:structNode{intval;Node*left;Node*right;Node*next;}填充它的每个next指针,让这个指针指向其......
  • LeetCode热题100中 35. 46. 70. 73. 118.
    35.搜索插入位置题目描述:实现思路:        这里主要就是二分查找,二分查找要注意对边界值的处理,l是数组的第一位,r是数组的最后一位,l<=r我们就返回l,因为我们的判断是nums[mid]<target 取的是mid的左区间已经不包含mid了,所以是 l=mid+1。代码:var......
  • 【LeetCode: 240. 搜索二维矩阵 II + 指针 + 遍历】
    ......
  • LeetCode题练习与总结:复数乘法--537
    一、题目描述复数 可以用字符串表示,遵循 "实部+虚部i" 的形式,并满足下述条件:实部 是一个整数,取值范围是 [-100,100]虚部 也是一个整数,取值范围是 [-100,100]i^2==-1给你两个字符串表示的复数 num1 和 num2 ,请你遵循复数表示形式,返回表示它们乘积的字符串。......
  • 代码随想录训练营第四十五天| 115.不同的子序列 583. 两个字符串的删除操作 72. 编辑
    115.不同的子序列题目链接:115.不同的子序列-力扣(LeetCode)讲解链接:代码随想录 hard确实不好直接说出来粘一下思路:(引自代码随想录)确定dp数组(dptable)以及下标的含义dp[i][j]:以i-1为结尾的s子序列中出现以j-1为结尾的t的个数为dp[i][j]。为什么i-1,j-1这么定义卡哥......
  • LeetCode Top Interview 150 - Matrix
    ThisismerelymypersonalreviewofallthetypicalproblemsthatconstitutethemindsetforDataStructuresandAlgorithms(DSA).pythonsolutionprovidedFortheremainingtypesofproblems,pleaserefertomychannel.everecursion-CSDN博客everecursion......
  • LeetCode:76.最小覆盖子串
    LeetCode:76.最小覆盖子串+helperdivdsxcpv+lean+edjuxdsksforgetAnalyticstomyself解题思路先找出所有的包含T的子串。找出长度最小那个子串,返回即可。用双指针维护一个滑动窗口。移动右指针,找到包含T的子串,移动左指针,尽量减少包含T的子串的长度。循环上述过程,找......