lintcode bugfree and good codestyle note

2016.12.4, 366

http://www.lintcode.com/en/problem/fibonacci/

一刷使用递归算法,超时。二刷使用九章算术的算法,就是滚动指针的思路,以前写python的时候也玩过,但是给忘了,这次又用c++拾起来了。lint有bug,不能用,很烦。

class Solution {
public:
    /**
     * @param n: an integer
     * @return an integer f(n)
     */
    int fibonacci(int n) {
        int a = 0, b = 1;
        for (int i = 1; i < n; i++) {
            int c = a + b;
            a = b;
            b = c;
        }
        return a;
    }
};

时间: 2024-10-13 03:03:46

lintcode bugfree and good codestyle note的相关文章

LintCode刷题笔记(九章ladder PartOne)--BugFree

九章ladder的前半部分刷题笔记,在这次二刷的时候补上~ @ 2017.05.21 141 - sqrtx 二分答案 ---  binarySearch二分法 --- class Solution: """ @param x: An integer @return: The sqrt of x """ def sqrt(self, x): # write your code here if not x: return 0 start, end

[LintCode] Number of Airplanes in the Sky

Given an interval list which are flying and landing time of the flight. How many airplanes are on the sky at most? Example For interval list [[1,10],[2,3],[5,8],[4,7]], return 3 Note If landing and flying happens at the same time, we consider landing

LintCode Subtree

原题链接在这里:http://www.lintcode.com/en/problem/subtree/ You have two every large binary trees: T1, with millions of nodes, and T2, with hundreds of nodes. Create an algorithm to decide if T2 is a subtree ofT1. Have you met this question in a real intervi

Lintcode: Majority Number II 解题报告

Majority Number II 原题链接: http://lintcode.com/en/problem/majority-number-ii/# Given an array of integers, the majority number is the number that occurs more than 1/3 of the size of the array. Find it. Note There is only one majority number in the arra

Lintcode: First Bad Version 解题报告

First Bad Version http://lintcode.com/en/problem/first-bad-version The code base version is an integer and start from 1 to n. One day, someone commit a bad version in the code case, so it caused itself and the following versions are all failed in the

android代码规范和studio配置CodeStyle

studio配置CodeStyle可以很好的帮助我们检测代码规范性,保持大家的代码统一,来看看怎么配置和使用吧 代码规范,自己公司的一套 代码规范 一.      简介 A.    目的 本文提供一整套编写高效可靠的 Java代码的标准.约定和指南.它们以安全可靠的软件工程原则为基础,使代码易于理解.维护和增强.而且,通过遵循这些程序设计标准,你作为一个 Java软件开发者的生产效率会有显著提高.经验证明,若从一开始就花时间编写高质量的代码,则在软件开发阶段,对代码的修改要容易很多.最后,遵循一

Lintcode:Longest Common Subsequence 解题报告

Longest Common Subsequence Given two strings, find the longest comment subsequence (LCS). Your code should return the length of LCS. 样例For "ABCD" and "EDCA", the LCS is "A" (or D or C), return 1 For "ABCD" and "

a note of R software write Function

Functionals “To become significantly more reliable, code must become more transparent. In particular, nested conditions and loops must be viewed with great suspicion. Complicated control flows confuse programmers. Messy code often hides bugs.” — Bjar

Lintcode: Kth Largest Element 解题报告

Kth Largest Element Find K-th largest element in an array. Note You can swap elements in the array Example In array [9,3,2,4,8], the 3th largest element is 4 Challenge O(n) time, O(1) space 原题链接: http://www.lintcode.com/en/problem/kth-largest-element