#include <stdlib.h>
#include <time.h>//用到了time函数
int main()
{ int i,number
srand((unsigned) time(NULL))//用时间做种,每次产生随机数不一样
for (i=0i<50i++)
{
number = rand() % 101 //产生0-100的随机数
printf("%d ", number)
}
return 0
}
1、打出主函数defs(n):ifn=1。2、在打出条件函数return1else:result=n+s(n-1)return.result。
3、最后结尾返回函数的值result=s(10)print(result)。
近期正在学习go语言,闲暇时间写点leetcode,正好当作熟悉语法,锻炼思路。有些类似的题目,也做些总结和思考。很久以前就特别佩服那些写技术博客的,一直都是懒性子,总算是让自己迈开了第一步,第一篇技术博客,算法、工程、生活,希望自己能多总结,加油!
leetcode No1: https://leetcode.com/problems/two-sum/
题目的意思是,一个数组,找到其中两个数,和为某个给定的值。
直接两层for循环,时间复杂度是O(n^2),空间复杂度是O(1)。
时间复杂度是O(n),空间复杂度是O(n)
顺道附上java版本的解法:
leetcode No167: https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/
这个题也完成可以按照上面leetcodeNo1的hashmap的解法,这种实际上并没有利用上这个是有序数组的优势。
go版本实现:
java版本的实现:
leetcode No653: https://leetcode.com/problems/two-sum-iv-input-is-a-bst/
空间复杂度O(n),时间复杂度O(n)