目录
[LCR 123. 图书整理 I - 力扣(LeetCode)](#LCR 123. 图书整理 I - 力扣(LeetCode))
[LCR 027. 回文链表 - 力扣(LeetCode)](#LCR 027. 回文链表 - 力扣(LeetCode))
[1614. 括号的最大嵌套深度 - 力扣(LeetCode)](#1614. 括号的最大嵌套深度 - 力扣(LeetCode))
[20. 有效的括号 - 力扣(LeetCode)](#20. 有效的括号 - 力扣(LeetCode))
[知识星球 | 深度连接铁杆粉丝,运营高品质社群,知识变现的工具](#知识星球 | 深度连接铁杆粉丝,运营高品质社群,知识变现的工具)
进制转换
cpp
#include <iostream>
#include <stack>
using namespace std;
#define int long long
int n, x;
void solve()
{
if (n == 0)
{
cout << 0 << endl;
return;
}
if (n < 0)
{
cout << '-';
n = -n;
}
stack<int> stk;
while (n)
{
stk.push(n % x);
n /= x;
}
while (!stk.empty())
{
int t = stk.top();
stk.pop();
if (t >= 10)
{
cout << (char)('A' + t - 10);
}
else
{
cout << t;
}
}
cout << endl;
}
signed main()
{
while (cin >> n >> x)
{
solve();
}
return 0;
}
Bitset
cpp
#include <iostream>
#include <stack>
using namespace std;
#define int long long
int n;
void solve()
{
if (n == 0)
{
cout << 0 << endl;
return;
}
if (n < 0)
{
cout << '-';
n = -n;
}
stack<int> stk;
while (n)
{
stk.push(n % 2);
n /= 2;
}
while (!stk.empty())
{
int t = stk.top();
stk.pop();
if (t >= 10)
{
cout << (char)('A' + t - 10);
}
else
{
cout << t;
}
}
cout << endl;
}
signed main()
{
while (cin >> n )
{
solve();
}
return 0;
}
LCR 123. 图书整理 I - 力扣(LeetCode)
cpp
/**
* Definition for singly-linked list.
* 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:
vector<int> reverseBookList(ListNode* head) {
vector<int> res;
ListNode * t=head;
while(t!=nullptr){
res.push_back(t->val);
t=t->next;
}
for(int i=0;i<res.size()/2;i++){
swap(res[i],res[res.size()-i-1]);
}
return res;
}
};
LCR 027. 回文链表 - 力扣(LeetCode)
cpp
/**
* Definition for singly-linked list.
* 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:
bool isPalindrome(ListNode* head) {
vector<int> res;
ListNode *t=head;
while(t!=nullptr){
res.push_back(t->val);
t=t->next;
}
for(int i=0;i<res.size()/2;i++){
if(res[i]!=res[res.size()-i-1]){
return false;
}
}
return true;
}
};
1614. 括号的最大嵌套深度 - 力扣(LeetCode)
cpp
class Solution {
public:
int maxDepth(string s) {
int res = 0;
int left = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '(') {
res = max(res, ++left);
}
else if (s[i] == ')') {
left--;
}
}
return res;
}
};
20. 有效的括号 - 力扣(LeetCode)
cpp
class Solution
{
public:
bool isValid(string s)
{
unordered_map<char,char>map{
{')', '('}, {']', '['}, {'}', '{'}
};
stack<int> stk;
for (int i = 0;i<s.size();i++){
if(map.count(s[i])){
if(stk.empty()||stk.top()!=map[s[i]]){
return false;
}
stk.pop();
}
else
stk.push(s[i]);
}
return stk.empty();
}
};
知识星球 | 深度连接铁杆粉丝,运营高品质社群,知识变现的工具
