leetcode和牛客刷题
哈哈哈哈哈haha
越努力,越幸运!
展开
-
【算法题】N 字形变换(桶排序法、C++实现、力扣第6题)
【算法题】N 字形变换(桶排序法、C++实现、力扣第6题)原创 2023-02-12 11:37:41 · 331 阅读 · 1 评论 -
【算法题】反转链表(头插法、C++实现、力扣第206题、剑指offer第24题)
【算法题】反转链表(头插法、C++实现、力扣第206题、剑指offer第24题)原创 2022-08-06 18:13:17 · 287 阅读 · 0 评论 -
leetcode 剑指 Offer 67. 把字符串转换成整数
class Solution {public: int strToInt(string str) { if(count(str.begin(), str.end(), ' ') == str.size()) return 0; int res; stringstream ss; ss << str; ss >> res; return res; }};原创 2020-09-02 09:25:28 · 129 阅读 · 0 评论 -
求和表达式
求和表达式sum=x+xx+xxx+xxxx+…最后一项为n个x,请给出程序算法步骤及函数实现int sum_nx(int x, int n)其中x的参数范围为0~9,假设不考虑n个x及sum在int范围溢出情况。#include <iostream>using namespace std;int sum_nx(int x, int n){ long int sn = 0; long int tn = 0; int count = 1; while (count <= n)原创 2020-09-01 16:31:22 · 2680 阅读 · 0 评论 -
leetcode61. 旋转链表
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* rotateRight(ListNode* head, int k) { if(head == n.原创 2020-08-21 13:29:41 · 132 阅读 · 0 评论 -
leetcode 20. 有效的括号
class Solution {public: bool isValid(string s) { stack <char> t; for(auto n : s) { if (n == '(') { t.push(')'); }else if (n == '{') { t.push.原创 2020-08-14 12:18:26 · 127 阅读 · 0 评论 -
leetcode 944. 删列造序
class Solution {public: int minDeletionSize(vector<string>& A) { int ans = 0; int n = A.size(), m = A[0].size(); for(int i = 0; i < m; i++){ for(int j = 0; j < n - 1; j++){ if(A[j][i.原创 2020-08-01 09:42:45 · 126 阅读 · 0 评论 -
leetcode 面试题 08.07. 无重复字符串的排列组合
class Solution {public: vector<string> permutation(string S) { vector<string> res; sort(S.begin(), S.end()); do { res.push_back(S); }while(next_permutation(S.begin(), S.end())); .原创 2020-07-31 21:35:17 · 599 阅读 · 0 评论 -
leetcode 92. 反转链表 II
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* reverseBetween(ListNode* head, int m, int n) { if.原创 2020-07-31 21:27:58 · 105 阅读 · 0 评论 -
leetcode 剑指 Offer 25. 合并两个排序的链表
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: vector<int> list(ListNode* L) { vector<int>.原创 2020-07-31 20:54:21 · 114 阅读 · 0 评论 -
leetcode 剑指 Offer 06. 从尾到头打印链表
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: vector<int> reversePrint(ListNode* head) { if(!head).原创 2020-07-31 20:42:21 · 166 阅读 · 0 评论 -
leetcode 剑指 Offer 38. 字符串的排列
class Solution {public: vector<string> permutation(string s) { vector<string> res; sort(s.begin(),s.end()); do{ res.push_back(s); }while(next_permutation(s.begin(),s.end())); return res;.原创 2020-07-31 20:38:16 · 124 阅读 · 0 评论 -
leetcode 47. 全排列 II
class Solution {public: vector<vector<int>> permuteUnique(vector<int>& nums) { sort(nums.begin(), nums.end()); vector<vector<int>> res; do { res.push_back(nums); }w.原创 2020-07-31 20:37:30 · 146 阅读 · 0 评论 -
leetcode 46. 全排列
class Solution {public: vector<vector<int>> permute(vector<int>& nums) { sort(nums.begin(), nums.end()); vector<vector<int>> res; do { res.push_back(nums); }while(n.原创 2020-07-31 20:36:43 · 131 阅读 · 0 评论 -
leetcode 24. 两两交换链表中的节点
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* swapPairs(ListNode* head) { if(head == nullptr) .原创 2020-07-31 19:47:54 · 122 阅读 · 0 评论 -
leetcode 25. K 个一组翻转链表
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* reverseKGroup(ListNode* head, int k) { if(head ==.原创 2020-07-31 19:33:36 · 100 阅读 · 0 评论 -
leecode 8. 字符串转换整数 (atoi)
class Solution {public: int myAtoi(string str) { int i = 0, flag = 1; long res = 0; //默认flag = 1,正数 while (str[i] == ' ') i ++; //若str全为空格,str[i] = '\0'(最后一个i) if (str[i] == '-') flag = -1; if (str[i] == '-' ||.原创 2020-07-31 16:12:44 · 132 阅读 · 0 评论 -
leetcode 1108. IP 地址无效化
class Solution {public: string defangIPaddr(string address) { string res; for(int i = 0; i < address.size(); i++) { if(address[i] == '.') { res += "[.]"; } els.原创 2020-07-30 21:32:04 · 124 阅读 · 0 评论 -
leetcode 693. 交替位二进制数
class Solution {public: bool hasAlternatingBits(int n) { vector<int> tmp; while(n) { tmp.push_back(n % 2); n /= 2; } for(int i = 0; i < tmp.size() - 1; i++) { .原创 2020-07-30 21:16:29 · 123 阅读 · 0 评论 -
leetcode 230. 二叉搜索树中第K小的元素
/** * 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) {} *.原创 2020-07-30 21:09:38 · 135 阅读 · 0 评论 -
leetcode 461. 汉明距离
class Solution {public: int hammingDistance(int x, int y) { vector<int> vecx; vector<int> vecy; while(x) { vecx.push_back(x % 2); x /= 2; } while(y) { .原创 2020-07-30 20:36:44 · 192 阅读 · 0 评论 -
leetcode 451. 根据字符出现频率排序
class Solution {public: string frequencySort(string s) { set<char> tmp; for(int i = 0; i < s.size(); i++) { tmp.insert(s[i]); } multimap<int, char> m; for(auto it = tmp.begin(.原创 2020-07-30 20:21:55 · 124 阅读 · 0 评论 -
二叉树的先序、中序、后序递归与非递归实现遍历
参考链接:https://www.cnblogs.com/houjun/p/10063468.html转载 2020-07-30 16:40:47 · 88 阅读 · 0 评论 -
leetcode 面试题 02.06. 回文链表
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: bool isPalindrome(ListNode* head) { vector<int> vec;.原创 2020-07-30 13:11:44 · 140 阅读 · 0 评论 -
leetcode 面试题 16.16. 部分排序
class Solution {public: vector<int> subSort(vector<int>& array) { if(!array.size()) { return {-1, -1}; } vector<int> res; vector<int> temp = array; sort(temp.begi.原创 2020-07-30 12:44:21 · 267 阅读 · 0 评论 -
leetcode 100. 相同的树
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: bool isSameTree(TreeNode* p, T.原创 2020-07-30 10:49:27 · 127 阅读 · 0 评论 -
leetcode 剑指 Offer 54. 二叉搜索树的第k大节点
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: vector<int> res; vec.原创 2020-07-30 09:52:09 · 201 阅读 · 0 评论 -
leetcode 234. 回文链表
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: bool isPalindrome(ListNode* head) { vector<int> vec;.原创 2020-07-28 15:15:06 · 111 阅读 · 0 评论 -
leetcode 1305. 两棵二叉搜索树中的所有元素
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: void dfs(TreeNode* node, vect..原创 2020-07-28 14:31:48 · 105 阅读 · 0 评论 -
leetcode 148. 排序链表
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* sortList(ListNode* head) { if(head == nullptr) .原创 2020-07-28 14:25:54 · 121 阅读 · 0 评论 -
leetcode 147. 对链表进行插入排序
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* insertionSortList(ListNode* head) { if(head == nu.原创 2020-07-28 14:22:46 · 90 阅读 · 0 评论 -
leetcode 1528. 重新排列字符串
class Solution {public: string restoreString(string s, vector<int>& indices) { string temp = s; for(int i = 0; i < indices.size(); i++) { char c = s[i]; s[i] = temp[indices[i]]; ..原创 2020-07-28 14:11:47 · 209 阅读 · 0 评论 -
leetcode 1502. 判断能否形成等差数列
class Solution {public: bool canMakeArithmeticProgression(vector<int>& arr) { sort(arr.begin(), arr.end()); for(int i = 0; i < arr.size() - 1; i++) { if(arr[i + 1] - arr[i] != arr[1] - arr[0]) .原创 2020-07-28 13:55:56 · 186 阅读 · 0 评论 -
leetcode 145. 二叉树的后序遍历
参考链接:https://leetcode-cn.com/problems/binary-tree-inorder-traversal/solution/die-dai-fa-by-jason-2//** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(原创 2020-07-27 21:08:22 · 98 阅读 · 0 评论 -
leetcode 94. 二叉树的中序遍历
参考链接:https://leetcode-cn.com/problems/binary-tree-inorder-traversal/solution/die-dai-fa-by-jason-2//** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(原创 2020-07-27 21:04:54 · 108 阅读 · 0 评论 -
leetcode 144. 二叉树的前序遍历
参考链接:https://www.jianshu.com/p/88af12725af8/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution原创 2020-07-27 20:57:12 · 102 阅读 · 0 评论 -
leetcode 1464. 数组中两元素的最大乘积
class Solution {public: int maxProduct(vector<int>& nums) { sort(nums.begin(), nums.end()); return (nums[nums.size() - 1] - 1) * (nums[nums.size() - 2] - 1); }};原创 2020-07-27 11:08:46 · 142 阅读 · 0 评论 -
leetcode 485. 最大连续1的个数
class Solution {public: int findMaxConsecutiveOnes(vector<int>& nums) { int res = 0; int count = 0; for(int i = 0; i < nums.size(); i++) { if(nums[i] == 1) { count++.原创 2020-07-27 11:04:52 · 69 阅读 · 0 评论 -
leetcode 1460. 通过翻转子数组使两个数组相等
class Solution {public: bool canBeEqual(vector<int>& target, vector<int>& arr) { sort(target.begin(), target.end()); sort(arr.begin(), arr.end()); if(target == arr) { return true; .原创 2020-07-27 10:23:41 · 170 阅读 · 0 评论 -
leetcode 896. 单调数列
class Solution {public: bool isMonotonic(vector<int>& A) { vector<int> B = A; vector<int> C = A; sort(B.begin(), B.end()); sort(C.begin(), C.end(), greater<int>()); if(A == B || A .原创 2020-07-27 10:20:00 · 173 阅读 · 0 评论