一开始觉得是简单的组合数学题,但是写完之后发现,首先组合数不是那么简单就能算好的,然后就是。。。。int对于up和down(分子和分母)太小了 囧啊 换成long好一点,还有错,然后再换成double,差不多了,但是有3个结果正好只差2,囧啊,改成float也不行,sigh
最后发现不能用强制转换要用Math.round()函数 囧啊囧
public class Solution { public int climbStairs(int n) { // Start typing your Java solution below // DO NOT write main() function int sum = 0; for (int i = n /2 + n % 2; i <= n; ++i){ int num1 = n - i; double up = 1, down = 1; for (int j = 1; j < n-i+1; ++j){ down *= j; up *= i - j + 1; } sum += (int)(up / down); } return sum; } }
最后发现不能用强制转换要用Math.round()函数 囧啊囧
public class Solution { public int climbStairs(int n) { // Start typing your Java solution below // DO NOT write main() function int sum = 0; for (int i = n /2 + n % 2; i <= n; ++i){ int num1 = n - i; double up = 1, down = 1; for (int j = 1; j < n-i+1; ++j){ down *= j; up *= i - j + 1; } sum += Math.round(up / down); } return sum; } }
发表评论
-
Insert Interval
2012-11-11 01:33 543各种条件真复杂,不仅是边界条件,而且还要分很多种情况讨论 而且 ... -
Implement strStr()
2012-11-07 15:44 582唉 终于到了要记算法的时候了 KMP。。。还没写完 回去再写。 ... -
Flatten Binary Tree to Linked List && Generate Parentheses && Gray Code
2012-11-07 00:08 1093Flatten太简单了 递归 一遍过 oh yeah = = ... -
First Missing Positive
2012-11-06 22:50 589唉 想了很久都没想出来 后来还是看了网上的答案 >_&l ... -
Edit Distance
2012-11-06 00:27 670动规 就是递推。。。比较难想 然后数组长度设置比字符串长度多一 ... -
Divide Two Integers
2012-11-05 00:12 714自己实现除法 太太太恶心了。。。。 就是用位移代替了乘法,然后 ... -
Distinct Subsequences
2012-11-04 21:44 657动规,从前到后用T的每一个字符i,扫描S的每一个字符j。维护一 ... -
Count and Say
2012-11-04 18:46 693public class Solution { ... -
Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Sear
2012-11-04 17:36 826/** * Definition for binary ... -
Container With Most Water
2012-11-04 00:25 697本来以为是个简单的题目,直接二重循环,结果小测试过了,大测试超 ... -
Construct Binary Tree from Inorder and Postorder Traversal
2012-11-03 23:40 748不知道为什么错了。。。eclipse上明明是正确的啊 leet ... -
Combinations
2012-11-03 22:19 606全排列 按理说很简单,可是用递归写,边界条件就还是难想清楚,s ... -
Combination Sum I && II
2012-11-03 21:41 751还是递归 但是边界条件以及边界上的处理不容易搞清楚(一开始我就 ... -
Binary Tree Inorder Traversal
2012-11-02 23:51 615I简单 直接递归就好 addAll函数很好用 /** ... -
Best Time to Buy and Sell Stock I & II
2012-11-02 22:05 1045啊 第一次直接过small和big测试 好爽!虽然主要是以前知 ... -
Balanced Binary Tree
2012-11-01 23:38 610/** * Definition for binary ... -
Anagrams
2012-10-31 00:33 587这题实在是没懂它的意思。。。囧啊 import java. ... -
Add Two Numbers
2012-10-30 23:03 614这题不难 直接上递归就行 /** * Definiti ... -
Add Binary
2012-10-29 00:07 601public String addBinary(Strin ... -
4Sum
2012-10-27 22:49 735本来以为只要在3Sum外面再包一层循环就好了,可是。。。在Ju ...
相关推荐
Carimus编码问题代码function climbingStairs(stairs: number) { if (stairs <= 2) return stairs; return climbingStairs(stairs - 1) + climbingStairs(stairs - 2); } function climbingStairsDriver(stairs: ...
Min Cost Climbing Stairs [746]2. Best Time to Buy and Sell Stock [121]3. Climbing Stairs [70]4. Maximum Subarray [53]5. House Robber [198]6. Range Sum Query - Immutable [303]7. Counting Bits [338]8. ...
Stairs](./leetcode/动态规划-Climbing Stairs.java) [动态规划-Decode Ways](./leetcode/动态规划-Decode Ways.java) [动态规划-Distinct Subsequences](./leetcode/动态规划-Distinct Subsequences.java) [动态...
Stairs).md](Java基础/数据结构与算法/LeetCode/LeetCode 70. 爬楼梯(Climbing Stairs).md) 7月3号 java 异常详解 7月2号 整理基础算法 7月1号 七月 6月30号 6月29号 6月25号 6月22号 Redis中String、List、Hash...
Climbing Stairs Set Matrix Zeroes Gas Station Candy Majority Element Rotate Array Contains Duplicate Contains Duplicate II Contains Duplicate III Product of Array Except Self Game of Life Increasing ...
leetcode怎么销号 LeetCode-Solutions :green_heart:My own LeetCode solutions No. Problem LeetCode 力扣 Python Go Solution Difficulty Tag ...Climbing Stairs Easy 动态规划 0075 Sort Colors M
70.Climbing Stairs 121.Best Time to Buy and Sell Stock 122.Best Time to Buy and Sell Stock II 123.Best Time to Buy and Sell Stock III 141.Linked List Cycle 142.Linked List Cycle II 188.Best Time to ...
70.爬楼梯 (Climbing Stairs) 83.删除排序链表中的重复元素 (Remove Duplicates from Sorted List) 88.合并两个有序数组 (Merge Sorted Array) 100.相同的树 (Same Tree) 104.二叉树的最大深度 (Maximum Depth of ...
第 482 章编码测试 JewelsAndStones_771 [Java] Java HashMap、ArrayList 包含方法性能对比 LicenseKeyFormatting_482 [Java] String、StringBuffer、StringBuilder 的区别和优缺点 ...ClimbingStairs_70 Q
leetcode 答案LeetCode LeetCode in Swift 这个Repo 用来存下我在LeetCode 解题的原始档,包含解题中遇到的错误,也包含解出AC 的答案, ...Climbing Stairs Easy #83 Remove Duplicates from Sorted L
Climbing Stairs 121 买卖股票的最佳时机 Best Time to Buy and Sell Stock 122 买卖股票的最佳时机 Ⅱ Best Time to Buy and Sell StockⅡ 123 买卖股票的最佳时机 Ⅲ Best Time to Buy and Sell StockⅢ 188 买卖...
Climbing Stairs 01. two sum 经典例题: 1. 二维数组查找问题。限制条件为左到右递增,上到下递增。 2. KMP算法实现 UVA problem 100 - The 3n + 1 problem 151 - Power Crisis -> 约瑟夫问题DP 问题 10607 - ...
Climbing Stairs Set Matrix Zeroes API System.arrayCopy 刷题顺序 TOP100 其中算法,主要是以下几种: 基础技巧:分治、二分、贪心 排序算法:快速排序、归并排序、计数排序 搜索算法:回溯、递归、深度优先遍历,...
70.Climbing Stairs, 198.House Robber, 55.Jump Game 72.Edit Distance, 97.Interleaving String, 115.Distinct Subsequences 2017/04/24 (Lintcode)92.Backpack, (Lintcode)125.Backpack II, (Lintcode)564....
Climbing Stairs 0719. Find K-th Smallest Pair Distance 0714. Best Time to Buy and Sell Stock with Transaction Fee 0713. 乘积小于K的子数组 0695. 岛屿的最大面积 0674. 最长连续递增序列 0673. 最长递增子...
Leetcode扑克 ...Climbing Stairs 变态跳台阶 矩形覆盖 二进制中1的个数 191. Number of 1 Bits 数值的整数次方 50. Pow(x, n) 调整数组顺序使奇数位于偶数前面 905. Sort Array By Parity 链表中倒数第
Climbing Stairs 一开始用传统的递归解题,结果TL了。看了下Dscuss,搜索了一下,发现这道居然就是典型的动态规划题,用哈希把子问题的答案记录了就能节省大量的运行时间。 Linked List Cycle 判断链表是否有环。...
Climbing Stairs 入门级记忆化dp 2014.10.30 Merge Sorted Array 归并排序基础 Remove Duplicates from Sorted List 脑残简单题 2014.10.31 *****今天被题目ThreeSum虐出翔,打了球太累,过几天
java lru leetcode 自述文件 这是我的 leetcode 答案库。 ...Climbing Stairs (爬踏板) 力码: localAnswer-python01: localAnswer-python02: 两数之和 力码: localAnswer-java1: localAnswer-
Climbing Stairs] [101. Symmetric Tree] [104. Maximum Depth of Binary Tree] [121. Best Time to Buy and Sell Stock] [167. Two Sum II - Input array is sorted] Medium [2. Add Two Numbers]