leetcode简单题目两道(4)

   心情还是有问题,保持每日更新,只能如此了。

Problem

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

Example:

Given binary tree {3,9,20,#,#,15,7},

····3
   / \
  9  20
    /  \
   15   7
Result

return its level order traversal as:

[
  [3],
  [9,20],
  [15,7]
]
Code

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int HeightOfTree(TreeNode* root) {
        int result = 0;
        if (root == NULL) {
            return result;
        }
        int left = HeightOfTree(root->left) + 1;
        int right = HeightOfTree(root->right) + 1;
        result = left > right ? left : right;
        return result;
    }
    void calLevelOrder(TreeNode* root, int level, vector<vector<int> >& result) {
        if (root == NULL) {
            return;
        }
        result[level].push_back(root->val);
        calLevelOrder(root->left, level + 1, result);
        calLevelOrder(root->right, level + 1, result);
    }
    vector<vector<int> > levelOrder(TreeNode* root) {
        vector<vector<int> > result;
        vector<int> v;
        if (root == NULL) {
            return result;
        }
        int height = HeightOfTree(root);
        for (int i = 0; i < height; i++) {
            result.push_back(v);
        }
        calLevelOrder(root, 0, result);
        return result;
    }
};
Problem1

Write a function that takes a string as input and returns the string reversed.

Example:

Given s = "hello", return "olleh".

Code

class Solution {
public:
    string reverseString(string s) {
        if (s.size() &lt;= 1) {
            return s;
        }
        int i = 0, j = s.size() - 1;
        char ch;
        while (i &lt; j) {
            ch = s[i];
            s[i] = s[j];
            s[j] = ch;
            i++;
            j--;
        }
        return s;
    }
};
Problem2

Write a function that takes a string as input and reverse only the vowels of a string.

Example 1:

Given s = "hello", return "holle".

Example 2:

Given s = "leetcode", return "leotcede".

Code

class Solution {
public:
    string reverseVowels(string s) {
        if (s.size() &lt;= 1) {
            return s;
        }
        int i = 0, j = s.size() - 1;
        char ch;
        while (i &lt; j) {
            while(i &lt; j && s[i] != &#39;a&#39; && s[i] != &#39;e&#39; && s[i] != &#39;i&#39; && s[i] != &#39;o&#39; && s[i] != &#39;u&#39;
            && s[i] != &#39;A&#39; && s[i] != &#39;E&#39; && s[i] != &#39;I&#39; && s[i] != &#39;O&#39; && s[i] != &#39;U&#39;) {
                i++;
            }
            while(i &lt; j && s[j] != &#39;a&#39; && s[j] != &#39;e&#39; && s[j] != &#39;i&#39; && s[j] != &#39;o&#39; && s[j] != &#39;u&#39;
            && s[j] != &#39;A&#39; && s[j] != &#39;E&#39; && s[j] != &#39;I&#39; && s[j] != &#39;O&#39; && s[j] != &#39;U&#39;) {
                j--;
            }
            ch = s[i];
            s[i] = s[j];
            s[j] = ch;
            i++;
            j--;
        }
        return s;
    }
};
说明

与上面同样的逻辑,只是出现特定的字符才交换位置

 

PS:存货快用完了。。。。

posted @ 2017-04-22 22:08  鹰搏长空08  阅读(123)  评论(0编辑  收藏  举报