C/C++每日一练(20230327)

简介: C/C++每日一练(20230327)

1. 矩阵中的最长递增路径


给定一个 m x n 整数矩阵 matrix ,找出其中 最长递增路径 的长度。

对于每个单元格,你可以往上,下,左,右四个方向移动。 你 不能对角线 方向上移动或移动到 边界外(即不允许环绕)。


示例 1:

d436ea72ef06c6038b50f729e3e23740.jpeg


输入:matrix = [[9,9,4],[6,6,8],[2,1,1]]

输出:4

解释:最长递增路径为 [1, 2, 6, 9]。

示例 2:

d467675b15c40a7cab7b64937f413672.jpeg


输入:matrix = [[3,4,5],[3,2,6],[2,2,1]]

输出:4


解释:最长递增路径是 [3, 4, 5, 6]。注意不允许在对角线方向上移动。



示例 3:

输入:matrix = [[1]]

输出:1

提示:

   m == matrix.length

   n == matrix[i].length

   1 <= m, n <= 200

   0 <= matrix[i][j] <= 2^31 - 1


出处:

https://edu.csdn.net/practice/23885010

代码:


#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    const int dirs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    int m, n;
    int longestIncreasingPath(vector<vector<int>> &matrix)
    {
        if (matrix.size() == 0 || matrix[0].size() == 0)
        {
            return 0;
        }
        m = matrix.size();
        n = matrix[0].size();
        int res = 0;
        auto memo = vector<vector<int>>(m, vector<int>(n, 0));
        for (int i = 0; i < m; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                if (memo[i][j])
                    res = max(res, memo[i][j]);
                else
                    res = max(res, dfs(i, j, matrix, memo));
            }
        }
        return res;
    }
    int dfs(int i, int j, vector<vector<int>> &matrix, vector<vector<int>> &memo)
    {
        int temp = 1;
        for (int k = 0; k < 4; ++k)
        {
            int x = i + dirs[k][0];
            int y = j + dirs[k][1];
            if ((x >= 0) && (x < m) && (y >= 0) && (y < n) && (matrix[i][j] < matrix[x][y]))
            {
                if (memo[x][y])
                    temp = max(temp, memo[x][y] + 1);
                else
                    temp = max(temp, dfs(x, y, matrix, memo) + 1);
            }
        }
        memo[i][j] = temp;
        return temp;
    }
};
int main()
{
  Solution s;
  vector<vector<int>> matrix = {{9,9,4},{6,6,8},{2,1,1}};
  cout << s.longestIncreasingPath(matrix) << endl;
  matrix = {{3,4,5},{3,2,6},{2,2,1}};
  cout << s.longestIncreasingPath(matrix) << endl;
  return 0;
}


输出:

4

4


2. 删除排序链表中的重复元素


存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除所有重复的元素,使每个元素 只出现一次

返回同样按升序排列的结果链表。

示例 1:

b6eba8ba59536dfb80d2f593a68a3f5f.jpeg


输入:head = [1,1,2]

输出:[1,2]


示例 2:

0141f292aa8d31b92eab3049dc01b485.jpeg


输入:head = [1,1,2,3,3]

输出:[1,2,3]


提示:

   链表中节点数目在范围 [0, 300] 内

   -100 <= Node.val <= 100

   题目数据保证链表已经按升序排列


出处:

https://edu.csdn.net/practice/23885011

代码:

#include <bits/stdc++.h>
using namespace std;
struct ListNode
{
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};
class Solution
{
public:
    ListNode *deleteDuplicates(ListNode *head)
    {
        if (head == nullptr)
        {
            return nullptr;
        }
        ListNode *prev = head;
        ListNode *p = prev->next;
        while (p != nullptr)
        {
            if (p->val != prev->val)
            {
                prev->next = p;
                prev = p;
            }
            p = p->next;
        }
        prev->next = p;
        return head;
    }
};
ListNode* buildNodeList(vector<int> vec) {
    ListNode *head = new ListNode(0);
    ListNode *p = head;
    for (int i = 0; i < vec.size(); i++) {
        ListNode *node = new ListNode(vec[i]);
        p->next = node;
        p = p->next;
    }
    return head->next;
}
string NodeList2String(ListNode *head) {
    if (head==NULL) return "[]";
    ListNode *p = head;
  string res;
  res.append("[");
    while (p != nullptr) {
        res.append(to_string(p->val));
        res.append(",");
        p = p->next;
    }
    res.pop_back();
    res.append("]");
    return res;
}
int main()
{
  Solution s;
  vector<int> nums = {1,1,2};
  struct ListNode *head = buildNodeList(nums);
    cout << NodeList2String(s.deleteDuplicates(head)) << endl;
  nums = {1,1,2,3,3};
  head = buildNodeList(nums);
    cout << NodeList2String(s.deleteDuplicates(head)) << endl;
  return 0;
}




