登录
  • #刷题

be‌‌‌‌‍‍‌‍‌‌‍‍‌‍‌‌‌‍‍‌‌‌‌‍‌‍‌‍‌‌‍‍autiful method for an interview question

sing1ee
1013
0
[backcolor=transparent]Given an array of numbers, nums, return an array of numbers products, where products is the product of all nums[j], j != i.[/backcolor]Input : [1, 2, 3, 4, 5]Output: [(2*3*4*5), (1*3*4*5), (1*2*4*5), (1*2*3*5), (1*2*3*4)] = [120, 60, 40, 30, 24][backcolor=transparent]You must do this in O(N) without using division.[/backcolor]

[backcolor=transparent]We can solve this question by O(n) time and O(n) space, or O(n) time and O(1)space by traversing the array two times.[/backcolor][backcolor=transparent]Or[/backcolor][backcolor=transparent]We can sovle in a recursive method.[/backcolor][backcolor=transparent]code: [/backcolor]
[backcolor=transparent]

[/backcolor][backcolor=transparent]perl, but easy to understand. [/backcolor]

0条回复
热度排序

发表回复