leetcode学习
xingkong1992
这个作者很懒,什么都没留下…
展开
-
25. Reverse Nodes in k-Group
LeetCode链接 描述:给一个链表,对间隔为k个元素翻转 要求:不能改变链表的值, 空间复杂度为常量 例子: 指定链表:1->2->3->4->5 k=2,输出:2->1->4->3->5 k=3,输出:3->2->1->4->5class Solution {public: ListNode* reverseKGroup(ListNode* head原创 2017-12-08 20:08:47 · 85 阅读 · 0 评论 -
53. Maximum Subarray
vector读写法人复杂度为常数级 连续的空间 以2倍增长 假设:你用了1百万,可能换要1百万 k为规模,一旦和规模相关,则不能省略去冗余是基础 用增量的方式去冗余 53. Maximum Subarray描述:Find the contiguous subarray within an array (containing at least one number) which原创 2017-12-08 23:29:58 · 82 阅读 · 0 评论 -
7 Reverse Integer
class Solution {public: int reverse(int x) { long long res=0; bool isPos=true; if(x>0) isPos=true; else { isPos=false;原创 2017-12-11 10:27:08 · 136 阅读 · 0 评论 -
9. Palindrome Number
回文判断多见于字符串,应该首先考虑将int转换成string,但题目中限制不能使用额外的空间class Solution {public: bool isPalindrome(int x) { if(x<0) return false; int div=1; while(x/div>=10) div*=10; while原创 2017-12-11 11:01:19 · 72 阅读 · 0 评论 -
13. Roman to Integer
class Solution {public: int romanToInt(string s) { int res=0; unordered_map<char,int> m{{'I',1},{'V',5},{'X',10},{'L',50},{'C',100},{'D',500},{'M',1000}}; for(int i=0;i<s.siz原创 2017-12-11 11:45:09 · 87 阅读 · 0 评论 -
14. Longest Common Prefix
通过i,j将vector变为一个二维数组,先扫每个string的i个字符,再一个字符的遍历class Solution {public: string longestCommonPrefix(vector<string>& strs) { if(strs.empty()) return ""; string max=""; for(int i原创 2017-12-11 12:54:56 · 87 阅读 · 0 评论 -
20. Valid Parentheses
class Solution {public: bool isValid(string s) { stack<char> stac; if(s.empty()) return false; for(int i=0;i<s.size();i++){ if(s[i]=='('||s[i]=='['||s[i]=='{'){原创 2017-12-11 14:16:40 · 111 阅读 · 0 评论 -
234. Palindrome Linked List
bool isPalindrome(ListNode* head) { if(!head||!head->next) return true; ListNode*slow=head,*fast=head; stack<int> s; s.push(head->val); while(fast->next&&fast->n原创 2017-12-19 22:51:52 · 142 阅读 · 0 评论 -
21. Merge Two Sorted Lists
class Solution {public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode* newLis=new ListNode(-1),*cur=newLis; while(l1&&l2){ if(l1->val>l2->val){原创 2017-12-11 23:32:02 · 87 阅读 · 0 评论 -
235. Lowest Common Ancestor of a Binary Search Tree
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if(!root) return NULL; if(root->val>max(p->val,q->val)) return lowestCommonAncestor(root->left,p,q原创 2017-12-21 09:05:25 · 105 阅读 · 0 评论 -
237. Delete Node in a Linked List
void deleteNode(ListNode* node) { node->val=node->next->val; node->next=node->next->next; }原创 2017-12-21 09:37:25 · 88 阅读 · 0 评论 -
242. Valid Anagram
核心在哈希表映射,我们用数组代替哈希表。 我们先判断字符串的长度是否相等,不相等直接返回false bool isAnagram(string s, string t) { if(s.size()!=t.size()) return false; int m[26]={0}; for(int i=0;i<s.size()原创 2017-12-21 10:24:07 · 93 阅读 · 0 评论 -
26. Remove Duplicates from Sorted Array
使用快慢指针:快,慢指针从坐标0开始,当所指的值一样,快指针加一,值不一样时,快慢指针都加一。当快指针遍历链表时,给慢指针的坐标值加一就是去重后的长度。 int removeDuplicates(vector<int>& nums) { if(nums.empty()) return 0; int slow=0,fast=0,n=nums.size();原创 2017-12-12 18:59:16 · 87 阅读 · 0 评论 -
35. Search Insert Position
int searchInsert(vector<int>& nums, int target) { for(int i=0;i<nums.size();i++){ if(nums[i]>=target) return i; } return nums.size(); }使用二分法优化时间复杂度 i原创 2017-12-12 19:40:54 · 101 阅读 · 0 评论 -
38. Count and Say
问题描述:第i+1个字符串是第i个字符串的读法。 string countAndSay(int n) { if(n<=0) return ""; string res="1"; while(--n){ string cur=""; for(int i=0;i<res.size();i++){原创 2017-12-12 20:33:54 · 107 阅读 · 0 评论 -
58. Length of Last Word
int lengthOfLastWord(string s) { if(s.empty()) return 0; int len=0; for(int j=s.size()-1;j>=0;j--){ if(s[j]!=' '){ len++; }else if(len!=0原创 2017-12-12 20:49:11 · 126 阅读 · 0 评论 -
66. Plus One
vector<int> plusOne(vector<int>& digits) { int i=digits.size()-1,flag=0; for(i;i>=0;i--){ if(digits[i]==9){ digits[i]=0; flag=1;原创 2017-12-12 22:12:00 · 115 阅读 · 0 评论 -
67. Add Binary
将短的字符串和长的字符串补齐,再相加。 string addBinary(string a, string b) { int na = a.size(); int nb = b.size(); if (na > nb) { for (int i = 0; i < na - nb; ++i) b.insert(b.begin(),原创 2017-12-13 01:47:06 · 99 阅读 · 0 评论 -
69. Sqrt(x)
使用二分法搜索最佳值 int mySqrt(int x) { long long left=0,right=x/2+1; while(left<=right){ long long mid=(left+right)/2; long long squa=mid*mid; if(squa==x) r原创 2017-12-13 10:28:00 · 239 阅读 · 0 评论 -
70. Climbing Stairs
类似菲波拉契数 int climbStairs(int n) { if(n<1) return 0; vector<int> dp(n); dp[0]=1; dp[1]=2; for(int i=2;i<n;i++){ dp[i]=dp[i-1]+dp[i-2]; }原创 2017-12-13 10:58:02 · 98 阅读 · 0 评论 -
83. Remove Duplicates from Sorted List
ListNode* deleteDuplicates(ListNode* head) { ListNode*slow=head,*fast=head; while(fast){ if(slow->val!=fast->val) { slow->next=fast; fast=fas原创 2017-12-13 11:15:04 · 86 阅读 · 0 评论 -
88. Merge Sorted Array
void merge(vector<int>& nums1, int m, vector<int>& nums2, int n) { vector<int> res(m+n); int i=0,j=0,k=0; while(i<m&&j<n){ if(nums1[i]<nums2[j]) res[原创 2017-12-13 12:22:32 · 85 阅读 · 0 评论 -
101. Symmetric Tree
递归的方法 bool isSymmetric(TreeNode* root) { if(!root) return true; return isSymmetric(root->left,root->right); } bool isSymmetric(TreeNode* left,TreeNode* right){ if(!le原创 2017-12-13 14:00:27 · 120 阅读 · 0 评论 -
257. Binary Tree Paths
使用dfs vector<string> binaryTreePaths(TreeNode* root) { vector<string> res; if(root) dfs(root,"",res); return res; } void dfs(TreeNode* root,string str1,vector<string>原创 2017-12-21 20:07:00 · 90 阅读 · 0 评论 -
258. Add Digits
考察的是数根Digital_root int addDigits(int num) { int sum; while(num/10){ sum=0; while(num){ sum+=num%10; num/=10; }原创 2017-12-21 21:24:46 · 97 阅读 · 0 评论 -
263. Ugly Number
bool isUgly(int num) { while(num>=2){ if(num%2==0) num/=2; else if(num%3==0) num/=3; else if(num%5==0) num/=5; else return false; }原创 2017-12-21 22:09:34 · 93 阅读 · 0 评论 -
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not use the same ele原创 2017-12-05 00:54:28 · 97 阅读 · 0 评论 -
104. Maximum Depth of Binary Tree
递归形式 int maxDepth(TreeNode* root) { if(!root) return 0; return 1+max(maxDepth(root->left),maxDepth(root->right)); }使用BFS int maxDepth(TreeNode* root) { if(!root) retur原创 2017-12-13 20:11:39 · 81 阅读 · 0 评论 -
107. Binary Tree Level Order Traversal II
vector<vector<int>> levelOrderBottom(TreeNode* root) { vector<vector<int>> res; if(!root) return res; queue<TreeNode*> q; q.push(root); while(!q.empty()){原创 2017-12-13 21:07:40 · 102 阅读 · 0 评论 -
268. Missing Number
int missingNumber(vector<int>& nums) { int sum=0,n=nums.size(); for(int i=0;i<n;i++) sum+=nums[i]; return n*(1+n)/2-sum; }我们将少了的数和完整的1到n数组异或一下,相同的为0,结果为少了的数。原创 2017-12-22 09:54:23 · 92 阅读 · 0 评论 -
278. First Bad Version
那就是如果left和right都特别大的话,那么left+right可能会溢出,我们的处理方法就是变成left + (right - left) / 2,很好的避免的溢出问题。 int firstBadVersion(int n) { int left=1,right=n; while(left<right){ int mid=left+原创 2017-12-22 10:24:01 · 88 阅读 · 0 评论 -
283. Move Zeroes
使用快慢指针 void moveZeroes(vector<int>& nums) { for(int i=0,j=0;i<nums.size();i++){ if(nums[i]) swap(nums[i],nums[j++]); } } void moveZeroes(vector<int原创 2017-12-22 11:24:06 · 91 阅读 · 0 评论 -
108. Convert Sorted Array to Binary Search Tree
二叉查找树始终满足左<根<右,如果将二叉搜索树中序遍历会得到一个有序数组。反过来,二叉搜索树的根节点应该是有序数组的中间点,从中间点分开为左右两个有序数组,再分别找出左右数组的中间点作为左右子树的根节点。 TreeNode* sortedArrayToBST(vector<int>& nums) { return sortedArrayToBST(nums,0,nums.size(原创 2017-12-14 10:42:06 · 87 阅读 · 0 评论 -
110. Balanced Binary Tree
bool isBalanced(TreeNode* root) { if(!root) return true; if(abs(getDept(root->left)-getDept(root->right))>1) return false; return isBalanced(root->left)&&isBalanced(root->right)原创 2017-12-14 11:10:04 · 82 阅读 · 0 评论 -
111. Minimum Depth of Binary Tree
用DFS完成 int minDepth(TreeNode* root) { if(!root) return 0; if(!root->left&&!root->right) return 1; if(root->left==NULL) return 1+minDepth(root->right); else if(root->r原创 2017-12-14 11:22:19 · 83 阅读 · 0 评论 -
Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it原创 2017-12-06 02:44:31 · 88 阅读 · 0 评论 -
112. Path Sum
bool hasPathSum(TreeNode* root, int sum) { if(!root) return false; if(!root->left&&!root->right&&root->val==sum) return true; return hasPathSum(root->left,sum-root->val)||hasPa原创 2017-12-14 18:15:07 · 85 阅读 · 0 评论 -
118. Pascal's Triangle
vector<vector<int>> generate(int numRows) { vector<vector<int>> res; if(numRows<=0) return res; res.assign(numRows,vector<int>(1,1)); for(int i=1;i<numRows;i++){原创 2017-12-14 19:23:51 · 112 阅读 · 0 评论 -
119. Pascal's Triangle II
vector<int> getRow(int rowIndex) { vector<int> res; if(rowIndex<0) return res; res.assign(rowIndex+1,0); for(int i=0;i<=rowIndex;i++){ if(i==0)原创 2017-12-14 20:40:55 · 90 阅读 · 0 评论 -
121. Best Time to Buy and Sell Stock
int maxProfit(vector<int>& prices) { int res=0,buy=INT_MAX; for(int price:prices){ buy=min(buy,price); res=max(res,price-buy); } return res;原创 2017-12-14 23:18:23 · 84 阅读 · 0 评论