2018年3月28日 星期三

24. Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head.
For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.
Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.

題意很簡單,就是遇到第偶數個Node就跟上一個交換


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        
        ListNode* dummy = new ListNode(0);
        ListNode* pre = dummy;
        int count = 1;
        
        dummy->next = head;
                    
        while (head != nullptr)
        {
            //偶數時交換
            if (count % 2 == 0)
            {
                swap(pre->val, head->val);
            }
            
            pre = head;
            head = head->next;
            count++;
        }
        
        head = dummy->next;
        delete dummy;
        return head;
    }
};

2018年3月20日 星期二

83. Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.

這一題的邏輯非常簡單
重複的就跳過就是如此


class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
       
        ListNode* ans = head;
        
        while(head != nullptr && head->next != nullptr)
        {
            if(head->val == head->next->val)
            {
                head->next = head->next->next;
            }
            else
            {
                head = head->next;
            }
        }
        
        return ans;
    }
};

2018年3月7日 星期三

列舉元素組合

最近在研究iota,發現他是使用平衡三進制{-1,0,1}的方式。所以在寫code時,必須將二進制轉換成三進制。

而tryte就是三進制的3次方,地位等同於二進制中的byte。 我們把它想像成三進制中的程式變數基本單位好了。程式上的變數通常也是用幾個byte幾個byte來表達。

當二進制轉換成三進制必須檢查是否符合tryte的合法元素。
但是哪些元素是符合的。我這邊懶得自己列舉,就交給程式來跑

程式碼如下


#include <vector>

using namespace std;

void find_element_set(const vector<int> &elements, vector<int> &element_collector,vector<vector<int>> &elements_set)
{
    if (element_collector.size() == 3)
    {
        elements_set.push_back(element_collector);
        return;
    }

    for(int i = 0; i < elements.size(); i++)
    {
        element_collector.push_back(elements[i]);
        find_element_set(elements, element_collector, elements_set);
        element_collector.pop_back();
    }
}

int main()
{
    vector<vector<int>> element_set;
    vector<int> elements{0,1,-1}, element_collector;

    find_element_set(elements, element_collector, element_set);

    return 0;
}

2018年1月13日 星期六

3. Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters.

Examples:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

解法
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        
        // 用一個hash table來紀錄有重複字元出現在array的第幾個element
        unordered_map<char, int> charmap;
        // 最大的不重複字元substring長度跟每次要去定位每一個substring的endflag
        int maxlength = 0, flag = -1;
        
        for (int i = 0; i < s.size(); i++)
        {
            // 一開始發現s[i]不出現
            if (charmap.count(s[i]) > 0)
            {
                //比大小更新end
                flag = max(charmap[s[i]], flag);
            }
            
            //標記它已經出現過了
            charmap[s[i]] = i;
            
            //更新目前為止最大的不重複字元substring長度
            maxlength = max(maxlength,(i - flag));
        }
        
        return maxlength;
        
    }
};