- 博客(38)
- 资源 (1)
- 收藏
- 关注
原创 leetcode+求两个链表的交叉点,循环,类似于求最小公倍数那样。或者把一个链表hash,另外一个进行查找
点击打开链接struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {}};class Solution {public: ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {...
2018-06-27 23:00:07 171
原创 leetcode + 判断链表是不是回文的,先存到vector中然后就简单了
class Solution {public: bool isPalindrome(ListNode* head) { vector<int> res; while (head) { res.push_back(head->val); head = head->next; ...
2018-06-27 21:40:10 131
原创 leetcode+ 有序链表删除相同元素
点击打开链接class Solution {public: ListNode* deleteDuplicates(ListNode* head) { ListNode* root = head; while (head != NULL) { /*下一节点存在,且当前节点和下一节点的值重复*/ while (h...
2018-06-27 19:43:45 205
原创 leetcode+删除链表元素,用了一点trick
点击打开链接class Solution {public: void deleteNode(ListNode* node) { //trick node后面的一个节点复制到ndoe,这样就删除node后面的节点就好啦 if(node==NULL) return; node->val = node->next->val; ...
2018-06-27 19:23:14 150
原创 leetcode+ 上楼梯最小花费,dp
点击打开链接class Solution {public: int minCostClimbingStairs(vector<int>& cost) { vector<int> dp(cost.size()+1, 0); //一开始的话费都是0. 因为是到达最顶层,所以要size()+1 for(int i=2; i<...
2018-06-27 18:41:35 211
原创 leetcode+ 上台阶,一步或者二步,DP
点击打开链接class Solution {public: int climbStairs(int n) { vector<int> dp; dp.push_back(0); dp.push_back(1); dp.push_back(2); for(int i=3;i<=n;i++) dp.push_back(dp[i-...
2018-06-25 14:59:09 643
原创 leetcode + 最后一位加1. vector
点击打开链接class Solution {public: vector<int> plusOne(vector<int>& digits) { int end = digits.size()-1; if(digits[end] !=9){ digits[end] +=1; r...
2018-06-23 22:20:37 137
原创 leetcode+ 最后一个单词长度,用栈,有点类似于反转一串单词输出
点击打开链接class Solution {public: int lengthOfLastWord(string s) { if(s.size()==0) return 0; int len = s.size(),num=0; while(s[len-1-num]==' ') num+=1; stack<char&g...
2018-06-23 10:32:50 88
原创 leetcode+模拟题,罗马数字和阿拉伯数字转化
点击打开链接class Solution {public: int romanToInt(string s) { int result = 0; for(int i=0; i<s.size(); i++){ switch (s[i]) { case 'M': result += 1000; br...
2018-06-21 01:33:05 257
原创 leetcode+ Dp题目,字符串匹配
点击打开链接class Solution {public: bool isMatch(string s, string p) { int slen = s.length(); int plen = p.length(); int num = 0; for(int i=0; i<plen; i++){ ...
2018-06-21 01:31:40 625
原创 leetcode+双指针问题,水桶量最大
点击打开链接//双指针class Solution {public: int trap(vector<int>& height) { int n = height.size(); int maxheight = 0, ret =0; vector<int> container(n, 0); ...
2018-06-21 01:29:23 701
原创 leetcode+Dp题目,最小分割使得每一部分为回文串
点击打开链接//DP 很有意思class Solution {public: int minCut(string s) { int n = s.size(); vector<int> f(n+1); vector<vector<bool>> st(n, vector<bool>(n, fal...
2018-06-21 01:27:59 198
原创 leetcode+ 二分题目,返回下标
点击打开链接//二分 class Solution {public: int searchInsert(vector<int>& nums, int target) { int l=0, r=nums.size()-1, pos = 0, mid =0; while (l<=r) { mid = (l+r...
2018-06-21 01:25:51 221
原创 leetcode+ 双指针问题
点击打开链接//双指针问题class Solution {public: int removeDuplicates(vector<int>& nums) { if(nums.size()<1) return 0; int index = 0; for(int i=1; i<nums.size(); i++)...
2018-06-21 01:24:51 195
原创 leetcode+翻转链表
点击打开链接struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {}};class Solution {public: ListNode *reverseKGroup(ListNode *head, int k) { ListNode ...
2018-06-21 01:23:35 97
原创 leetcode+多路归并排序
点击打开链接//多路归并struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {}};class Solution {public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ...
2018-06-21 01:21:25 1105
原创 leetcode+ Jump game||, 贪心
点击打开链接//贪心class Solution {public: int Jump(vector<int>& nums) { int ret=0, curMax=0, curRch =0; for(int i=0; i<nums.size(); i++){ if(curRch < i){ ...
2018-06-20 22:01:44 256
原创 leetcode+ 找到数组中缺失的最小整数,找规律,有点trick
点击打开链接class Solution {public: int firstMissingPositive(vector<int>& nums) { int n = nums.size(); vector<bool> rec(n+2); for(int i=0; i<n; i++){ ...
2018-06-20 14:04:45 1115
原创 leetcode+ 双指针问题,必须要修改数组。不然过不去
点击打开链接class Solution {public: int removeElement(vector<int>& nums, int val) { int size =0, length = nums.size(); for(int i=0; i<length; i++){ if(nums[i]!=...
2018-06-15 23:51:36 191
原创 leetcode+ 链表翻转,注意事项都注释了
点击打开链接class Solution {public: ListNode* reverseList(ListNode* head) { if(!head) return NULL; if(head->next==NULL) return head; ListNode *pre= head; ListNode *cu...
2018-06-15 13:56:06 171
原创 leetcode+ 二叉树最大路径和,不一定要经过root,DFS
点击打开链接struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {public: int ans = 0; int dfs(TreeNode...
2018-06-14 22:11:04 156
原创 leetcode+ 最长连续子序列,哈希
点击打开链接class Solution {public: int longestConsecutive(vector<int>& nums) { int res = 0; unordered_map<int, int> tr_left, tr_right; for(auto& x:nums){ ...
2018-06-14 22:05:55 424
原创 leetcode+ 固定区间长和平均最大,固定窗口,然后滑动。
点击打开链接class Solution {public: double findMaxAverage(vector<int>& nums, int k) { int maxSum =0 , sum =0; for(int i=0; i<k;i++){ sum+=nums[i]; } ...
2018-06-14 22:02:43 180
原创 leetcode+ 二叉树BFS,用栈存储每一层,最后就能输出从底层到顶层每一层
点击打开链接class Solution {public: vector<vector<int>> levelOrderBottom(TreeNode* root) { vector<vector<int>> res; queue<TreeNode*> Q; if(root) Q....
2018-06-11 11:40:15 311
原创 leetcode + 买卖股票,DP,一次扫描
点击打开链接class Solution {public: int maxProfit(vector<int>& prices) { if(prices.size() <=1) return 0; int low = prices[0], maxProfit =0; for(int i=1; i<prices...
2018-06-11 10:50:45 286
原创 leetcode + 最长回文数, DP,使用最长公共子序列
点击打开链接class Solution {public: int longestPalindromeSubseq(string s) { int size = s.length(); char str1[size+2], str2[size+2]; memset(str1, '\0', sizeof(str1)); mem...
2018-06-09 12:06:52 364
原创 leetcode+ 栈的经典题
点击打开链接class Solution {public: bool isValid(string s) { stack<char> st; if(s.size()==0) return true; for(int i=0;i<s.size(); i++){ if(s[i]=='{' || s[i]...
2018-06-07 10:16:08 867
原创 leetcode+ 循环数组,求右边第一个大的数字
点击打开链接//循环数组 求右边第一个大的数class Solution {public: vector<int> nextGreaterElements(vector<int>& nums) { int n = nums.size(); vector<int> res(n, -1); for(...
2018-06-06 23:37:56 777
原创 leetcode + 求数组后第一个大的数字
点击打开链接//找数组后第一个大的数字class Solution {public: void solve(int i, int j, vector<int>& findNums, vector<int>& nums, vector<int>& res) { for(int k=j+1; k<nu...
2018-06-06 23:37:06 428
原创 leetcode + 腾讯面试
点击打开链接//腾讯面试。class Solution {public: int result = 0; void dfs(int sum, int cnt, vector<int>&nums, int S) { if(cnt==nums.size()){ if(sum==S) ...
2018-06-06 23:35:55 977
原创 leetcode + 子树和最多,map操作
点击打开链接class Solution {public: map<int, int> m; void dfs(TreeNode* root){ if(root==NULL) return; if(root->left !=NULL){ dfs(root->left); roo...
2018-06-06 23:32:37 218
原创 leetcode + Dp, 买卖股票最大
点击打开链接class Solution {public: int maxProfit(vector<int>& prices) { int n = prices.size(), result = 0; if(n<2) return 0; vector<int> f(n, 0), g(n,0); ...
2018-06-06 23:18:08 365
原创 leetcode + 找出两个有序数组的中位数,注意特殊清楚处理就行
点击打开链接class Solution {public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { int m = nums1.size(), n = nums2.size(); double result =0;...
2018-06-06 10:56:38 402
原创 leetcode + 最后一层的最左边值,使用BFS,queue的大小.size()
点击打开链接class Solution {public: int findBottomLeftValue(TreeNode* root) { queue<TreeNode*> Q; Q.push(root); TreeNode* leftnode = NULL; while (!Q.empty()) { ...
2018-06-05 15:45:03 175
原创 leetcode+ 递归,从根到叶子累加值和为sum?
点击打开链接class Solution {public: bool hasPathSum(TreeNode* root, int sum) { if(!root) return false; if(!root->left && !root->right) return root->val == sum; ...
2018-06-02 17:32:33 132
原创 leetcode + 构造题
点击打开链接//构造题class Solution {public: int magicalString(int n) { string s= "122"; int index = 2; while (s.length() < n) { int cnt = s[index] - '0'; ...
2018-06-02 17:22:07 191
原创 leetcode+ 求所有数据的haiming distance,异或。
点击打开链接class Solution {public: int totalHammingDistance(vector<int>& nums) { int len = nums.size(); if(len==0) return 0; int result = 0; for(int i=0; i<...
2018-06-02 11:47:51 106
原创 leetcode+ 前面0不算,取反
点击打开链接class Solution {public: int findComplement(int num) { long int flag = 1; long int res = 0; while(flag && flag<=num){ int t = flag & num; ...
2018-06-01 15:33:33 223
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人