![](https://img-blog.csdnimg.cn/20201014180756757.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode备份-简单
ChenD17
这个作者很懒,什么都没留下…
展开
-
leetcode-67-二进制求和
classSolution{ public: stringaddBinary(stringa,stringb){ stringres=""; intflag=0; if(a.length()<b.length())swap(a,b); for(inti=0;i<b.length();++i){ intib=b.length()-...原创 2020-06-24 14:45:56 · 137 阅读 · 0 评论 -
leetcode-437-路径总和III
classSolution{ public: //包含root结点的条数 intfindPath(TreeNode*root,intsum){ intres=0; if(root){ if(root->val==sum)res++; //if(roo...原创 2020-06-22 20:11:54 · 135 阅读 · 0 评论 -
leetcode-437-路径总和III
classSolution{ public: //包含root结点的条数 intfindPath(TreeNode*root,intsum){ intres=0; if(root){ if(root->val==sum)res++; //if(roo...原创 2020-06-22 20:12:01 · 84 阅读 · 0 评论 -
leetcode-257-二叉树的所有路径
/** *Definitionforabinarytreenode. *structTreeNode{ *intval; *TreeNode*left; *TreeNode*right; *TreeNode(intx):val(x),left(NULL),right(NULL){} *};...原创 2020-06-22 20:09:52 · 109 阅读 · 0 评论 -
leetcode-404-左叶子之和
/** *Definitionforabinarytreenode. *structTreeNode{ *intval; *TreeNode*left; *TreeNode*right; *TreeNode(intx):val(x),left(NULL),right(NULL){} *};...原创 2020-06-22 20:10:02 · 85 阅读 · 0 评论 -
leetcode-112-路径总和
/** *Definitionforabinarytreenode. *structTreeNode{ *intval; *TreeNode*left; *TreeNode*right; *TreeNode(intx):val(x),left(NULL),right(NULL){} *};...原创 2020-06-23 14:26:45 · 153 阅读 · 0 评论 -
leetcode-110-平衡二叉树
/** *Definitionforabinarytreenode. *structTreeNode{ *intval; *TreeNode*left; *TreeNode*right; *TreeNode(intx):val(x),left(NULL),right(NULL){} *};...原创 2020-06-22 20:12:16 · 101 阅读 · 0 评论 -
leetcode-101-对称二叉树
/** *Definitionforabinarytreenode. *structTreeNode{ *intval; *TreeNode*left; *TreeNode*right; *TreeNode(intx):val(x),left(NULL),right(NULL){} *};...原创 2020-06-22 20:12:09 · 134 阅读 · 0 评论 -
leetcode-100-相同的树
/** *Definitionforabinarytreenode. *structTreeNode{ *intval; *TreeNode*left; *TreeNode*right; *TreeNode(intx):val(x),left(NULL),right(NULL){} *};...原创 2020-06-22 20:10:44 · 73 阅读 · 0 评论 -
leetcode-226-翻转二叉树
//注意使用swap /** *Definitionforabinarytreenode. *structTreeNode{ *intval; *TreeNode*left; *TreeNode*right; *TreeNode(intx):val(x),left(NULL),right(NUL...原创 2020-06-22 20:11:26 · 94 阅读 · 0 评论 -
leetcode-111-二叉树的最小深度
//和最大深度的情况不一样,注意不能直接return min(左右子叶递归) /** *Definitionforabinarytreenode. *structTreeNode{ *intval; *TreeNode*left; *TreeNode*right; *TreeNode(intx):v...原创 2020-06-22 20:10:54 · 134 阅读 · 0 评论 -
leetcode-104-二叉树的最大深度
//递归,不用helper /** *Definitionforabinarytreenode. *structTreeNode{ *intval; *TreeNode*left; *TreeNode*right; *TreeNode(intx):val(x),left(NULL),right(...原创 2020-06-23 14:26:54 · 96 阅读 · 0 评论 -
leetcode-20-有效的括号
classSolution{ public: boolisValid(strings){ stack<char>helper; intN=s.length(); for(inti=0;i<N;i++){ if(s[i]=='{'||s[i]=='...原创 2020-06-23 14:28:21 · 150 阅读 · 0 评论 -
leetcode-237-删除链表中的节点
//复制后一个的val给当前node,并删除后一个node /** *Definitionforsingly-linkedlist. *structListNode{ *intval; *ListNode*next; *ListNode(intx):val(x),next(NULL){} *}; */ ...原创 2020-06-23 14:27:21 · 86 阅读 · 0 评论 -
leetcode-121-买卖股票的最佳时机
原创 2020-06-23 14:28:39 · 81 阅读 · 0 评论 -
leetcode-122-买卖股票的最佳时机II
classSolution{ public: intmaxProfit(vector<int>&prices){ if(prices.size()==0)returnNULL; vector<int>status(2); vector<vector<int>...原创 2020-06-23 14:27:32 · 111 阅读 · 0 评论 -
leetcode-53-最长子序和
classSolution{ public: intmaxSubArray(vector<int>&nums){ if(nums.size()==0)returnNULL; if(nums.size()==1)returnnums[0]; intres=nums[0]; ...原创 2020-06-23 14:28:47 · 141 阅读 · 0 评论 -
leetcode-203-移除链表元素
//注意链表为空的情况 class Solution { public: ListNode* removeElements(ListNode* head, int val) { while (head != NULL && head->val == val) { ListNode* temp = head; ...原创 2020-06-23 14:30:12 · 89 阅读 · 0 评论 -
leetcode-83-删除排序链表中的重复元素
//注意每次取值或取next,都需要检查当前节点是否为空 class Solution { public: ListNode* deleteDuplicates(ListNode* head) { ListNode* curNode = head; while (curNode != NULL) { if (curNode-&g...原创 2020-06-23 14:28:58 · 100 阅读 · 0 评论 -
leetcode-206-反转链表
/** *Definitionforsingly-linkedlist. *structListNode{ *intval; *ListNode*next; *ListNode(intx):val(x),next(NULL){} *}; */ classSolution{ public: ...原创 2020-06-22 20:05:30 · 93 阅读 · 0 评论 -
leetcode-219-存在重复元素II
//一开始使用的map,后来发现用set就行了 class Solution { public: bool containsNearbyDuplicate(vector<int>& nums, int k) { if (nums.size() == 0 || nums.size() == 1) return false; set&l...原创 2020-06-23 14:29:34 · 107 阅读 · 0 评论 -
leetcode-1-两数之和
bool cmp(pair<int, int> a, pair<int, int> b) { return (a.first < b.first); } class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { ...原创 2020-06-24 14:42:17 · 101 阅读 · 0 评论 -
leetcode-290-单词规律
//使用map构建pattern与单词之间的关系,使用set确保关系一一对应 class Solution { public: bool wordPattern(string pattern, string str) { vector<string> input; int cnt = 0; map<char, stri...原创 2020-06-24 14:43:03 · 171 阅读 · 0 评论 -
leetcode-242-有效的字母异位词
classSolution{ public: boolisAnagram(strings,stringt){ if(s.length()!=t.length())returnfalse; intN=s.length(); map<char,int>freq_t,freq_s; ...原创 2020-07-01 16:18:50 · 95 阅读 · 0 评论 -
leetcode-350-两个数组的交集II
classSolution{ public: vector<int>intersect(vector<int>&nums1,vector<int>&nums2){ map<int,int>record; vector<int>res; ...原创 2020-06-24 14:42:26 · 95 阅读 · 0 评论 -
leetcode-345-反转字符串中的元音字母
bool helper(char ch) { if ((ch == 'a') || (ch == 'e') || (ch == 'i') || (ch == 'o') || (ch == 'u') || (ch == 'A') || (ch == 'E') || (ch ==...原创 2020-07-01 16:19:41 · 128 阅读 · 0 评论 -
leetcode-125-验证回文串
string char_type(char input) { if (input > 47 && input < 58) return "numbers"; else if (input > 64 && input < 91) return "LETTERS"; else if (input > 96 &am...原创 2020-06-24 14:44:06 · 78 阅读 · 0 评论 -
leetcode-88-合并两个有序数组
//原地插入,没有另外开辟空间,插入使用vector的insert classSolution{ public: voidmerge(vector<int>&nums1,intm,vector<int>&nums2,intn){ intnums1i=0; for(inti...原创 2020-06-28 09:46:40 · 82 阅读 · 0 评论 -
leetcode-27-移除元素
classSolution{ public: intremoveElement(vector<int>&nums,intval){ intcount=0,N=nums.size(); for(inti=0;i<N;i++){ if(nums[i]=...原创 2020-06-22 20:05:18 · 61 阅读 · 0 评论 -
leetcode-258-各位相加
using namespace std; //日常取余操作 class Solution { public: int addDigits(int num) { if (num / 10 == 0) return num; int sum = 0; while (num / 10 != 0) { sum += num...原创 2020-06-23 14:29:42 · 112 阅读 · 0 评论 -
leetcode-171-excel表序列号
class Solution { public: int letters2nums(char c) { return int(c - 64); } int titleToNumber(string s) { int N = s.length(), res=0, temp, power; for (int i = N - 1...原创 2020-06-28 09:47:12 · 352 阅读 · 0 评论 -
leetcode-434-字符串中的单词数
class Solution { public: int countSegments(string s) { int res=0, N = s.length(); if (N == 0) return 0; char temp; for (int i = 0; i < N-1; i++) { ...原创 2020-06-23 14:31:06 · 107 阅读 · 0 评论 -
leetcode-26-删除排序数组中的重复项
class Solution { public: int removeDuplicates(vector<int>& nums) { int left = 0, right = 1, N = nums.size(); if (N == 0) return 0; for (; right < N; right++)...原创 2020-06-28 09:47:19 · 71 阅读 · 0 评论 -
leetcode-821-字符的最短距离
821.字符的最短距离 class Solution { public: vector<int> shortestToChar(string S, char C) { vector<int> res, C_pos; int pos_i = 1, temp; int N = S.length(); ...原创 2020-06-22 20:11:05 · 101 阅读 · 0 评论