This problem is relatively simple, but the quality of the code may not be very good, I share my notes do questions and issues do you enjoy the process to have any deficiencies hope you point it out heavyweights
The original title, junior partner you can try to do something
82 . The number of the single Chinese English given 2 * n-+ . 1 digit, wherein in addition to the other figures are each a number appears twice, to find this number. Sample Sample 1 : Input: [ 1 , 1 , 2 , 2 , 3 , 4 , 4 ] Output: 3 Explanation: only occurs once 3 Sample 2 : Input: [ 0 , 0 , 1 ] Output: 1 Explanation: 1 only appear once challenge next iteration of the complexity of the constant level of additional space considerations n ≦ 100
Do question notes
I understand that individuals visualize,
either lost in, or out, twice the time is out, if only once, then it is lost in
that there are two movements, lost in, come to
define an empty array B for storing a state of the cartridge as
per the first loop a number, each number of the array B are scanned, if there is present on B deleted, added to give the absence array B, so that the single digital You will be able to come out.
Implementation code
/** * @param A: An integer array * @return: An integer */ const singleNumber = function (A) { var b = new Array(); for (var i = 0; i <= A.length; i++) { var found = -1;// 初始化 for(var j=0;j <= b.length;j++) { if (b[j] == A[i]) { found = j; } } if (found > -1) { b.splice(found,1); // delete } the else { b.push (A [I]); // push } } IF (to b.length> 0) { // This is to prevent the actual step number without leading to the single error // In practice this also acquires a plurality of the single number, this is enough to take a return B [0 ]; } }
He took a second Minghahaha ~~~
The first will always be worthy of envy, so can a few milliseconds [surprised]