
LeetCode - 简单
LeetCode 简单题
IDrandom
菜菜菜
展开
-
[LeetCode 404, 405][简单]左叶子之和/数字转换为十六进制数
404.左叶子之和题目链接static const auto io_speed_up =[](){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); return 0;}();class Solution {public: int sumOfLeftLeaves(TreeNode* root...原创 2020-02-29 16:06:32 · 256 阅读 · 0 评论 -
[LeetCode 392,401][简单]判断子序列/二进制手表
392.判断子序列题目链接static const auto io_speed_up = [](){ ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); return NULL;}();class Solution {public: bool isSubsequence(strin...原创 2020-02-28 16:01:35 · 205 阅读 · 0 评论 -
[LeetCode 387,389][简单]字符串中的第一个唯一字符/找不同
387.字符串中的第一个唯一字符题目链接static const auto io_speed_up = [](){ ios::sync_with_stdio(false); cin.tie(0); return 0;}();class Solution {public: int firstUniqChar(string s) { int f...原创 2020-02-27 16:25:02 · 158 阅读 · 0 评论 -
[LeetCode 374,383][简单]猜数字大小/赎金信
374.猜数字大小题目链接static const auto io_speed_up = [](){ ios::sync_with_stdio(0); cin.tie(0); return 0;}();int guess(int num);class Solution {public: typedef long long LL; int gu...原创 2020-02-26 17:24:27 · 239 阅读 · 0 评论 -
[LeetCode 367,371][简单]有效的完全平方数/两整数之和
367.有效的完全平方数题目链接static const auto io_speed_up = [](){ ios::sync_with_stdio(0); cin.tie(0); return 0;}();class Solution {public: bool isPerfectSquare(int num) { double ans...原创 2020-02-25 15:13:47 · 154 阅读 · 0 评论 -
[LeetCode 349,350][简单]两个数组的交集/两个数组的交集 II
349.两个数组的交集题目链接static const auto io_speed_up = [](){ ios::sync_with_stdio(0); cin.tie(0); return 0;}();class Solution {public: vector<int> intersection(vector<int>&am...原创 2020-02-24 15:46:37 · 155 阅读 · 0 评论 -
[LeetCode 344,345][简单]反转字符串/反转字符串中的元音字母
344.反转字符串题目链接static const auto io_speed_up = []() { ios::sync_with_stdio(false); cin.tie(0); return 0; }();class Solution {public: void reverseString(vector<char>& s) {...原创 2020-02-23 16:59:24 · 186 阅读 · 0 评论 -
[LeetCode 326,342][简单]3的幂/4的幂
326.3的幂题目链接比较好想的是取对数法。class Solution {public: double eps = 1e-11; bool isPowerOfThree(int n) { if(n<=0)return 0; double pw = log2(n) / log2(3.0); return fabs(rou...原创 2020-02-23 16:32:21 · 242 阅读 · 0 评论 -
[LeetCode 299,303][简单]猜数字游戏/区域和检索 - 数组不可变
299.猜数字游戏题目链接class Solution {public: string getHint(string secret, string guess) { ios::sync_with_stdio(0); cin.tie(0); int a[10]={0},b[10]={0}; int acnt=0,bcnt=...原创 2020-02-22 22:39:14 · 400 阅读 · 0 评论 -
[LeetCode 290,292][简单]单词规律/Nim 游戏
290.单词规律题目链接class Solution {public: bool wordPattern(string pattern, string str) { ios::sync_with_stdio(0); cin.tie(0); istringstream iss(str); unordered_map<...原创 2020-02-21 15:17:25 · 172 阅读 · 0 评论 -
[LeetCode 278,283][简单]第一个错误的版本/移动零
278.第一个错误的版本题目链接// Forward declaration of isBadVersion API.bool isBadVersion(int version);class Solution {public: typedef long long LL; int firstBadVersion(int n) { LL l = 1, r =...原创 2020-02-20 17:49:54 · 179 阅读 · 0 评论 -
[LeetCode 263,268][简单]丑数/缺失数字
263.丑数题目链接class Solution {public: bool isUgly(int num) { while(num&&(num&1)==0)num>>=1; while(num&&(num%3)==0)num/=3; while(num&&(num%5...原创 2020-02-19 13:32:23 · 246 阅读 · 0 评论 -
[LeetCode 257,258][简单]二叉树的所有路径/各位相加
257.二叉树的所有路径题目链接class Solution {public: vector<string>ans; string path=""; void getans(TreeNode* root,string path){ if(root)path += to_string(root->val); else r...原创 2020-02-18 18:06:30 · 155 阅读 · 0 评论 -
[LeetCode 237,242][简单]删除链表中的节点/有效的字母异位词
237.删除链表中的节点题目链接class Solution {public: void deleteNode(ListNode* node) { *node = *(node -> next); }};242.有效的字母异位词题目链接class Solution {public: bool isAnagram(string s, s...原创 2020-02-17 18:33:42 · 160 阅读 · 0 评论 -
[LeetCode 234,235][简单]回文链表/二叉搜索树的最近公共祖先
234.回文链表题目链接class Solution {public: bool isPalindrome(ListNode* head) { ListNode *h = head; ListNode *f = new ListNode(0); ListNode *s = new ListNode(0); f->n...原创 2020-02-16 19:26:13 · 168 阅读 · 0 评论 -
[LeetCode 231,232][简单]2的幂/用栈实现队列
231.2的幂题目链接class Solution {public: bool isPowerOfTwo(int n) { return n>0 && !(n&-n^n); }};232.用栈实现队列题目链接class MyQueue {public: /** Initialize your data stru...原创 2020-02-15 18:05:24 · 155 阅读 · 0 评论 -
[LeetCode 225,226][简单]用队列实现栈/翻转二叉树
225.用队列实现栈题目链接class MyStack {public: /** Initialize your data structure here. */ deque<int>mq; int tp; MyStack() { } /** Push element x onto stack. */ ...原创 2020-02-14 16:12:58 · 207 阅读 · 0 评论 -
[LeetCode 217,219][简单]存在重复元素/存在重复元素II
217.存在重复元素题目链接class Solution {public: bool containsDuplicate(vector<int>& nums) { unordered_set<int>ms; for(auto i : nums){ if(ms.count(i))return tru...原创 2020-02-13 16:13:49 · 194 阅读 · 0 评论 -
[LeetCode 205,206][简单]同构字符串/反转链表
205.同构字符串题目链接只用比较每个字符上一次出现的位置是否一致class Solution {public: int mps[256],mpt[256]; bool isIsomorphic(string s, string t) { memset(mps,-1,sizeof mps); memset(mpt,-1,sizeof mpt)...原创 2020-02-12 12:36:02 · 150 阅读 · 0 评论 -
[LeetCode 203,204][简单]移除链表元素/计数质数
203.移除链表元素题目链接比较通用的思路是加一个dummyhead,那我就用二级指针做一下class Solution {public: ListNode* removeElements(ListNode* head, int val) { ListNode **p = &head; while(*p){ if((*...原创 2020-02-11 19:07:20 · 195 阅读 · 0 评论 -
[LeetCode 198,202][简单]打家劫舍/快乐数
198.打家劫舍题目链接dp[n]=max(dp[n−1],dp[n−2]+w[n])dp[n] = max(dp[n-1],dp[n-2]+w[n])dp[n]=max(dp[n−1],dp[n−2]+w[n])可以滚动优化一下空间class Solution {public: int rob(vector<int>& nums) { int...原创 2020-02-10 17:31:35 · 256 阅读 · 0 评论 -
[LeetCode 190,191][简单]颠倒二进制位/位1的个数
190.颠倒二进制位题目链接贴一个纯位运算的版本和一个纯bitset的版本class Solution {public: uint32_t reverseBits(uint32_t n) { bitset<32>a(n); string tmp = a.to_string(); reverse(tmp.begin(), t...原创 2020-02-09 18:27:16 · 257 阅读 · 0 评论 -
[LeetCode 172,189][简单]阶乘后的零/旋转数组
172.阶乘后的零题目链接10 = 2 * 5,比n小是2的倍数的数肯定比是5的多,所以只需要看因子5出现的次数class Solution {public: int trailingZeroes(int n) { int ans = 0; while(n){ n/=5; ans += n; ...原创 2020-02-08 19:10:37 · 248 阅读 · 0 评论 -
[LeetCode 169,171][简单]多数元素/Excel表列序号
169.多数元素题目链接比较显然的做法是排个序后取中间元素和哈希表看了下题解发现最后一个方法比较巧妙,叫做Boyer-Moore 投票算法,就是先把第一个元素设置为候选,然后循环过程中对候选+1,非候选 -1,到零后把下一个元素置为候选,最后的候选就是真值,简单证明下:候选为真,则最多删掉同样数量的候选与非候选候选为假删掉更多的非候选,对结果无影响删掉同样多的非候选与候选真...原创 2020-02-07 14:48:21 · 266 阅读 · 0 评论 -
[LeetCode 111,168][简单]二叉树的最小深度/Excel表列名称
111.二叉树的最小深度题目链接之前漏掉了,补上class Solution {public: int minDepth(TreeNode* root) { if(root == NULL)return 0; if(root->left && root->right) return min(minDepth(root -&...原创 2020-02-06 18:04:33 · 217 阅读 · 0 评论 -
[LeetCode 160,167][简单]相交链表/两数之和 II - 输入有序数组
160.相交链表题目链接class Solution {public: ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { ios::sync_with_stdio(false); if(!(headA&&headB))return NULL; ...原创 2020-02-06 02:31:02 · 162 阅读 · 0 评论 -
[LeetCode 141,155][简单]环形链表/最小栈
141.环形链表题目链接显然的做法是用unordered_map,节约空间可以用快慢指针。class Solution {public: bool hasCycle(ListNode *head) { ios::sync_with_stdio(false); if(!(head && head -> next))return f...原创 2020-02-05 21:41:29 · 229 阅读 · 0 评论 -
[LeetCode 125,136][简单]验证回文串/只出现一次的数字
125.验证回文串题目链接class Solution {public: bool isv(char p){ if(p<='z' && p>='a')return true; if(p<='9' && p>='0')return true; return false; }...原创 2020-02-04 23:17:30 · 170 阅读 · 0 评论 -
[LeetCode 121,122][简单]买卖股票的最佳时机/买卖股票的最佳时机II
121.买卖股票的最佳时机题目链接class Solution {public: int maxProfit(vector<int>& prices) { int mn=INT_MAX,ans=0; for(int i=1,siz=prices.size();i<siz;i++){ mn = min(m...原创 2020-02-04 21:45:36 · 175 阅读 · 0 评论 -
[LeetCode 118,119][简单]杨辉三角/杨辉三角II
118.杨辉三角题目链接存一个杨辉三角class Solution {public: vector<vector<int>> generate(int numRows) { vector<vector<int>>ans; for(int i=0;i<numRows;i++){ ...原创 2020-02-04 01:43:56 · 141 阅读 · 0 评论 -
[LeetCode 110,112][简单]平衡二叉树/路径总和
110.平衡二叉树题目链接判断一棵树是不是平衡二叉树class Solution {public: bool blc(TreeNode *a,int &h){ if(a==NULL){h=0;return true;} int hl,hr; if(!blc(a->left,hl))return false; ...原创 2020-02-03 21:05:02 · 168 阅读 · 0 评论 -
[LeetCode 107,108][简单]二叉树的层次遍历 II/将有序数组转换为二叉搜索树
107,二叉树的层次遍历 II题目链接class Solution {public: typedef pair<TreeNode *,int> pii; vector<vector<int>> levelOrderBottom(TreeNode* root) { vector<vector<int>>...原创 2020-02-03 04:34:59 · 190 阅读 · 0 评论 -
[LeetCode 101,104][简单]对称二叉树/二叉树的最大深度
101.对称二叉树题目链接class Solution {public: bool issame(TreeNode* a, TreeNode* b){ if(a == NULL && b == NULL)return true; if(a == NULL || b == NULL)return false; if(a -...原创 2020-02-03 00:39:03 · 165 阅读 · 0 评论 -
[LeetCode 88,100][简单]合并两个有序数组/相同的树
88.合并两个有序数组题目链接class Solution {public: void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { int i=m-1,j=n-1,k=m+n-1; while(i>=0&&j&g...原创 2020-02-01 16:53:45 · 199 阅读 · 0 评论 -
[LeetCode 70,83][简单]爬楼梯/删除排序链表中的重复元素
70.爬楼梯题目链接根据递推公式写一个快速幂,矩阵形式如下A=(1110)A = \left( \begin{array}{cc} 1 & 1\\ 1 & 0 \end{array} \right)A=(1110)当然递推公式也可以写成解析形式的解,有兴趣可以了解下线性齐次递推式的解class Solution { typedef long long LL;...原创 2020-02-01 16:14:54 · 165 阅读 · 0 评论 -
[LeetCode 67,69][简单]二进制求和(模拟)/x的平方根(牛顿法)
67.二进制求和题目链接class Solution {public: void upd(string &c,int add,int &op){ c += to_string((add + op) & 1); op = (add + op) >> 1; } string addBinary(strin...原创 2020-01-31 05:31:01 · 206 阅读 · 0 评论 -
[LeetCode 58,66][简单]最后一个单词的长度/加一
58.最后一个单词的长度题目链接流的使用class Solution {public: int lengthOfLastWord(string s) { reverse(s.begin(),s.end()); istringstream iss(s); string tmp=""; while(iss>>t...原创 2020-01-29 20:33:36 · 170 阅读 · 0 评论 -
[LeetCode 38,53][简单]外观数列/最大子序和
38.外观数列题目链接可以熟悉一下stringclass Solution {public: string countAndSay(int n) { string pre = "1"; for(int i = 1; i < n; i++){ int cnt = 1; string nxt = ""...原创 2020-01-29 04:53:30 · 200 阅读 · 0 评论 -
[LeetCode 28,35][简单]实现strStr()/搜索插入位置
28.实现strStr()题目链接复习了下kmpclass Solution {public: typedef vector<int> vi; vi nxt; void getnext(string needle){ int len=needle.length(); nxt=vi(len+10);nxt[0]=-1; ...原创 2020-01-27 03:48:27 · 129 阅读 · 0 评论 -
[LeetCode 21,26,27][简单]合并两个有序链表/删除排序数组中的重复项/移除元素
21.合并两个有序链表题目链接直接比过去,把原来的链表拼起来,当然也可以拿值new过去。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }...原创 2020-01-23 03:32:02 · 1572 阅读 · 0 评论