Friday, May 3, 2013

Day 24, 125, 129, Valid Palindrome, Sum Root to Leaf Numbers

Best Time to Buy and Sell Stock
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
"A man, a plan, a canal: Panama" is a palindrome.
"race a car" is not a palindrome.
Note:
Have you consider that the string might be empty? This is a good question to ask during an interview.
For the purpose of this problem, we define empty string as valid palindrome.
-----------------------------------------------------------
class Solution {
public:
    bool isPalindrome(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        string str="";
        for (int i=0;i<s.length();i++) {
            if (isalnum(s[i])) {
                str += tolower(s[i]);
            }
        }
        int length = str.length();
        for (int i=0;i<length/2;i++) {
            if (str[i] != str[length - i-1]) {
                return false;
            }
        }
        return true;
    }
};
Update on Jan-16-2015
constant space
class Solution {
public:
    bool isPalindrome(string s) {
        int left = 0;
        int right = s.length();
        
        while (left < right) {
            if (isalnum(s[left]) && isalnum(s[right])) {
                if (s[left] == s[right] 
                    || s[left] + 32 == s[right] 
                    || s[left] == s[right] + 32) {
                    left++;
                    right--;
                }else {
                    return false;
                }
                
            }else if (!isalnum(s[left])) {
                left++;
            }else if (!isalnum(s[right])) {
                right--;
            }
        }
        return true;
    }
};

Sum Root to Leaf Numbers
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path 1->2->3 which represents the number 123.
Find the total sum of all root-to-leaf numbers.
For example,
    1
   / \
  2   3
The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.
Return the sum = 12 + 13 = 25.
 -----------------------------------------------------------------
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    void leef (TreeNode *root, int pathSum, int & sum) {
        int sumL = pathSum, sumR = pathSum;
        if (root->left != NULL) {
            sumL = sumL*10 + root->left->val;
            leef(root->left,sumL,sum);
        }
        if (root->right != NULL) {
            sumR = sumR*10 + root->right->val;
            leef(root->right,sumR,sum);
        }
        if (root->left == NULL && root->right == NULL) {
            sum += pathSum;
        }
    }

    int sumNumbers(TreeNode *root) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (root == NULL) return 0;
        int sum=0;
        leef(root,root->val,sum);
        return sum;
    }
};

No comments:

Post a Comment