Friday, March 15, 2013

Day 2 leetcode 58, 66, 83

Length of Last Word
Solution#1
int lengthOfLastWord(const char *s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int length=0;
        if (*s== NULL) return length;
        while (*s != NULL) {
            length=0;
            while (isalpha(*s)) {
                *s++;
                length++;
            }
            while (*s == ' ') 
                *s++;
        }
        return length;
}
Solution#2
class Solution {
public:
    int lengthOfLastWord(const char *s) {
        bool inAWord = false;
        int length = 0;
        int itr = 0;
        while (s[itr] != NULL) {
            if (isalpha(s[itr])) {
                if (!inAWord) length = 0;
                itr++;
                length++;
                inAWord = true;
            }else if (isspace(s[itr])) {
                itr++;
                inAWord = false;
            }
        }
        
        return length;
    }
};
Plus One
Solution#1
    vector<int> plusOne(vector<int> &digits) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int length = digits.size();
        int index = 0;
        bool flag = false;
        // look for '9'+
        for (int i=0;i<length;i++) {
            if (digits[i] == 9 && !flag) {
                index = i;
                flag = true;
            }
            else if (digits[i] != 9 && flag)
                flag = false;
        }
        
        if (!flag) digits[length-1] = digits[length-1] + 1;
        else {
            for (int i=index;i<length;i++) {
                digits[i] = 0;
            }
            // if it starts at beginning
            if (index == 0) {
                digits[0] = 1;
                digits.push_back(0);
            }
            else digits[index-1] = digits[index-1] + 1;   
        }
        return digits;     
    }

Solution#2
class Solution {
public:
    vector<int> plusOne(vector<int> &digits) {
        bool carry = true;
        int itr = digits.size() - 1;
        while (carry && itr >= 0) {
            if (digits[itr] == 9) {
                digits[itr] = 0;
            }else {
                digits[itr] = digits[itr] + 1;
                carry = false;
                return digits;
            }
            itr--;
        }

        vector<int> copy(digits.size() + 1);
        copy[0] = 1;
        for (int i = 0; i < digits.size(); i++) {
            copy[i + 1] = digits[i];
        }
            
        return copy;
        
    }
};

找到最后一个非9的数
class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        int notNine = -1;
        for (int i = digits.size() - 1; i >= 0; i--) {
            if (digits[i] != 9) {
                notNine = i;
                break;
            }
        }
        
        for (int i = notNine + 1; i < digits.size(); i++) {
            digits[i] = 0;
        }
        
        if (notNine == -1) {
            vector<int> rt(digits.size() + 1,0);
            rt[0] = 1;
            return rt;
        }
        
        digits[notNine]++;
        return digits;
    }
};

Remove Duplicates from Sorted List
ListNode *deleteDuplicates(ListNode *head) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (head == NULL) return head;
        ListNode *temp = head;
        
        while (temp->next != NULL) {
            if (temp->val == temp->next->val) 
                temp->next =  temp->next->next;
            else temp = temp->next;
        }
        return head;
}

No comments:

Post a Comment