1. 程式人生 > 資訊 >TCL 科技 2021 年營收 1635.28 億元,淨利潤逾 100.62 億元同比增長 129.3%

TCL 科技 2021 年營收 1635.28 億元,淨利潤逾 100.62 億元同比增長 129.3%

1. 二分查詢

對於數字運算,左移一位相當於乘以2,兩位相當於乘以4,依次下去,右移相反是除以

 public static int binarySearch(int[]A,int value) {
        int low = 0;
        int len = A.length;
        int high = len -1;
​
        while(low <= high) {
            int mid = (low + high)>>1;
            if(A[mid] == value)
                
return mid; else if (A[mid] < value) low = mid +1; else high = mid -1; } return -1; }

二分查詢易錯的地方

  • 迴圈退出的條件

注意是low <= high,而不是low<high.

  • mid的取值

如果寫成 mid (low+high)/2是有問題,如果low和high比較大,兩者的和可能會溢位改進的方案low+(high-low)/2,但是計算機位運算比除法快,故可改成low+((high-low)>>1)

2. reverse(result.begin(),result.end())逆序

vector<int>(result.rbegin(),result.rend());逆序

 

3.find() 函式

本質上是一個模板函式,用於在指定範圍內查詢和目標元素值相等的第一個元素。

另外,該函式會返回一個輸入迭代器,當 find() 函式查詢成功時,其指向的是在 [first, last) 區域內查詢到的第一個目標元素;如果查詢失敗,則該迭代器的指向和 last 相同。

 

4. distance() 函式用於計算兩個迭代器表示的範圍內包含元素的個數,返回個數

 

5.c++在string末尾新增字元或字串

append函式

#include<iostream>
using namespace std;
int main()
{
    string s1 = "1";
    string s2 = "2";
    string s3 = "0123456";
    s1.append(s2);
    cout<<s1<<endl;
    s1.append(s3,3,6);
    cout<<s1<<endl;
    s1.append(5,'0');
    cout<<s1<<endl;
}
//12
//123456
//12345600000 

 6.begin()和end()獲取指標

 

7.重建二叉樹(遞迴)

1、力扣上的一種解法

題目描述

好題 絕對的好題

輸入某二叉樹的前序遍歷和中序遍歷的結果,請重建出該二叉樹。假設輸入的前序遍歷和中序遍歷的結果中都不含重複的數字。例如輸入前序遍歷序列{1,2,4,7,3,5,6,8}和中序遍歷序列{4,7,2,1,5,3,8,6},則重建二叉樹並返回。

需要首先熟悉二叉樹先序遍歷與中序遍歷的規則。 先找到preorder中的起始元素作為根節點,在inorder中找到根節點的索引mid;那麼,preorder[1:mid + 1]為左子樹,preorder[mid + 1:]為右子樹;inorder[0:mid]為左子樹,inorder[mid + 1:]為右子樹。遞迴建立二叉樹。

TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {
         if (pre.size() == 0 || vin.size() == 0) {
            return NULL;
        }
        TreeNode* treeNode = new TreeNode(pre[0]);
        int mid = distance(begin(vin), find(vin.begin(), vin.end(), pre[0]));
        vector<int> left_pre(pre.begin() + 1, pre.begin() + mid + 1);
        vector<int> right_pre(pre.begin() + mid + 1, pre.end());
        vector<int> left_in(vin.begin(), vin.begin() + mid);
        vector<int> right_in(vin.begin() + mid + 1, vin.end());

        treeNode->left = reConstructBinaryTree(left_pre, left_in);
        treeNode->right = reConstructBinaryTree(right_pre, right_in);
        return treeNode;
    }

8. bitset用法

主要是將 n 轉化為 32位表示,int 最大也就是 2^32次方,然後利用bitset。count()函式,返回 其中 1 的數量

bitset<4> bitset1;  //無參構造,長度為4,預設每一位為0

 

bitset<8> bitset2(12);  //長度為8,二進位制儲存,前面用0補充

string s = "100101";
bitset<10> bitset3(s);  //長度為10,前面用0補充

char s2[] = "10101";
bitset<13> bitset4(s2);  //長度為13,前面用0補充

cout << bitset1 << endl;  //0000
cout << bitset2 << endl;  //00001100
cout << bitset3 << endl;  //0000100101
cout << bitset4 << endl;  //0000000010101

bitset<8> foo ("10011011");

cout << foo.count() << endl;  //5  (count函式用來求bitset中1的位數,foo中共有5個1
cout << foo.size() << endl;   //8  (size函式用來求bitset的大小,一共有8位

cout << foo.test(0) << endl;  //true  (test函式用來查下標處的元素是0還是1,並返回false或true,此處foo[0]為1,返回true
cout << foo.test(2) << endl;  //false  (同理,foo[2]為0,返回false

cout << foo.any() << endl;  //true  (any函式檢查bitset中是否有1
cout << foo.none() << endl;  //false  (none函式檢查bitset中是否沒有1
cout << foo.all() << endl;  //false  (all函式檢查bitset中是全部為1