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

本文涉及的产品
公网NAT网关,每月750个小时 15CU
简介: C/C++每日一练(20230407)

1. 选择排序法


使用选择排序法对10个整数进行由大到小排序。


出处:

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


代码:

原题代码四个选项都错了,改正如下:

#include <stdio.h>
int main() {
  int a[10];
  int i,j,temp=0;
  int k,x=0;
  printf("输入10个数:\n");
  for(i=0; i<10; i++)
    scanf("%d",&a[i]);
  for(i=0; i<9;i++) {
    k = i;
    for(j=i+1; j<10; j++)
      if(a[i]<a[j]){
        k = j;
        temp=a[i];
        a[i]=a[k];
        a[k]=temp;
      }
  }
  printf("排序后:\n");
  for(i=0; i<10; i++)
    printf("%d ",a[i]);
  getchar();
  return 0;
}



输出:


2. 多数元素


给定一个大小为 n 的数组,找到其中的多数元素。多数元素是指在数组中出现次数 大于⌊ n/2 ⌋ 的元素。


你可以假设数组是非空的,并且给定的数组总是存在多数元素。


示例 1:

输入:[3,2,3]

输出:3


示例 2:

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

输出:2


进阶:

   尝试设计时间复杂度为 O(n)、空间复杂度为 O(1) 的算法解决此问题。

出处:

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

代码:

#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    int majorityElement(vector<int> &nums)
    {
        unordered_map<int, int> counts;
        int majority = 0, cnt = 0;
        for (int num : nums)
        {
            ++counts[num];
            if (counts[num] > cnt)
            {
                majority = num;
                cnt = counts[num];
            }
        }
        return majority;
    }
};
int main()
{
  Solution s;
  vector<int> nums = {3,2,3};
  cout << s.majorityElement(nums) << endl;
  nums = {2,2,1,1,1,2,2};
  cout << s.majorityElement(nums) << endl;
  return 0;
} 


输出:

3

2


3. 字母异位词分组


给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。


示例:

输入:["eat", "tea", "tan", "ate", "nat", "bat"]

输出:[["ate","eat","tea"],["nat","tan"],["bat"]]


说明:

   所有输入均为小写字母。

   不考虑答案输出的顺序。


以下程序实现了这一功能,请你填补空白处内容:


```c++
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    vector<vector<string>> groupAnagrams(vector<string> &strs)
    {
        vector<vector<string>> res;
        unordered_map<string, vector<string>> ht;
        for (const auto &str : strs)
        {
            int counts[26] = {0};
            for (char c : str)
            {
                counts[c - 'a']++;
            }
            string key;
            for (int i : counts)
            {
                ________________;
            }
            ht[key].push_back(str);
        }
        for (const auto &t : ht)
        {
            res.push_back(t.second);
        }
        return res;
    }
};
```



出处:

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

代码:

#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    vector<vector<string>> groupAnagrams(vector<string> &strs)
    {
        vector<vector<string>> res;
        unordered_map<string, vector<string>> ht;
        for (const auto &str : strs)
        {
            int counts[26] = {0};
            for (char c : str)
            {
                counts[c - 'a']++;
            }
            string key;
            for (int i : counts)
            {
        key.push_back('#');
        key.push_back(i + '0');
            }
            ht[key].push_back(str);
        }
        for (const auto &t : ht)
        {
            res.push_back(t.second);
        }
        return res;
    }
};
string vectorToString(vector<string> vect) {
    stringstream ss;
  ss << "[";
    for (int i = 0; i < vect.size(); i++)
  {
        ss << vect[i];
        if (i < vect.size() - 1)
          ss << ",";
    }
    ss << "]";
    return ss.str();
}
int main()
{
  Solution s;
  vector<string> str = {"eat", "tea", "tan", "ate", "nat", "bat"};
  vector<vector<string>> res = s.groupAnagrams(str);
  cout << "[";
  for (int i = 0; i < res.size(); i++)
  {
    cout << vectorToString(res[i]);
        if (i < res.size() - 1)
          cout << ",";
  }
  cout << "]";
  return 0;
} 




输出:

[[bat],[tan,nat],[eat,tea,ate]]









相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
目录
相关文章
|
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