摘要:这题还蛮有意思的,看了下解析,分成两部分分开来求解。 左右边界都是普通的二分查找算法,重点就是当等于的时候的处理,左边界函数等于目标值的时候,要记录当前mid的值作为边界,同时区间要向左移。 反过来,右边界的话,区间要向右移动。记得记录相等时候的mid值,最后一次相等记录的值也就是这个边界值。 cl
阅读全文
摘要:这题也是利用二分查找来计算。 首先区间是[0,x]。通过x/2的平方判断应该区间左移还是区间右移,同时值得注意的是要记录mid的平方小于x的mid值,因为比如8的平方根是2,所以平方小于x的mid要记录。 class Solution { public: int mySqrt(int x) { in
阅读全文
摘要:class Solution { public: bool isPerfectSquare(int num) { int left = 0, right = num; while(left <= right){ int mid = left + (right - left) / 2; if(pow(
阅读全文
摘要:像个漏斗一样把元素筛出来就好了。 class Solution { public: int removeElement(vector<int>& nums, int val) { int head = 0, curIndex = 0; for(; curIndex < nums.size(); ++
阅读全文
摘要:class Solution { public: int removeDuplicates(vector<int>& nums) { int head = 0, curIndex = 0, target = INT_MIN; for(; curIndex < nums.size(); ++curIn
阅读全文
摘要:class Solution { public: void moveZeroes(vector<int>& nums) { int head = 0, curIndex = 0; for(; curIndex < nums.size(); ++curIndex){ if(nums[curIndex]
阅读全文
摘要:c++字符串还是不太熟练 class Solution { public: bool backspaceCompare(string s, string t) { return dealString(s) == dealString(t); } private: string dealString(
阅读全文
摘要:sort 一下 class Solution { public: vector<int> sortedSquares(vector<int>& nums) { vector<int> ret; for(int num : nums){ ret.push_back(pow(num, 2)); } so
阅读全文
摘要:滑动窗口!! class Solution { public: int minSubArrayLen(int target, vector<int>& nums) { int left = 0, right = 0, sum = nums[0]; int minLength = INT_MAX; w
阅读全文
摘要:改不了C的思维 class Solution { public: int totalFruit(vector<int>& fruits) { if(fruits.size() <= 2) return fruits.size(); int left = 0, right = 0, curNum =
阅读全文
摘要:跟着别人的代码履了一遍,明天自己再重写遍。 class Solution { public: map<char, int> tstr, sstr; bool isContained(){ for(auto tchar : tstr){ if(tchar.second > sstr[tchar.fir
阅读全文
摘要:class Solution { public: map<char, int> maps, mapt; bool isContained(){ for(pair<char, int> elem : mapt){ if(elem.second > maps[elem.first]) return fa
阅读全文
摘要:不知道一年后会成长成什么样,只感觉好难好难。有好多东西要学,源码也看不懂,项目也不会做。 class Solution { public: vector<vector<int>> generateMatrix(int n) { vector<vector<int>> vec(n, vector<in
阅读全文
摘要:中秋节摆了一天,感觉畏难情绪一直困扰着我,要好好调制状态才行。 #include<iostream> #include<vector> using namespace std; int main(){ int n = 0; cin >> n; vector<int> sum(n, 0); for(i
阅读全文
摘要:\ 路长且艰,任重而道远,什么时候才能成长成真正的程序员呢 #include<iostream> #include<vector> #include<climits> using namespace std; int main(){ int n, m; cin >> n >> m; vector<v
阅读全文
摘要:学习了C++ stack的使用 class Solution { public: #include<stack> bool isValid(string s) { stack<char> stk; for(int i = 0; i < s.size(); ++i){ if(s[i] == '(' |
阅读全文
摘要:\ class LRUCache { public: LRUCache(int capacity) :_capacity(capacity) {} int get(int key) { auto it = _unorderedMap.find(key); if(it != _unorderedMap
阅读全文
摘要:老题新做 class Solution { public: ListNode* removeElements(ListNode* head, int val) { if(!head) return nullptr; if(head->val != val){ head->next = removeE
阅读全文
摘要:心要细,这题才能作对。 class MyLinkedList { public: MyLinkedList() :_size(0), _head(new listedNode(0)) {} int get(int index) { if(index < 0 || index >= _size) re
阅读全文
摘要:class Solution { public: ListNode* reverseList(ListNode* head) { if(!head) return nullptr; ListNode* cur = head->next; if(!cur) return head; head->nex
阅读全文
摘要:感觉自己畏难情绪好严重呀,一点都不想学新东西。 class Solution { public: ListNode* swapPairs(ListNode* head) { if(!head) return nullptr; if(!head->next) return head; ListNode
阅读全文
摘要:相当于删除正数第n个节点 class Solution { public: ListNode* removeNthFromEnd(ListNode* head, int n) { if(!head) return head; int listLength = 0; ListNode* temp =
阅读全文
摘要:class Solution { public: ListNode* removeNthFromEnd(ListNode* head, int n) { if(!head) return head; ListNode* dummyHead = new ListNode(0, head); ListN
阅读全文
摘要:明天回家喽,最近在学习的瓶颈期,感觉学的东西好难,有厌学的心理,但是我相信过了这段煎熬的时期,就好了。 class Solution { public: ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { int na
阅读全文
摘要:class Solution { public: ListNode *detectCycle(ListNode *head) { ListNode* fast = head; ListNode* slow = head; while(fast && fast->next){ fast = fast-
阅读全文
摘要:class Solution { public: bool isAnagram(string s, string t) { if(s.size() != t.size()) return false; for(int i = 0; i < s.size(); ++i) ++maps[s[i]]; f
阅读全文
摘要:class Solution { public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { unordered_set<int> ans_set; unordered_set<int> num1_set(nu
阅读全文
摘要:预计再过半个月就可以写项目了,不知道像我这样的彩笔,能不能驾驭得住项目难度,希望可以随便拿捏。 class Solution { public: bool isHappy(int n) { unordered_set<int> unset; while(1){ int sum = getNextNu
阅读全文
摘要:class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { unordered_map<int, int> unmap; for(int i = 0; i < nums.size(); ++i){ unma
阅读全文
摘要:class Solution { public: int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) { int ans = 0; unordered_map
阅读全文
摘要:class Solution { public: bool canConstruct(string ransomNote, string magazine) { unordered_map<char, int> unmapran; unordered_map<char, int> unmapmag;
阅读全文
摘要:practise makes perfect! 相信自己的努力不会白费,继续学习吧 class Solution { public: vector<vector<int>> threeSum(vector<int>& nums) { vector<vector<int>> ans; sort(num
阅读全文
摘要:class Solution { public: vector<vector<int>> fourSum(vector<int>& nums, int target) { vector<vector<int>> ans; sort(nums.begin(), nums.end()); long su
阅读全文
摘要:class Solution { public: void reverseString(vector<char>& s) { int head = 0, tail = s.size() -1; while(head < tail){ char temp = s[head]; s[head] = s[
阅读全文
摘要:class Solution { public: string reverseStr(string s, int k) { for(int i = 0; i < s.size(); i += 2*k){ if(s.size() - i >= k){ reverse(s, i, i + k -1);
阅读全文
摘要:#include <iostream> #include <string> using std::cin; using std::cout; using std::endl; using std::string; int main(){ int k; string s; cin >> k; cin
阅读全文
摘要:决定转前端了。先用c++刷题刷着先 class Solution { public: int strStr(string haystack, string needle) { if(haystack.size() < needle.size()) return -1; for(int i = 0;
阅读全文
摘要:KMP算法一点都不想回忆了,等找实习的时候再仔细看吧 class Solution { public: bool repeatedSubstringPattern(string s) { if(s.size() <= 1) return false; string temp = "", str =
阅读全文
摘要:class MyQueue { public: MyQueue() { } void push(int x) { s1.push(x); } int pop() { int ret; if(!empty()){ if(!s2.empty()){ ret = s2.top(); s2.pop(); r
阅读全文
摘要:class MyStack { public: MyStack() : q1(queue<int>()), q2(queue<int>()) { } void push(int x) { q1.push(x); } int pop() { int ret; if(q1.size() == 1){ r
阅读全文
摘要:class Solution { public: bool isValid(string s) { int r1 = 0, r2 = 0, r3 = 0; char temp; stack<char> stk; for(int i = 0; i < s.size(); ++i){ if(s[i] =
阅读全文