算法题C++
qq_42681040
这个作者很懒,什么都没留下…
展开
-
Leetcode链表练习总结
Leetcode链表练习总结原创 2022-06-15 02:27:17 · 116 阅读 · 0 评论 -
Leetcode数组练习总结
leetcode数组练习原创 2022-06-11 23:08:34 · 120 阅读 · 0 评论 -
字符全排列(字典序)
给定一个字符序列,按照字典序,依次输出所有的全排列#include <iostream>#include <vector>#include <map>using namespace std;vector<char> v; // origin char listmap<char, int> m; // record charvector<vector<char>> result; // .原创 2022-02-15 10:51:34 · 376 阅读 · 0 评论 -
The Castle题解
链接#include <iostream>#include <cstring>using namespace std;const int MAXN = 60;const int MAXM = 60; // 最大的房间数量int N, M;int map[MAXN][MAXM]; // 记录输入数据bool visit[MAXN][MAXM]; // 辅助数组,记录是否访问过,dfs要用到// x, y表示房间的横纵坐标,返回相连的房间数量int df原创 2021-10-10 21:56:54 · 170 阅读 · 0 评论 -
螺旋矩阵题解
链接class Solution { vector<int> v;public: vector<int> spiralOrder(vector<vector<int>>& matrix) { if (matrix.size() == 0) return v; spiralOrder(matrix, 0, matrix.size() - 1, 0, matrix[0].size() - 1);原创 2021-09-28 22:36:00 · 50 阅读 · 0 评论 -
相同的树题解
链接/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}原创 2021-09-28 21:59:00 · 80 阅读 · 0 评论 -
最后一个单词的长度题解
链接class Solution {public: int lengthOfLastWord(string s) { int len = 0; int j = s.size() - 1; while (s[j] == ' ') --j; for (; j >= 0; j--) { if (s[j] == ' ') { break; } else {原创 2021-09-28 21:50:12 · 43 阅读 · 0 评论 -
删除排序链表中的重复元素题解
链接/** * 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(ne原创 2021-09-28 21:39:36 · 46 阅读 · 0 评论 -
链表排序题解
链接/** * 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(ne原创 2021-09-25 16:17:27 · 190 阅读 · 0 评论 -
最长不含重复字符的子字符串
链接class Solution {public: int lengthOfLongestSubstring(string s) { int i = 0; // 指标,指示最长子串的起始位置 int result = 0; // 存放结果 unordered_map<char, int> umap; // 记录字符、下标的hash表 for (int j = 0; j < s.size(); j++) {原创 2021-09-15 23:18:39 · 57 阅读 · 0 评论 -
输出规律图形例题(叠框)
#include<iostream>using namespace std;int main() { int k; char center; char outer; int i_index, j_index; scanf("%d %c %c", &k, ¢er, &outer); // 输入一个奇整数,两个字符 int mode = ((k - 1) / 2) % 2; // 判断先输出center还是ou原创 2021-09-06 15:50:23 · 185 阅读 · 0 评论 -
判断两个日期之间间隔天数
#include<iostream>using namespace std;// 设置一个自己的日期类typedef struct MyDate { int year; int month; int day;}MyDate;// 存放每月的天数(闰年需要判断后在2月+1)int month_day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};// 根据一个输入的整数,转化为自己的日期类原创 2021-09-05 20:52:03 · 340 阅读 · 0 评论 -
根据前序和后序遍历构造二叉树题解
链接/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}原创 2021-08-06 11:25:31 · 56 阅读 · 0 评论 -
找出顶峰元素题解
链接class Solution {public: vector<int> findPeakGrid(vector<vector<int>>& mat) { int rowh = mat.size() - 1; int rowl = 0; int rowm = -1; int maxValueIndex = -1; vector<int> ret{-1,-1};原创 2021-08-06 10:56:33 · 158 阅读 · 0 评论 -
前序中序遍历序列构造二叉树
链接/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}原创 2021-08-05 17:28:02 · 87 阅读 · 0 评论 -
颠倒二进制位题解
链接class Solution {public: uint32_t reverseBits(uint32_t n) { uint32_t ret = 0; for (int i = 0; i < 32; i++) { if (n & (1 << i)) { flipBit(ret, 31-i); } } return ret;原创 2021-08-05 15:32:17 · 45 阅读 · 0 评论 -
**所有可能的满二叉树题解
链接/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}原创 2021-08-05 11:38:06 · 54 阅读 · 0 评论 -
**字符串解码题解
链接class Solution {public: string decodeString(string s) { int num = 0; string ret = ""; stack<string> str; stack<int> n; int len = s.size(); for (int i = 0; i < len; ++i) { if原创 2021-07-10 23:19:33 · 108 阅读 · 0 评论 -
重排链表题解
链接/** * 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(ne原创 2021-07-10 19:48:28 · 121 阅读 · 0 评论 -
反转字符串题解
链接class Solution {public: void reverseString(vector<char>& s) { char tmp; for (int i = 0; i < s.size()/2; ++i) { tmp = s[i]; s[i] = s[s.size()-1-i]; s[s.size()-1-i] = tmp; }原创 2021-07-10 18:40:51 · 65 阅读 · 0 评论 -
3的幂题解
链接class Solution {public: bool isPowerOfThree(int n) { if (n == 0) return false; if (n == 1) return true; if (n % 3 != 0) return false; else return isPowerOfThree(n/3); }};思路先判断0和1,若大于1,则通过是否整除3进行判断;要点注意在n原创 2021-07-10 18:08:03 · 50 阅读 · 0 评论 -
反转链表题解
链接/** * 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(ne原创 2021-07-10 18:01:08 · 72 阅读 · 0 评论 -
移除链表元素题解
链接/** * 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(ne原创 2021-07-10 17:44:05 · 71 阅读 · 0 评论 -
合并两个有序链表题解
链接/** * 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(ne原创 2021-07-10 17:31:46 · 37 阅读 · 0 评论 -
有效的数独题解
链接class Solution {public: bool isValidSudoku(vector<vector<char>>& board) { int v_tmp[11] = {0}; int index,r,c; for (int i = 0; i < 9; i++) { memset(v_tmp, 0, sizeof(v_tmp)); for (int原创 2021-07-05 22:17:02 · 35 阅读 · 0 评论 -
下一个排列题解
链接class Solution {public: void nextPermutation(vector<int>& nums) { int index = -1; int tmp; for (int i = nums.size()-1; i > 0; --i) { if (nums[i-1] < nums[i]) { index = i-1;原创 2021-06-15 23:23:48 · 38 阅读 · 0 评论 -
盛最多水的容器题解
链接class Solution {public: int maxArea(vector<int>& height) { int i = 0; int j = height.size() - 1; int max_v = 0; int tmp_v = 0; while (i < j) { tmp_v = min(height[i], height[j]) * (j -原创 2021-06-10 22:34:52 · 56 阅读 · 0 评论 -
第三大的数题解
链接class Solution {public: int thirdMax(vector<int>& nums) { int index = 1; int i = 0; int pre; sort(nums.begin(), nums.end()); for (i = nums.size() - 1; i >= 0; i--) { if (i == nums.siz原创 2021-06-10 16:25:49 · 58 阅读 · 0 评论 -
种花问题题解
链接class Solution {public: bool canPlaceFlowers(vector<int>& flowerbed, int n) { int count = 0; for (int i = 0; i < flowerbed.size();) { if (flowerbed[i] == 1) { if (i > 0) flowerbed[i-1] = 1原创 2021-06-10 16:15:16 · 126 阅读 · 0 评论 -
二进制矩阵中的特殊位置题解
链接class Solution {public: int numSpecial(vector<vector<int>>& mat) { int row = mat.size(); int col = mat[0].size(); int c_index = -1; int r_nums = 0; int count = 0; for (int i = 0; i <原创 2021-06-05 22:41:11 · 49 阅读 · 0 评论 -
重塑矩阵题解
链接class Solution {public: vector<vector<int>> matrixReshape(vector<vector<int>>& mat, int r, int c) { if (mat.size() * mat[0].size() != r * c) { return mat; } vector<vector<int>原创 2021-06-05 21:54:11 · 119 阅读 · 0 评论 -
复写零题解
链接class Solution {public: void duplicateZeros(vector<int>& arr) { vector<int> temp(arr.size()); temp[0] = 0; for (int i = 0; i < arr.size() - 1; i++) { if (arr[i] == 0) {原创 2021-06-04 23:13:06 · 53 阅读 · 0 评论 -
多数元素题解
链接class Solution {public: int majorityElement(vector<int>& nums) { int mid = nums.size() / 2; sort(nums.begin(), nums.end()); return nums[mid]; }};思路第一种:使用map数据结构,遍历数组,map中有则计数加一;在map中找到最大值;第二种:先对数组进行排序,按照定原创 2021-06-04 22:38:43 · 38 阅读 · 0 评论 -
买卖股票的最佳时机题解
链接class Solution {public: int maxProfit(vector<int>& prices) { int lowest_price = prices[0]; int profit = 0; for (int i = 0; i < prices.size(); i++) { if (profit < prices[i] - lowest_price) {原创 2021-06-03 23:25:47 · 62 阅读 · 0 评论 -
最大子序和题解
链接class Solution {public: int maxSubArray(vector<int>& nums) { int max = nums[0]; int tmpSum = 0; int index = 0; while (max < 0) { while (index < nums.size()) { if (nums[inde原创 2021-06-03 22:52:43 · 62 阅读 · 0 评论 -
删除有序数组中的重复项题解
链接class Solution {public: int removeDuplicates(vector<int>& nums) { if (nums.size() < 2) { return nums.size(); } int i = 0; int j = 1; while (j < nums.size()) { if (nums[原创 2021-06-03 00:06:24 · 62 阅读 · 0 评论 -
两数之和题解
链接class Solution {public: vector<int> twoSum(vector<int>& nums, int target) { vector<int> ret(2); for (int i = 0; i < nums.size(); i++) { for (int j = i + 1; j < nums.size(); j++) {原创 2021-06-02 23:51:55 · 121 阅读 · 0 评论