Algorithm Study

Fibonacci Number

醉酒当歌 提交于 2021-02-08 23:51:10
public static long fib(long index){ long f0=0; long f1=1; long f2=1; if(index==0) return f0; else if(index==1) return f1; else if(index==2) return f2; for(int i=3;i<=index;i++){ f0=f1; f1=f2; f2=f0+f1; } return f2; } 来源: oschina 链接: https://my.oschina.net/u/2742034/blog/749611

【LeetCode】415 Add Strings (java实现)

旧街凉风 提交于 2020-04-12 11:52:31
原题链接 https://leetcode.com/problems/add-strings/ 原题 Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. Note: The length of both num1 and num2 is < 5100. Both num1 and num2 contains only digits 0-9. Both num1 and num2 does not contain any leading zero. You must not use any built-in BigInteger library or convert the inputs to integer directly. 题目要求 题目叫“字符串相加”,给定两个非负的由字符串表示整数,求出它们的和。这里需要注意的是,num1和num2字符串的长度都小于5100,这是个很大的数字,肯定不能用int来计算。数字只包含0-9,也就是说没有负号,没有小数点等,完全当做整形来计算。不允许使用内建的大整形的库函数。 解法 实现一:思路比较简单,既然不能把字符串转为整形来计算,那就一个字符一个字符取出来相加,算好进位即可。 public

【LeetCode】455. Assign Cookies (java实现)

瘦欲@ 提交于 2019-12-10 08:50:19
原题链接 https://leetcode.com/problems/assign-cookies/ 原题 Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which is the minimum size of a cookie that the child will be content with; and each cookie j has a size sj. If sj >= gi, we can assign the cookie j to the child i, and the child i will be content. Your goal is to maximize the number of your content children and output the maximum number. Note: You may assume the greed factor is always positive. You cannot assign more than one

【LeetCode】475. Heaters (java实现)

强颜欢笑 提交于 2019-11-27 05:16:39
原题链接 https://leetcode.com/problems/heaters/ 原题 Winter is coming! Your first job during the contest is to design a standard heater with fixed warm radius to warm all the houses. Now, you are given positions of houses and heaters on a horizontal line, find out minimum radius of heaters so that all houses could be covered by those heaters. So, your input will be the positions of houses and heaters seperately, and your expected output will be the minimum radius standard of heaters. Note: Numbers of houses and heaters you are given are non-negative and will not exceed 25000. Positions of houses and