模拟题
anieoo
这个作者很懒,什么都没留下…
展开
-
498. 对角线遍历
原题链接:498. 对角线遍历solution: 模拟原创 2022-06-14 08:25:53 · 66 阅读 · 0 评论 -
43. 字符串相乘
原题链接:43. 字符串相乘solution: 高精度相乘,模拟小学乘法相乘,最后再进位,如下图所示:class Solution {public: string multiply(string num1, string num2) { if(num1 == "0" || num2 == "0") return "0"; int m = num1.length(),n = num2.length(); vect...原创 2022-05-25 22:28:37 · 110 阅读 · 0 评论 -
6. Z 字形变换
原题链接:6. Z 字形变换solution: 直接模拟,保存Z字形变换数组,在按顺序遍历即可class Solution {public: string convert(string s, int numRows) { if(numRows == 1) return s; int n = s.length(); string res; //定义返回值 vector<string&...原创 2022-05-25 16:46:04 · 61 阅读 · 0 评论 -
225. 用队列实现栈
原题链接:225. 用队列实现栈solution: 用队列模拟栈,由于队列只能在末尾进入,开头出,每次pop,和top复杂度都是O(n)class MyStack {public: queue<int> que; MyStack() { } void push(int x) { que.push(x); } int pop() { int res = 0; ...原创 2022-05-23 12:11:39 · 89 阅读 · 0 评论 -
155. 最小栈
原题链接:155. 最小栈solution: 一个栈存储数据,一个占存储最小值用系统自带stack:class MinStack {public: stack<int> st,minst; MinStack() { } void push(int val) { st.push(val); if(minst.empty() || minst.top() >= val){...原创 2022-04-28 17:15:57 · 255 阅读 · 0 评论 -
824. 山羊拉丁文
原题链接:824. 山羊拉丁文solution:模拟题class Solution {public: string toGoatLatin(string sentence) { int n = sentence.length(); string res = ""; //定义返回值 for(int i = 0,k = 0;i < n;i++){ int j = i; while(j .原创 2022-04-21 09:42:28 · 108 阅读 · 0 评论 -
819. 最常见的单词
原题链接:819. 最常见的单词solution:class Solution {public: string lower(string str) { //将字符串转换为小写 for (int i = 0; i < str.size(); ++i) str[i] = tolower(str[i]); return str; } string mostCommonWord(string paragrap.原创 2022-04-17 10:26:07 · 220 阅读 · 0 评论 -
380. O(1) 时间插入、删除和获取随机元素
原题链接:380. O(1) 时间插入、删除和获取随机元素solution:class RandomizedSet {public: unordered_map<int,int> hash; //存储插入的数和他的下标 vector<int> e; //存储插入的数 RandomizedSet() { } bool insert(int val) { if(hash.count(val)){.原创 2022-04-13 10:00:45 · 101 阅读 · 0 评论 -
762. 二进制表示中质数个计算置位
原题链接:762. 二进制表示中质数个计算置位solution: 模拟class Solution {public: int sumone(int x) //计算x二进制中1的个数 { int cnt = 0; while(x){ int tmp = x & (-x); x-=tmp; cnt++; } re...原创 2022-04-06 10:06:36 · 282 阅读 · 0 评论 -
954. 二倍数对数组
原题链接:954. 二倍数对数组solution:class Solution {public: bool canReorderDoubled(vector<int>& arr) { map<double,int> cnt; //存储 for(auto &x : arr) cnt[x]++; for(auto &[c,d] : cnt){ if(d == 0.原创 2022-04-01 12:07:47 · 105 阅读 · 0 评论 -
728. 自除数
原题链接:728. 自除数solution:class Solution {public: bool check(int x) { int num = x; bool res = true; while(num){ int temp = num%10; if(temp==0 || x%temp!=0){ res=false; ..原创 2022-03-31 10:16:30 · 111 阅读 · 0 评论 -
1385. 两个数组间的距离值
原题链接:1385. 两个数组间的距离值solution: 暴力法class Solution {public: int findTheDistanceValue(vector<int>& arr1, vector<int>& arr2, int d) { int n = arr1.size(), m = arr2.size(); int res = 0; for(int i=0;i<n;.原创 2022-03-30 16:44:23 · 184 阅读 · 0 评论 -
693. 交替位二进制数
原题链接:693. 交替位二进制数solution: 每次判断最后一位与前一位是否相同class Solution {public: bool hasAlternatingBits(int n) { int pre=2; while(n!=0){ int cur = n%2; //最后一位 if(cur==pre) return false; pre = ...原创 2022-03-28 09:53:47 · 123 阅读 · 0 评论 -
2028. 找出缺失的观测数据
原题链接:2028. 找出缺失的观测数据solution:class Solution {public: vector<int> missingRolls(vector<int>& rolls, int mean, int n) { vector<int> res; //定义返回值 int m=rolls.size(); int sum = (m+n)*mean; //求出结.原创 2022-03-27 22:50:13 · 379 阅读 · 0 评论 -
682. 棒球比赛
原题链接:682. 棒球比赛solution:class Solution {public: int calPoints(vector<string>& ops) { vector<int> st; int i=0; for(auto& it : ops){ if(it == "+"){ st.push_back(st[i-2] + st[.原创 2022-03-26 20:13:25 · 204 阅读 · 0 评论 -
232. 用栈实现队列
原题链接:232. 用栈实现队列solution:class MyQueue {private: stack<int> stin; //入队栈 stack<int> stout; //出队栈public: MyQueue() { } void push(int x) { stin.push(x); } int pop() { int res = 0.原创 2022-03-23 11:20:05 · 119 阅读 · 0 评论