LintCode刷题记录入门题汇总

来源:互联网 发布:淘宝网包邮服务 编辑:程序博客网 时间:2024/06/08 19:38

366.斐波那契数列

#include<iostream>using namespace std;class Solution {public:    /*     * @param n: an integer     * @return: an ineger f(n)     */    int fibonacci(int n) {        // write your code here        int x1 = 0, x2 = 1, sum = 0;if (n == 1){return x1;}if (n == 2){return x2;}for (int i = 2; i < n; i++){sum = x1 + x2;x1 = x2;x2 = sum;}return sum;    }};

452.删除链表中的元素

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:    /*     * @param head: a ListNode     * @param val: An integer     * @return: a ListNode     */    ListNode * removeElements(ListNode * head, int val)    {        if(head==NULL)        {            return head;        }        ListNode *p=head,*q=head->next;        while(q!=NULL)        {            if(q->val==val)            {                p->next=q->next;                q=q->next;            }            else            {                p=p->next;                q=q->next;            }        }        if(head->val==val)        {            head=head->next;        }        return head;    }};

632.二叉树的最大节点

class Solution {public:    /*     * @param root: the root of tree     * @return: the max node     */    TreeNode *p=new TreeNode(-100000);    TreeNode * maxNode(TreeNode * root) {        // write your code here        if(root==NULL)        {            return NULL;        }        if(root->val>p->val)        {            p=root;        }        maxNode(root->left);        maxNode(root->right);        return p;    }};              


466.链表节点计数

/** * Definition of ListNode * 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) {        // write your code here        int count=0;        while(head!=NULL)        {            head=head->next;            count++;        }        return count;    }};

463.整数排序

class Solution {public:    /*     * @param A: an integer array     * @return:      */    void sortIntegers(vector<int> &A) {        // write your code here        int n = A.size();int temp = 0;int flag = 0;for (int i = 0; i < n-1; i++){for (int j = 0; j < n - i - 1; j++){if (A[j] > A[j + 1]){temp = A[j + 1];A[j + 1] = A[j];A[j] = temp;}}}    }};

454.矩阵面积

实现一个矩阵类Rectangle,包含如下的一些成员变量与函数:

  1. 两个共有的成员变量 width 和 height 分别代表宽度和高度。
  2. 一个构造函数,接受2个参数 width 和 height 来设定矩阵的宽度和高度。
  3. 一个成员函数 getArea,返回这个矩阵的面积。
Python:    rec = Rectangle(3, 4)    rec.getArea()
class Rectangle:    '''     * Define a constructor which expects two parameters width and height here.    '''    # write your code here    def __init__(self,width,height):        self.width=width        self.height=height    '''     * Define a public method `getArea` which can calculate the area of the     * rectangle and return.    '''    # write your code here    def getArea(self):        return self.width*self.height



原创粉丝点击