LeetCode Javascript实现 283. Move Zeroes 349. Intersection of Two Arrays 237. Delete Node in a Linked List

283. Move Zeroes

var moveZeroes = function(nums) {


   var num1=0,num2=1;
   while(num1!=num2){
   nums.forEach(function(x,y){
           if(x===0){
               nums.splice(y,1);
               nums.push(0);
           }
           num1 = nums ;
   }); 
   nums.forEach(function(x,y){
           if(x===0){
               nums.splice(y,1);
               nums.push(0);
           }
           num2 = nums ;
   }); 
}
};

这题本身并不难,只是方法要考虑好就最好了,用到方法forEach(),splice(),push()


349. Intersection of Two Arrays

/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number[]}
 */
var intersection = function(nums1, nums2) {
    var arrt1 = [], i = 0;
    nums1.forEach(function(x,y){
        nums2.forEach(function(z,v){
            if(z==x){
                arrt1[i]=x;
                i++;
            }
        });

    });



  var ret = [];

  for (var k = 0; k < arrt1.length; k++) {
    var item = arrt1[k];
    if (ret.indexOf(item) === -1) {
      ret.push(item);
    }
  }



return ret;

};

这题我的思路是先将两个数组递归遍历,将有重复的部分都存进某个数组,然后数组去重!但是这样在效率上很低,大约击败7%左右的人,仅仅是个可用但是不好用的方法


237. Delete Node in a Linked List

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} node
 * @return {void} Do not return anything, modify node in-place instead.
 */
var deleteNode = function(node) {
    node.val=node.next.val;
    node.next=node.next.next;
};

【注】一道链表题,题目很简单,不过一开始没读懂。算是复习一下链表。

原文地址:https://www.cnblogs.com/cndotabestdota/p/5730231.html