小屋創作

日誌2019-09-21 23:13

【C++】Vector and Map and Iterator and Two_Sum

作者:パっパっパスタ

vector
#include <iostream>
#include <vector>

using namespace std;

//vector:

int main(){
    vector<int> raw;
    raw.push_back(8);
    raw.push_back(7);
    raw.push_back(6);
    raw.push_back(3);
    for(vector<int>::iterator iter = raw.begin(); iter != raw.end(); iter++){
        cout << *iter << endl;

map
#include <iostream>
#include <map>
#include <string>

using namespace std;

int main(){
    map<string, string> hashmap;
    map<string, string>::iterator iter;
   
    hashmap.insert(pair<string, string>("r000", "ST1"));
    hashmap["r111"] = "ST2";
    hashmap["r222"] = "ST3";


    for(iter = hashmap.begin(); iter != hashmap.end(); ++iter){
        cout << iter->first << " , " << iter->second << endl;
    }

/*
r000 , ST1
r111 , ST2
r222 , ST3
*/


    iter = hashmap.find("r000");
    if(iter != hashmap.end()){
        cout << iter->second;
    }else{
        cout << "Not found!";
    }

//ST1

}

Two Sum

// Two Sum

#include <iostream>
#include <vector>
#include <map>

using namespace std;

class Solution{
public:
    vector<int> twosum(vector<int> &nums, int target){
        map <int, int> hashtable;            //store value and index
        map <int, int> :: iterator iter;     //iterator for find value
        vector <int> ansIndex(2);            //store index answer

        for(int i = 0; i < nums.size(); i++){
            iter = hashtable.find(target - nums[i]);

            if(iter != hashtable.end()){
                ansIndex[0] = iter->second;
                ansIndex[1] = i;
                return ansIndex;
            }else{
                //map only can change value, not key
                hashtable[nums[i]] = i;                
            }
        }
        cout << "Not found!" << endl;
    }
};

/*
    step by step:

    i->0 == 2
    hashtable.find(9 - 7 == 2) (X) #hashtable is clear
    hashtable[2](key) = 0(value)

    i->1 == 7
    hashtable.find(9 - 7 == 2) (O)    
    there is a '2' in hashtable, its value is 0!
    now i == 1 and iter is adress of '2' which in hashtable
    and iter->second is hashtable[2]'s value '0'
    so return index(0) and index(1)
*/


int main(){
    vector <int> arr;    //imput value
    arr.push_back(2);
    arr.push_back(7);
    arr.push_back(11);
    arr.push_back(15);
    int target = 9;

    Solution kkk;    //call function and print
    vector <int> point = kkk.twosum(arr , target);
    for(vector <int> :: iterator iter2 = point.begin(); iter2 != point.end(); iter2++){
        cout << "Index[ " << *iter2 << " ] " << endl;
    }
}

搞了三小時把vector map iterator還有最簡單的LeetCode第一題弄懂
我怕忘記所以先記錄起來
因為沒有每天認真寫題目思考
最近真的覺得邏輯思考能力真的在退化.....

4

4

LINE 分享

相關創作

我回了!!!

04/28 認識新朋友

《仙劍奇俠傳之揮劍問情》閒談-6

留言

開啟 APP

face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】