算法 day2
【leetcode】连续整数求和
class Solution { public: int sum1(int k) { return k*(k+1)/2; } int consecutiveNumbersSum(int N) { int ccount = 0; int k=1; while(sum1(k) <= N) { if((N-sum1(k))%k == 0) { ccount++; } k++; } return ccount; } };
【剑指 Offer 】53 - II. 0~n-1中缺失的数字
class Solution { public: int missingNumber(vector<int>& nums) { if(nums[0]!=0) return 0; for(int i=0;i<nums.size()-1;i++) { if(nums[i]+1!=nums[i+1]) { return nums[i]+1; } } return nums[nums.size()-1]+1; } };
【leetcode】判断是否有环
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: bool hasCycle(ListNode *head) { ListNode* slow = head; ListNode* fast = head; while(slow!=NULL && fast!=NULL) { slow = slow->next; fast = fast->next; if(fast == NULL) break; fast = fast->next; if(slow == fast) return true; } return false; } };
【leetcode】最接近的三数之和
class Solution { public: int threeSumClosest(vector<int>& nums, int target) { sort(nums.begin(),nums.end()); int left,right; int len = nums.size(); //[a,b) int a1; int min_delta = INT_MAX; for(int i=0;i<len-2;i++) { left = i+1; right = len-1; int sum; int ans = target - nums[i]; while(left<right) { sum = nums[left] + nums[right]; if(abs(ans - sum)<abs(min_delta)) { min_delta = ans - sum; a1 = sum + nums[i]; } if(sum < ans) { left++; } else if(sum > ans) { right--; } else if(sum == ans) { return target; } } } return a1; } };
【leetcode】盛最多水的容器
class Solution { public: int maxArea(vector<int>& height) { int maxv = 0; int left,right; left = 0; right = height.size()-1; while(left<right) { int nowv = (right-left)*min(height[right],height[left]); if(nowv>maxv) maxv = nowv; if(height[right]>height[left]) { left++; } else right--; } return maxv; } };
【leetcode】删除链表的倒数第N个节点
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* removeNthFromEnd(ListNode* head, int n) { ListNode* pre; ListNode* right = head; ListNode* left = head; while(n--) {right = right->next;} if(right == NULL) { return head->next; } while(right!=NULL) { if(right->next == NULL) pre = left; left = left->next; right = right->next; } pre->next = left->next; return head; } };
【leetcode】回文串
class Solution { public: bool isPalindrome(int x) { char str[10000]; sprintf(str,"%d",x); int left ,right; left = 0; right = strlen(str)-1; while(left<right) { if(str[left] == str[right]) { left++; right--; } else return false; } return true; } };
【leetcode】最长不重复字符串
class Solution { public: int lengthOfLongestSubstring(string s) { map<char,int> hashmap; int left = 0; int right = 0; int ccount = 0; int max = 0; while(left<s.length()) { while((hashmap.find(s[right])==hashmap.end()||hashmap[s[right]]==0)&&right<s.length()) { hashmap[s[right]] = 1; right++; printf("%d\n",right); } ccount = right-left; if(max<ccount)max = ccount; hashmap[s[left]]--; left++; printf("%c%d %c%d \n",s[left],left,s[right],right); } return max; } };
【leetcode】括号匹配
最简单的栈简单题
class Solution { public: bool isValid(string s) { char stack[10000]; int p=0; for(int i=0;i<s.length();i++) { if(s[i] == '('||s[i] == '['||s[i] == '{') { stack[p++] = s[i]; } else if(p==0) { return false; } else if(s[i] == ')') { if(stack[p-1]!='(') { return false; } else { p--; } } else if(s[i] == ']') { if(stack[p-1]!='[') { return false; } else { p--; } } else if(s[i] == '}') { if(stack[p-1]!='{') { return false; } else { p--; } } } if(p!=0)return false; return true; } };
要注意判断当有输入时,栈是不是为空