备战程序设计大赛-LintCode刷题记录(一)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/BruceYan63/article/details/79133544

366. 斐波那契数列


题目来源:LintCode

题目:
请找出第N个斐波那契数
已知斐波那契数列满足以下条件:
1.前两个数各为0和1.
2.第i个数是第i-1个数和第i-2个数之和.
前十个斐波那契数如下所示:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ...

样例
给定 1, 返回0
给定 2, 返回1
给定 10, 返回34
难度级别:
容易

使用的编程语言:
C++

思路分析:
此题比较简单,申请一个数组, 将斐波那契数列置于数组中, 再根据给定的下标进行输出.
实现代码:
class Solution {
public:
    /*
     * @param n: an integer
     * @return: an ineger f(n)
     */
    int fibonacci(int n) {
    	int a[1000] = {0, 1};
    	for(int i = 2; i < 1000; i++)
    	{
    		a[i] = a[i-1] + a[i-2];
    	}
    	return a[n-1];
    }
};


452. 删除链表中的元素


题目来源:LintCode

题目:

删除链表中等于给定值val的所有节点.

样例:
给定:1->2->3->3->4->5->3  val = 3
返回:1->2->4->5

编程语言:C++

难度级别:容易

思路分析:
此题需要关注的一点就是处理好需要删除多个节点的情况.利用while循环实现.

实现代码:
class Solution 
{
public:
    /*
     * @param head: a ListNode
     * @param val: An integer
     * @return: a ListNode
     */
    ListNode * removeElements(ListNode * head, int val) 
 {
        ListNode *pnew = new ListNode(0);
        pnew->next = head;
        head = pnew;
        ListNode *p = head;
        ListNode *q = p->next;
        while(q != NULL)
        {
            if(q->val == val)
            {
                p->next = q->next;
                q = p->next;
            }
            else
            {
                p = p->next;
                q = q->next;
            }
        }
        return head->next;
    }
};


454. 矩阵面积

题目来源:LintCode

题目:
实现一个矩阵类 Rectangle, 包含如下的一些成员变量与函数:
1.两个共有的成员变量 width 和 height 分别代表宽度和高度.
2.一个构造函数, 接受2个参数 width和height 来设定矩阵的宽度和高度.
3.一个成员函数 getArea, 返回这个矩阵的面积.

样例:
Java:
    Rectangle rec = new Rectangle(3, 4);
    rec.getArea(); // should get 12

Python:
    rec = Rectangle(3, 4)
    rec.getArea()

编程语言:java

难度级别:容易

实现代码:
public class Rectangle {
    /*
     * Define two public attributes width and height of type int.
     */
    public int width, height;
    /*
     * Define a constructor which expects two parameters width and height here.
     */
    Rectangle(int x, int y) {
        width = x;
        height = y;
    }
    /*
     * Define a public method `getArea` which can calculate the area of the
     * rectangle and return.
     */
    //定义方法 getArea 计算矩形面积并return
    public int getArea() {
        return width*height;
    }
}


463. 整数排序

题目来源:LintCode
题目:对给定的数组进行排序(冒泡法).
样例:
给定: [3, 2, 1, 4, 5]
结果: [1, 2, 3, 4, 5]
编程语言:java
难度级别:容易
思路分析:
冒泡排序想必大家都很熟悉, 直接上代码.
实现代码:
public class Solution {
    /*
     * @param A: an integer array
     * @return: 
     */       
    public void sortIntegers(int[] A) {
        int temp = 0;
        for (int i = 0; i < A.length; i++) 
            for (int j = 0; j < A.length-i-1; j++) {
                if (A[j] > A[j+1]) {
                    temp = A[j];
                    A[j] = A[j+1];
                    A[j+1] = temp;
            }
        }
    }
}


466. 链表节点计数

题目来源:LintCode

题目:给定一单链表, 计算链表中有多少个节点.


编程语言:C++

难度级别:容易

样例
给出:1->3->5
返回:3

思路分析:
单链表的尾节点指针域为空, 利用这个终止条件遍历整个链表, 可以轻松做到计数.

实现代码:
class Solution {
public:
    /*
     * @param head: the first node of linked list.
     * @return: An integer
     */
    int countNodes(ListNode * head) 
    {
        int counter = 0;
        while (head != NULL) 
        {
            counter++;
            head = head->next;
        }
        return counter;
    }

632. 二叉树的最大节点

题目来源:LintCode
题目:在二叉树中寻找值最大的节点并返回.
编程语言:C++
难度级别:较易
样例
给出如下一棵二叉树:
     1
   /   \
 -5     2
 / \   /  \
0   3 -4  -5 

返回值为3的节点.


思路分析:

查阅有关数据结构资料后, 遍历二叉树选择了递归算法,递归出口就是root == NULL.
我将max指向的节点的数据初始化为INT_MIN(已经被宏定义的整型变量可取的最小值).
从根开始进行遍历, 先遍历所有左子节点后遍历所有右子节点.

之后苦于这个Runtime Error:
segmentation fault (core dumped)
发现如果将一个指针赋值为空之后再让这个指针指向其他区域就会报以上错误.
解决方法就是给指针 new一个空间.


实现代码:
/**
 * 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 *max = new TreeNode(INT_MIN);
    TreeNode * maxNode(TreeNode * root)
    {
        if (root == NULL)
        {
            return root;
        }
        if (max->val < root->val)
        {
            max = root;
        }
        maxNode(root->left);
        maxNode(root->right);
        return max;
    }
};












猜你喜欢

转载自blog.csdn.net/BruceYan63/article/details/79133544