【Code】cpp不定時更新
阿新 • • 發佈:2018-11-09
1. Jewels and Stones
- String的大小和訪問string中的第i個char: s.size() ; s[i] . 其中s為string
- sort函式的用法:
- 必須宣告標頭檔案#include <algorithm>
- 三個input,第一個是排序的第一個元素的地址,第二個是排序的第二個的地址,第三個是排序方法 -- 通過bool的一個函式確定是從大到小還是從小到大。sort函式每次會把兩個數值放進這個bool函式進去比大小。
- bool函式中寫成const能避免不小心改動了a和b的值。 &在這裡表引用。
#include <iostream> #include <vector> #include <algorithm> using namespace std; bool cmp(const int &a, const int &b){ return a<b; } int main() { vector<int> myvec{1,2,7,6,1,7}; sort(myvec.begin(), myvec.end(), cmp); cout << "The order is:" << endl; for (int item:myvec) cout << item << ' '; cout << endl; return 0; }
- Lambda 表示式 -- C++11的新功能
- [capture list] (params list) mutable exception -> return type {function body}
- 注意單純捕獲一個值a在lambda裡面也無法改變它的值,除非引用它(&a),或者加上mutable
- https://www.cnblogs.com/DswCnblog/p/5629165.html
sort(myvec2.begin(), myvec2.end(), [](int a, int b) -> bool { return a < b; });
- vector的用法
- 要包括標頭檔案#include <vector>
- https://www.cnblogs.com/youyoui/p/5779965.html
- insert的用法: http://www.cplusplus.com/reference/vector/vector/insert/
myvec.push_back(3); myvec.pop_back();
- 幾種常見容器
- list支援快速的插入和刪除,但查詢費時;vector支援快速查詢,但插入費時;map查詢的時間複雜度是對數,最快。hash也是對數。
- Hash表
- 二叉樹和紅黑樹
- 二叉樹:對比於有序陣列,便於插入新的資料項;對比於連結串列,便於查詢資料項。
- https://blog.csdn.net/eson_15/article/details/51138663
- 紅黑樹: http://www.cnblogs.com/skywang12345/p/3245399.html
- set和unordered_set
- unordered_set 的底層是hashtable,所以是無序的;set 的底層是RB-Tree,所以是有序的。
- https://blog.csdn.net/zhangxiao93/article/details/74357101
- count函式
- 返回統計容器中等於value的元素的個數
- https://blog.csdn.net/lyj2014211626/article/details/69615514
- 更好的解決方法:
2. To lower Case
3. Big Countries
- SQL的題目。用UNION兩個索引同時進行,所以會比直接用OR快一些
- http://www.bubuko.com/infodetail-2253174.html
- SQL教程:http://www.w3school.com.cn/sql/index.asp
4. Unique Morse Code Words
- set用於儲存不同的transformations.
5. Hamming Distance
- vector的幾種初始化方式https://blog.csdn.net/yjunyu/article/details/77728410?locationNum=10&fps=1
- 位運算https://blog.csdn.net/fox64194167/article/details/20692645
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<int> v(32);
for(vector<int>::iterator i = v.begin();i!= v.end();++i)
{
cout << *i << " ";
}
cout << endl;
return 0 ;
}
6. Flipping an Image
- 遍歷方法:for(vector<int> &vi:A) - vi為A中每項的遍歷 https://zhidao.baidu.com/question/303137003.html
- 將vector vi反轉: reverse(vi.begin(),vi.end());
- i^=1; - 對於i取反 (相當於if (i==0) {i = 1;} else {i=0;})
- 更好的解法:https://www.cnblogs.com/zhuangbijingdeboke/p/9192104.html
7. Judge Route Circle
8. Projection Area of 3D Shapes
- vector賦值
vector< vector<int> > grid(2, vector<int>(2));
int b[2][2]={{1,2},{3,4}};
for(int i=0; i<2;i++){
for(int j=0;j<2;j++){
grid[i][j] = b[i][j];
}
}
9. Merge Two Binary Trees
- TreeNode *newNode = new TreeNode(t1->val + t2->val);
10. Self Dividing Numbers
- 關鍵在於求餘後判斷是否餘數能整除
11. Array Partition 1
- 要使min的和最大就要按從小到大的順序排列後兩兩作為一對,取前一個數字。(否則如果不這樣取,就會導致大數被浪費)
- 第一個解法,直接sort完隔一個數取值相加
- 第二個解法把數值對映到一個reference table中,記錄每個數出現的次數;然後從小到大通過flag來決定是要加還是不管。
12. Middle of the Linked List
- 關鍵在於設定兩個指標同時跑,p2每次跑兩格,p1每次跑一格,所以當p2跑完的時候p1就剛好在一半的位置。
13. Transpose Matrix
- 建一個新的向量矩陣,然後把A中的值一一拷貝到B內
14. Leaf-Similar Trees
- 關鍵在於寫一個函式來收集全部的葉子 - 遞迴的方法,終止條件是左右指標都指向nullptr,然後每次都先找左邊後找右邊
- nullptr vs null: null是舊的已經被棄用的,它會被當作一個int;而nullptr是新的,它還是會被當成一個pointer。
- 樹節點和一個vector被當作input放進函式裡面的時候的表達方式如下,此時呼叫函式的時候直接輸入樹根和vector的名字就好,在函式裡面也一樣。
void leaves(TreeNode* root, vector<int>& leaf)
15. Number Complement
- ******位運算:https://blog.csdn.net/morewindows/article/details/7354571
- 更好的解法: https://www.cnblogs.com/grandyang/p/6275742.html
16. Uncommon Words
- unordered_map<string, int> 可以用來統計每個詞出現的數量
- 用iter來做map的for迴圈
- istringstream的用法:https://blog.csdn.net/longzaitianya1989/article/details/52909786
class Solution {
public:
vector<string> uncommonFromSentences(string A, string B) {
istringstream wrdA(A);
istringstream wrdB(B);
vector<string> output;
unordered_map<string, int> words;
string word;
while(wrdA>>word){
words[word]++;
}
while(wrdB>>word){
words[word]++;
}
for(auto iter = words.begin(); iter!=words.end();iter++){
if(iter->second==1){
output.push_back(iter->first);
}
}
return output;
}
};
17. Reverse Words in a String |||
- istringstream讀入每一個數,然後用reverse(tmp.begin(), tmp.end())實現反轉,再重新存入一個string型別的result