LintCode入门八题

37. 反转一个3位整数

Note
这一题反复的测试后我发现了,运算符之间减少空格能很明显地降低运行时间,且同样的数字,拆开运算比整个运算更加省时,比如100就拆为10*10。

class Solution {
public:
    /**
     * @param number: A 3-digit number.
     * @return: Reversed number.
     */
    int reverseInteger(int number) {
        return number%10*10*10+number/10%10*10+number/10/10%10;
    }
};

145. 大小写转换

Note
我现在算是知道为什么LintCode必刷了。返回形参和直接返回结果,其用时是完全不一样的。直接返回character - 32,用时50ms,而return m只需10ms,差距太大了吧。。。

class Solution {
public:
    /**
     * @param character: a character
     * @return: a character
     */
    char lowercaseToUppercase(char character) {
        // write your code here
        char m = character - 32;
        return m;
    }
};

283. 三数之中的最大值

Note
无。

class Solution {
public:
    /**
     * @param num1: An integer
     * @param num2: An integer
     * @param num3: An integer
     * @return: an interger
     */
    int maxOfThreeNumbers(int num1, int num2, int num3) {
        int max = num1 > num2? num1:num2;
        max = max > num3? max:num3;
        return max;
    }
};

366. 斐波纳契数列

Note
无。
最开始的代码。

class Solution {
public:
    /**
     * @param n: an integer
     * @return: an ineger f(n)
     */
    int fibonacci(int n) {
        vector<int> vec;
        vec.push_back(0);
        vec.push_back(1);
        for (int i = 2; i < 50; ++i)
        {
            vec.push_back(vec[i - 1] + vec[i - 2]);
        }
        return vec[n - 1];
    }
};

改进后的代码,直接只计算到n - 1即可。

class Solution {
public:
    /**
     * @param n: an integer
     * @return: an ineger f(n)
     */
    int fibonacci(int n) {
        vector<int> vec = {0, 1};
        while (n > vec.size())
            vec.push_back(vec.back() + vec[vec.size() - 2]);
        return vec[n - 1];
    }
};

463. 整数排序

Note
用的插入排序,用随机快排应该可以用时更少。

class Solution {
public:
    /**
     * @param A: an integer array
     * @return: nothing
     */
    void sortIntegers(vector<int> &A) {
        for (int i = 0; i < A.size(); ++i)
        {
            int temp = A[i], j = i;
            while (j > 0 && temp < A[j - 1])
            {
                A[j] = A[j - 1];
                --j;
            }
            A[j] = temp;
        }
    }
};

466. 链表节点计数

Note
很意外的,事实证明,堆中的形参确实参与运算要快许多。

/**
 * Definition of singly-linked-list:
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 *     ListNode(int val) {
 *        this->val = val;
 *        this->next = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param head: the first node of linked list.
     * @return: An integer
     */
    int countNodes(ListNode * head) {
        ListNode *p = head;
        int count = 0;
        while (p != nullptr)
        {
            ++count;
            p = p->next;
        }
        return count;
    }
};

484. 交换数组两个元素

Note
真不明白那些42ms通过的怎么做到的。。。居然还有代码长度只有一百多的。。。

class Solution {
public:
    /**
     * @param A: An integer array
     * @param index1: the first index
     * @param index2: the second index
     * @return: nothing
     */
    void swapIntegers(vector<int> &A, int index1, int index2) {
        int temp = A[index1];
        A[index1] = A[index2];
        A[index2] = temp;
    }
};

632. 二叉树的最大节点

Note
要考虑到这是一颗二叉树,所以存在没有子结点或只有一个子结点的情况。采用递归,自上而下的递归,自下而上的返回结点。左右孩子都有时,返回左孩子和右孩子中值较大的结点;只有一个孩子时返回孩子结点;没有孩子时返回根结点。将返回的结点与根结点比较,再向上返回值较大的一个。以此实现自下而上的返回出值最大的结点。

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */

class Solution {
public:
    /*
     * @param root: the root of tree
     * @return: the max node
     */
    TreeNode * maxNode(TreeNode * root) {
        if (root == NULL)
            return NULL;
        if (root->left == NULL && root->right == NULL)
            return root;
        TreeNode *a, *b, *max;
        a = maxNode(root->left);
        b = maxNode(root->right);
        if (a == NULL && b != NULL)
            max = b;
        else if (a != NULL && b == NULL)
            max = a;
        else
            max = a->val > b->val? a:b;
        if (root->val < max->val)
            return max;
        else
            return root;
    }
};

一定要自己写一遍哦~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值