I don't get why my code is not accepted (javascript)


  • 0
    J
       var removeElement = function(A, elem) {
        var skipped = 0;
        A.forEach(function(d,i) {
              if (d === elem) {
                  skipped++;
              }
              A[i] = A[i + skipped];
        })
        return A;
    };
    

    input [3, 3], 5
    output []
    expected [3, 3]

    but that code would definitely output [3, 3].


  • 0
    A
    var removeElement = function(A, elem) {
       var skipped = 0;
       var len = A.length;
       A.forEach(function(d,i) {
             if (d === elem) {
               skipped++;
               len--;
             }
            if(skipped !== 0){
              A[i - skipped + 1] = A[i+1];
            }
       });
       A.length = len;
       return A;
    };
    

    你的思路很好,但是细节没考虑清楚哦^_^


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.