顯示具有 LeetCode 標籤的文章。 顯示所有文章
顯示具有 LeetCode 標籤的文章。 顯示所有文章

2017年7月7日 星期五

Roman to Integer

The submission can not too complex.
var RomanNumber = map[rune]int {
    'I': 1,
    'V': 5,
    'X': 10,
    'L': 50,
    'C': 100,
    'D': 500, 
    'M': 1000,
}

func romanToInt(s string) int {
    r := []rune(s)
    result := 0
    for i :=0; i<len(r); i++ {  
        if i>0 && RomanNumber[r[i]] > RomanNumber[r[i-1]] {  
            result += RomanNumber[r[i]] - 2*RomanNumber[r[i-1]]
        } else {
            result += RomanNumber[r[i]]
        }  
    }  
    return result
}
Overview this solution. It just use a map to looking for number.
The only thing need explanation is 
if i>0 && RomanNumber[r[i]] > RomanNumber[r[i-1]] {  
    result += RomanNumber[r[i]] - 2*RomanNumber[r[i-1]]
}
Take a example
Roman: IV r[0] = I = 1, r[1] = V = 5
We know that is 4, 5 - 1. Ok, why -2*1? Because we need to remove prev round side effect.
overhead r[0] = I = 1 -> r[1] = V = 5
state          0 += 1 = 1 ->  1 += 5 - 2*1 = 4

That's all.
          

2017年5月18日 星期四

Remove Element

Leetcode easy題(嗯,真的很easy)
class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        while( haveVal(nums, val) ) {
            nums.erase(std::find(nums.begin(), nums.end(), val));
        }
        return nums.size();
    }
private:
    bool haveVal(vector<int> nums, int val) {
        for(int i=0; i<nums.size(); i++) {
            if(nums.at(i) == val) return true;
        }
        return false;
    }
};
重點在善用STL幫助你省事,畢竟C++的各個容器都並非非常完整、操作簡單的那種東西
相反的它非常鼓勵各個組件組合應用這種形式
By the way,Algorithm中的幾個方法還不如像這樣自行實現方便,因為他們被設計成適合解泛化問題的形式

ps. Python只要這樣寫就行了,選錯工具,誤你一生(誤www)
def removeElement(list, val):
    while val in list:
        list.remove(val)
    return len(list)

這則是利用了find的版本,可以看得出來有些冗長
public:
    int removeElement(vector<int>& nums, int val) {
        while(std::find(nums.begin(), nums.end(), val) != nums.end()) {
            nums.erase(std::find(nums.begin(), nums.end(), val));
        }
        return nums.size();
    }

2017年3月1日 星期三

將串列中的值兩兩交換

function ListNode(val) {
    this.val = val;
    this.next = null;
    this.forEach = function(fun) {
        var now = this;
        fun(now.val);
        //  loop version
//        while(now.next !== null) {
//            fun(now.next.val);
//            now = now.next;
//        }
        //  recrsion version
        if(now.next !== null) {
            now.next.forEach(fun);
        }
    };
}

const head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(3);
head.next.next.next = new ListNode(4);
head.next.next.next.next = new ListNode(5);

head.forEach(console.log);

var swapPairs = function(head) {
    now = head;
    while (now.next !== null) {
        var temp = now.val;
        now.val = now.next.val;
        now.next.val = temp;
        now = now.next.next;
    }
    return head;
};
console.log("");
const result = swapPairs(head);

result.forEach(console.log);

這是leetcode的swap pairs,雖然不知道為啥他一直說不能存取now.next,如果有人能試成功可以告訴我
原版並沒有forEach,這是我加的,可以看到它有兩個版本的實作方式

初始化串列之後,先印出整個串列
接著swapPairs head,然後把改完的串列再傳給自己,這樣不太好,不過我是要測試能不能有副作用所以這樣寫,比較好的做法是const head,由於JS的const只保證指標不變,所以對const物件的prop改變是可以的,所以可以做成
const result = swapPairs(head);
result.forEach(console.log);
的樣子