I answered using the map function + 1.
in java8"s stream class, but watching the interviewer"s reaction convinced me that this was not the right answer. What would you do
I answered using the map function + 1.
in java8"s stream class, but watching the interviewer"s reaction convinced me that this was not the right answer. What would you do
Stream of Java8
can be executed concurrently, but Stream
does not change that the original list, can only return a new list, and assign a value to the reference of the original list. But if the list is RandomAccess
, that is, the underlying implementation is an array, such as ArrayList
, then you can directly use the traditional for loop to traverse, because for RandomAccess
List
, the time complexity of accessing the array elements through the subscript is O (1), then the time complexity of traversing is O (N), which is an excellent time complexity, and no extra space is used. The space complexity is O (1); LinkedList
, then the time complexity of the corresponding element in the list is obtained by subscript. If O (N), uses the previous method, then the total time complexity will be O (N ^ 2), then it is recommended to create a new List
of the same size, and then traverse the original list, to add the value of + 1 for each element to the new List
. At this time, the time complexity is O (N), and the space complexity is also O (N). (of course, you can also use Stream
to generate a new List
) .
< hr >
so I guess the interviewer is not satisfied with your answer because you don't take into account different List types.
there are now 100 arrays, each with 100 numbers. And the elements of each array are arranged from small to large. how to select the smallest number in the first 100. ...
for example, when I learn multithreading, I can only write a few simple demo, to write a consumer model. I don t need it at work. I don t know the specific application scenarios. For example, dubbo and springcloud, just say that building a simple demo,...
implement a sum method to make it behave as follows sum(2,3).valueOf() 5 sum(2,3,4).valueOf() 9 sum(2,3)(4).valueOf() 9 sum(2,3,4)(2)(3,4).valueOf() 18 the previous parts sum () , sum () () are easy t...