C++ learning of leetcode (Difficulty: easy)
【String】
"found" can get needle “index” in haystack string:
(int type of found also can be used.)
size_t found = haystack.find(needle);
Use pointer to check char in string:
for(auto it=s.begin(); it!=s.end(); ++it){
if (*it == ' '){...
length() for how many char of string:
i=s.length()-1;
Let different string equal length:
if (a.length() > b.length()) swap(a,b);
diff2= b.length() - a.length();
while(diff2--){
a= '0' + a;
}
Remove non-alphanumeric characters from “s”:
s.erase(remove_if(s.begin(), s.end(), [](char c) { return !isalpha(c); } ), s.end());
Reverse string:
reverse(s.begin(), s.end());
Insert char(ascii code) to the string: (refer to ASCII table)
ans2.insert(0, string(1, char(26+64)));
string= (to_string(int1) + " " + to_string(int2));
【Char】
Use toupper()、tolower() transfor Upper Case and Lower Case
Use remove_if() remove specific char, e.g. "iseven", "isodd", "isalnum"
【Vector】
vector<int>::iterator it;
for (it = digits.end() - 1; it >= digits.begin() && *it == 9; *(it--) = 0);
=> the last number will turn into 0, when the last number is 9,
until the last number not 9.
Some method of push, pop, top, min.
digits.insert(digits.begin(), 1); // insert 1 in front of digits.
digits.erase(digits.begin(), 1); // erase from 0 to 1, should be used point to erase.
For each:
for (int i : prices){ //for each
bestBuy= min(i, bestBuy);
ans2= max(ans2, i-bestBuy);
}
Hash Table [O(n)]
Count the number of appearances for each distinct number in nums
, once we see a number appear more than n / 2
times, it is the majority element.
unordered_map<int, int> counter;
for (int num : nums) {
if (++counter[num] > nums.size() / 2) {
return num;
}
}
Sorting [O(logn)]
Since the majority element appears more than n / 2
times, the n / 2
-th element in the sorted nums
must be the majority element. In this case, a partial sort by nth_element
is enough.
nth_element(nums.begin(), nums.begin() + nums.size() / 2, nums.end());
return nums[nums.size() / 2];
【Stack】
FIFO example (first in first out)
【ListNode】
ListNode *temp2= new ListNode();
ListNode *ans2= head;
head= head->next;
head before: -A-B-C-D-
^
head after:-A-B-C-D-
^
head->next= head->next->next;
head before: -A-B-C-D-
^
head after:-A-C-D-
^
Use recursive to remove the element.
ListNode* removeElements(ListNode* head, int val) {
if (!head) return nullptr;
if (head->val == val) return removeElements(head->next, val);
head->next= removeElements(head->next, val);
return head;
}
std::unordered_set<int> unordered_set; // int can change to other type, e.g. Vector, ListNode
if want to write the capacitor:
unordered_set.insert(1);
if want to read the capacitor:
for (const auto &s : myunordered_set) {
std::cout << s << " ";
}
if want to count elements with a specific key(.count): <1>
【Debugging】
if (debug){cout << s[i] << '-';}
【Sub function】
Vector to large integer:
int vec2num(int i, int times, vector<int>& vec){
if (i >= 0){return vec[i]*times+ vec2num(i-1, times*10, vec);}
else{return 0;}
}
Bit to large integer:
unsigned long int bit2num(int i, int times, string bin){
if (i >= 0){return (bin[i]- 48)*times+ bit2num(i-1, times*2, bin);}
else{return 0;}
}
Vector upside down:
for (int y=x-1; y>=0; y--){ // upside down temp2 instead pop_front
ans2.push_back(temp2[y]);
}
【Method】
Binary Search: If the array of numbers is in non-decreasing order. example
留言列表