[关闭]
@ChuckIsReady 2018-04-13T03:15:23.000000Z 字数 3142 阅读 619

4.13微策略一面,二面挂面

未分类


We are given an array asteroids of integers representing asteroids in a row.
For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed.
Find out the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet.
Example 1:
Input: asteroids = [5, 10, -5] Output: [5, 10] Explanation: The 10 and -5 collide resulting in 10. The 5 and 10 never collide.
Example 2:
Input: asteroids = [8, -8] Output: [] Explanation: The 8 and -8 collide exploding each other.
Example 3:
Input: asteroids = [10, 2, -5] Output: [10] Explanation: The 2 and -5 collide resulting in -5. The 10 and -5 collide resulting in 10.
Example 4:
Input: asteroids = [-2, -1, 1, 2] Output: [-2, -1, 1, 2] Explanation: The -2 and -1 are moving left, while the 1 and 2 are moving right.
Asteroids moving the same direction never meet, so no asteroids will meet each other.
Note:
The length of asteroids will be at most 10000.
Each asteroid will be a non-zero integer in the range [-1000, 1000]..
var asteroidCollision = function(asteroids) {

};

大意就是一个数组的小星星, 正负代表方向左右,大小代表体积,相撞的话大的摧毁小的,相同同归于尽

  1. //面试写的代码。循环体里slice用错了结果有问题
  2. var asteroidCollision = function(asteroids) {
  3. var arr = asteroids;
  4. var temp = [];
  5. for(var i =0;i<arr.length;i++){
  6. temp.push(arr[i]);
  7. if(temp.length==1)continue;
  8. if(temp[temp.length-2] > 0){
  9. temp = temp.slice(-2).concat(compare(temp[temp.length-2],temp[temp.length-1]))
  10. }
  11. }
  12. return temp;
  13. };
  14. var ab = function(num){
  15. if(num>0)
  16. return num
  17. else
  18. return num*-1
  19. }
  20. var compare = function(a,b){
  21. if(a<0) return [a,b];
  22. else{
  23. if (b>0) return [a,b];
  24. else{
  25. if(ab(a)>ab(b))
  26. {return [a]}
  27. else if(ab(a)<ab(b)){ return [b]}
  28. else return [];
  29. }
  30. }
  31. }

结束后花了10分钟改了下就ok了

  1. var asteroidCollision = function(asteroids) {
  2. var arr = asteroids;
  3. var temp = [];
  4. for(var i =0;i<arr.length;i++){
  5. temp.push(arr[i]);
  6. if(temp.length==1)continue;
  7. var length = temp.length;
  8. do{
  9. length = temp.length;
  10. var tempPop = temp.pop();
  11. temp = temp.concat(compare(temp.pop(),tempPop)) //改用pop就可以了
  12. }
  13. while(temp.length>1&&temp.length<length) //如果发生了碰撞导致数组长度变短就继续碰撞前一个
  14. }
  15. console.log(temp);
  16. return temp;
  17. };
  18. var ab = function(num){
  19. if(num>0)
  20. return num
  21. else
  22. return num*-1
  23. }
  24. var compare = function(a,b){
  25. if(a<0) return [a,b];
  26. else{
  27. if (b>0) return [a,b];
  28. else{
  29. if(ab(a)>ab(b))
  30. {return [a];}
  31. else if(ab(a)<ab(b)){ return [b]}
  32. else return [];
  33. }
  34. }
  35. }
  36. console.log("输入[5, 10, -5]");
  37. asteroidCollision([5, 10, -5])
  38. console.log("输入[8, -8]");
  39. asteroidCollision([8, -8])
  40. console.log("输入[10, 2, -5]");
  41. asteroidCollision([10, 2, -5])
  42. console.log("输入[-2, -1, 1, 2]");
  43. asteroidCollision([-2, -1, 1, 2])

最后给写代码的时间太短了,然后他也看不懂js,所以没有步骤分直接挂了- -

第二面

问我操作系统是怎么处理多线程并发的,cpu的算法是什么

Median of two sorted arrays

nums1 = [1, 3]
nums2 = [2]
==> 2

nums1 = [1, 3]
nums2 = [2, 4]
==> 2.5 = (2 + 3)/2

求两个有序数列的中位数

  1. var getMedian = function(arr1,arr2){
  2. var tempArr = [].concat(arr1,arr2).sort(function(a,b){return a-b})
  3. var length = tempArr.length;
  4. if(length%2==0){
  5. var g = (tempArr[length/2] + tempArr[(length-1)/2>>0])/2;
  6. var h = g.toFixed(1);
  7. return h;
  8. }
  9. else{
  10. return tempArr[((length+1)/2)];
  11. }
  12. }

后来说不让先合并排序,我说就一个一个比较着来,比到中间就输出

  1. var getMedian2 = function(arr1,arr2){
  2. var length = tempArr.length;
  3. var tempArr = [];
  4. var count = 0;
  5. for(var i =0;i<arr1.length;i++){
  6. var j =0;
  7. if(arr1[i]>arr2[j]){
  8. tempArr.push(arr1[i]);
  9. count++;
  10. }
  11. else{
  12. tempArr.push(arr2[j++]);
  13. count++
  14. }
  15. if(count == length/2)
  16. return ((tempArr[count] + tempArr[count-1])/2).toFixed(1);
  17. }

大意是这样,没写完就时间到了 然后GG

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注