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

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

1. 只出现一次的数字


给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。


说明:

你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?


示例 1:

输入: [2,2,1]

输出: 1


示例 2:

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

输出: 4

出处:

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

代码:

#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    int singleNumber(vector<int> &nums)
    {
        int res = 0;
        for (int i = 0; i < nums.size(); i++)
        {
            res ^= nums[i];
        }
        return res;
    }
};
int main()
{
  Solution s;
  vector<int> nums = {2,2,1};
    cout << s.singleNumber(nums) << endl;
  nums = {4,1,2,1,2};
    cout << s.singleNumber(nums) << endl;
  return 0;
}

输出:

1

4


2. 有效的括号

给定一个只包括 '('')''{''}''['']' 的字符串 s ,判断字符串是否有效。


有效字符串需满足:

   左括号必须用相同类型的右括号闭合。

   左括号必须以正确的顺序闭合。


示例 1:

输入:s = "()"

输出:true


示例 2:

输入:s = "()[]{}"

输出:true


示例 3:

输入:s = "(]"

输出:false


示例 4:

输入:s = "([)]"

输出:false


示例 5:

输入:s = "{[]}"

输出:true


提示:

   1 <= s.length <= 10^4

   s 仅由括号 '()[]{}' 组成

出处:

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

代码:

#include <bits/stdc++.h>
using namespace std;
char ascii_tab[128];
class Solution
{
public:
    bool isValid(string s)
    {
        if (s.size() == 0)
            return true;
        stack<char> st;
        ascii_tab['('] = 11;
        ascii_tab['{'] = 12;
        ascii_tab['['] = 13;
        ascii_tab[')'] = 21;
        ascii_tab['}'] = 22;
        ascii_tab[']'] = 23;
        for (auto c : s)
        {
            char n = ascii_tab[c];
            if (n < 20)
                st.push(n);
            else
            {
                if (st.empty())
                    return false;
                if (n != st.top() + 10)
                    return false;
                st.pop();
            }
        }
        if (st.empty())
            return true;
        return false;
    }
};
int main()
{
  Solution sol;
  string s = "()";
    cout << (sol.isValid(s) ? "true" : "false") << endl;
  s = "()[]{}";
    cout << (sol.isValid(s) ? "true" : "false") << endl;
  s = "(]";
    cout << (sol.isValid(s) ? "true" : "false") << endl;
  s = "([)]";
    cout << (sol.isValid(s) ? "true" : "false") << endl;
  s = "{[]}";
    cout << (sol.isValid(s) ? "true" : "false") << endl;
  return 0;
}

输出:

true

true

false

false

true


3. 递归反序正整数


编写一个递归函数,将任意的正整数按反序输出。例如:

输入:"12345"

输出:"54321"

出处:

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

代码:

#include <iostream>
using namespace std;
void revert(int n)
{
    if ( n>= 0 && n<= 9 )
    {
        cout<<n;
    }
    else
    {
        cout<<n % 10;
        revert(n/10);
    }   
}
int main()
{
    int n = 12345 ;
    revert(n);
}

输出:

54321

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