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
,包含如下的一些成员变量与函数:
- 两个共有的成员变量
width
和height
分别代表宽度和高度。 - 一个构造函数,接受2个参数 width 和 height 来设定矩阵的宽度和高度。
- 一个成员函数
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
阅读全文
0 0
- LintCode刷题记录入门题汇总
- 概念题刷题记录
- 二、lintcode刷题记录--二叉树的路径和
- lintcode刷题记录合并排序数组 java
- lintcode刷题记录合并排序数组 java
- LintCode做题记录- 数字三角形
- leetCode高频题刷题记录1;
- OI刷题记录
- OI刷题记录~
- leetcode刷题记录
- 刷题记录
- 6.22刷题记录
- 7.26-刷题记录
- hdu 刷题记录
- 面试刷题记录
- 每日刷题记录
- leetcode刷题记录
- hdu刷题记录
- “戏”说spark---spark 内存管理详解
- ubuntu16.04 sudo:无法解析主机 解决方案
- 操作系统--硬件与软件的桥梁
- 电子科技大学第九届ACM趣味程序设计竞赛第二场(正式赛) 题解
- 算法提高 三进制数位和
- LintCode刷题记录入门题汇总
- C++学习笔记--类对象、继承、多态的内存模型
- Big Data Developer(Day01)
- const常量
- 【Android】一个例子解释创建者模式
- java基础系列(二)
- framebuffer的驱动框架
- 写作的乐趣
- SQL Server2005实现累加