Friday, April 5, 2013

Day 11, 14,19,20, Longest Common Prefix, Remove Nth Node From End of List, Valid Parentheses

Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.  
---------
 grab the first string in array, compare its i-th element to other strings' i-th element

Is there a better solution?
 
class Solution {
public:
    string longestCommonPrefix(vector<string> &strs) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (strs.size() == 0) {
            return "";
        }
        bool flag = false; // to break the outer for loop
        string prefix = "";
        for (int i=0;i<strs[0].length();i++) {
            for (int j=1;j<strs.size();j++) {
                if (strs[j] == "" || strs[0][i] != strs[j][i]) {
                    // to break the outer for loop
                    flag = true;
                    break;
                }
            }
            
            if (flag) break;
            prefix = prefix + strs[0][i];
        }
        return prefix; 
    }
}; 
Added on Sep-02-2014
Instead of using first string, use 26 alphabet letters to check each string

Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head.
For example,
   Given linked list: 1->2->3->4->5, and n = 2.

   After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given n will always be valid.
Try to do this in one pass.
----------
two pointers
 
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode *pre = new ListNode(0), *dummy = pre;
        pre->next = head;
        int k = 0;
        while (head != NULL) {
            head = head->next;
            k++;
        }
        k = k - n;
        while (k > 0) {
            k--;
            pre = pre->next;
        }
    
        pre->next = pre->next->next;
        return dummy->next;
    }
};
Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
---------------------
use a stack 
class Solution {
public:
    bool isValid(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        stack<char> st;
        for (int i=0;i<s.length();i++) {
            if (s[i] == '(' || s[i] == '[' || s[i] == '{') {
                st.push(s[i]);
            }else {
                if (st.empty()) {
                    return false;
                }
                char c = st.top();
                st.pop();
                if ((s[i] == ')' && c == '(') || (s[i] == '}' && c == '{') || (s[i] == ']' && c == '[')) {
                    ;
                }else return false;
            }
        }
        if (st.empty()) {
            return true;
        }
        return false;
    }
};

No comments:

Post a Comment