输出:

[1,2]

[1,2,3]

附: 单链表遍历、vectorToString

/*
vector<int> traverseNodeList(ListNode *head) {
    vector<int> res;
    ListNode *p = head;
    while (p != nullptr) {
        res.push_back(p->val);
        p = p->next;
    }
    return res;
}
string vectorToString(vector<int> vec) {
    stringstream ss;
    ss << "[";
    for(int i = 0; i < vec.size(); i++) {
        ss << vec[i];
        if(i != vec.size() - 1) {
            ss << ",";
        }
    }
    ss << "]";
    return ss.str();
}
*/



3. 商品优惠计算器


商品优惠计算器 使用if语句编程实现输入购货金额,输出实际付款金额。购货折扣率如下:

购货金额≤500元 不打折

500元<购货金额≤1000元 9折

1000元<购货金额 8折


出处:

https://edu.csdn.net/practice/23885012


代码:

c++ 略


输出:






目录
相关文章
|
4月前
|
Linux 监控 Ubuntu
Linux 终端操作命令(1)
Linux 终端操作命令(1)
89 1
Linux 终端操作命令(1)
|
4月前
|
算法 Java Go
Rust每日一练(Leetday0018) N皇后II、最大子数组和、螺旋矩阵
Rust每日一练(Leetday0018) N皇后II、最大子数组和、螺旋矩阵
40 1
Rust每日一练(Leetday0018) N皇后II、最大子数组和、螺旋矩阵
|
4月前
|
Linux 监控 Shell
Linux 终端命令之文件浏览(4) head, tail
Linux 终端命令之文件浏览(4) head, tail
46 0
Linux 终端命令之文件浏览(4) head, tail
|
4月前
|
Shell Linux 机器学习/深度学习
Linux 终端操作命令(3)内部命令用法
Linux 终端操作命令(3)内部命令用法
72 0
Linux 终端操作命令(3)内部命令用法
|
4月前
|
Python Linux Ubuntu
Linux系统部署Python语言开发运行环境
Linux系统部署Python语言开发运行环境
197 0
Linux系统部署Python语言开发运行环境
|
4月前
|
Go Unix 开发者
Go语言time库,时间和日期相关的操作方法
Go语言time库,时间和日期相关的操作方法
87 0
Go语言time库,时间和日期相关的操作方法
|
4月前
|
C++ 存储 Serverless
力扣C++|一题多解之数学题专场(2)
力扣C++|一题多解之数学题专场(2)
43 0
力扣C++|一题多解之数学题专场(2)
|
4月前
|
Go 机器学习/深度学习 Rust
Golang每日一练(leetDay0119) 反转字符串I\II Reverse String
Golang每日一练(leetDay0119) 反转字符串I\II Reverse String
76 0
Golang每日一练(leetDay0119) 反转字符串I\II Reverse String
|
4月前
|
Java Go C++
Golang每日一练(leetDay0115) 重新安排行程、递增的三元子序列
Golang每日一练(leetDay0115) 重新安排行程、递增的三元子序列
56 0
Golang每日一练(leetDay0115) 重新安排行程、递增的三元子序列
|
4月前
|
Java Go C++
Golang每日一练(leetDay0111) 摆动排序II\I Wiggle Sort
Golang每日一练(leetDay0111) 摆动排序II\I Wiggle Sort
47 0
Golang每日一练(leetDay0111) 摆动排序II\I Wiggle Sort