Saturday, May 9, 2015

206. Reverse Linked List (easy)

Q:

Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
Follow up:
A linked list can be reversed either iteratively or recursively. Could you implement both?

A:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode oldPre(0);
        oldPre.next = head;
        
        ListNode newPre(0);
        while(oldPre.next)
        {
            auto tmp = oldPre.next;
            oldPre.next = tmp->next;
            
            tmp->next = newPre.next;
            newPre.next = tmp;
        }
        return newPre.next;
    }
};



**********Mistakes*************

ListNode *oldPre(0);  ---------> this grammar is WRONG

Tuesday, May 5, 2015

Addictive Number

Q:
Problem (Epic): Additive numbers are defined to be a positive integer whose digits form an additive sequence. E.g. 11235 (1+1=2, 1+2=3, 2+3=5). What makes it difficult is that 12,122,436 is also one (12+12=24, 12+24=36). Given a range of integers, find all the additive numbers in that range..


A:
自己的思路是先确定是几位的数字,例如从 1 到99999,我们就分别设置数组长为1 到5.

然后对那么长的数组,分别求addative number. 再检查是否within range 



XXX number  -------- sorry (I forget the name ):
find all XXX number within range [low high]
XXX number is defined as :
  each adjacent position will differ only 1.   for example 8798 is a XXX number,  890 is NOT.

permutaion II

Given a string, print all its permutations, (only lower case character can be permuted)


Contagious patient
就是一个party,互相握手。 有一个人开始有传染病。 最终多少人会有传染病?
输入是一个数值, 和一个2D array.

Saturday, May 2, 2015

Digit Counts

Q:
Count the number of k's between 0 and n. k can be 0 - 9.
Example
if n=12, in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], we have FIVE 1's (1, 10, 11, 12)

A:



Friday, May 1, 2015

205. Isomorphic Strings -E

Given two strings s and t, determine if they are isomorphic.
Two strings are isomorphic if the characters in s can be replaced to get t.
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.
Example 1:
Input: s = "egg", t = "add"
Output: true
Example 2:
Input: s = "foo", t = "bar"
Output: false
Example 3:
Input: s = "paper", t = "title"
Output: true
Note:
You may assume both and have the same length.
A:
就是两个一一映射的关系。
用2个mapping就行了

class Solution {
public:
    bool isIsomorphic(string s, string t) {
        return helper(s,t) && helper(t,s);
    }
private:
    bool helper(string s, string t)
    {
        map<char, char> mymap;
        for(int i =0;i<s.length();++i)
        {
            char ch1 = s[i];
            char ch2 = t[i];
            if(mymap.find(ch1)==mymap.end()) // first time find
            {
                mymap.insert({ch1,ch2});
            }else{
                if(mymap.find(ch1)->second != ch2)
                {
                    return false;
                }
            }
        }
        return true;
    }
};


通常的做法就是用2个map,这样就不用call 2 遍。

还有就是不用map, 直接用array, 用char 的值做index.