33 * @return {number }
44 */
55
6+ var twoSum = function ( nums , target ) { // assume nums [3,2,4] target 6
7+ const map = new Map ( ) ; // create a new hashmap
8+ for ( let i = 0 ; i < nums . length ; i ++ ) { // now iterate over nums
9+ const complement = target - nums [ i ] // find complement=target-current. for 3 = 3
10+ if ( map . has ( complement ) ) { // if map has 3
11+ return [ map . get ( complement ) , i ] // return index of 3, and i = 0
12+ } // well it has not 3 so
13+ map . set ( nums [ i ] , i ) // set current number to the map
14+ }
15+ } ;
16+
17+ var missingNumber = function ( nums ) {
18+ const n = nums . length ;
19+ const expextedSum = ( n * ( n + 1 ) ) / 2 ;
20+ const actualSum = nums . reduce ( ( sum , num ) => num + sum , 0 )
21+ return expextedSum - actualSum ;
22+ } ;
23+
24+ function singleNumber ( nums ) {
25+ let unique = 0 ;
26+ for ( let num of nums ) {
27+ unique ^= num ; // XOR each number
28+ }
29+ return unique ;
30+ }
31+
32+ function moveZeroes ( nums ) {
33+ let lastNonZeroIndex = 0 ;
34+
35+ // Move all non-zero elements to the front
36+ for ( let i = 0 ; i < nums . length ; i ++ ) {
37+ if ( nums [ i ] !== 0 ) {
38+ nums [ lastNonZeroIndex ] = nums [ i ] ;
39+ lastNonZeroIndex ++ ;
40+ }
41+ }
42+ // Fill the rest of the array with zeros
43+ for ( let i = lastNonZeroIndex ; i < nums . length ; i ++ ) {
44+ nums [ i ] = 0 ;
45+ }
46+ }
47+
648var check = function ( nums ) { // check if array is sorted (increasing) and rotated
749 // Simply check if the next number is greater than the current
850 // If the array is sorted in cyclic order then the next is nums[(i+1)%nums.length]
@@ -66,8 +108,8 @@ var removeElement = function (nums, val) { // remove element by value
66108
67109var rotate = function ( matrix ) { // rotate matrix 90 degree
68110 let n = matrix . length ;
69- for ( let i = 0 ; i < n ; i ++ ) {
70- for ( let j = i + 1 ; j < n ; j ++ ) { // j is always 1 more to i to avoid unnecessary swap (overriding number itself)
111+ for ( let i = 0 ; i < n ; i ++ ) {
112+ for ( let j = i + 1 ; j < n ; j ++ ) { // j is always 1 more to i to avoid unnecessary swap (overriding number itself)
71113 [ matrix [ i ] [ j ] , matrix [ j ] [ i ] ] = [ matrix [ j ] [ i ] , matrix [ i ] [ j ] ] ;
72114 }
73115 }
@@ -86,7 +128,7 @@ var maxArea = function (height) { // container with most water
86128 const area = Math . min ( height [ left ] , height [ right ] ) * ( right - left ) // calculate area enclosed by left and right pillar
87129 maxWater = Math . max ( maxWater , area ) // if current area is maximum, update it
88130 if ( height [ left ] < height [ right ] ) { // continue with next larger pillar whether it is left or right
89- left ++ ;
131+ left ++ ;
90132 } else {
91133 right -- ;
92134 }
@@ -126,6 +168,38 @@ var firstMissingPositive = function (nums) { // first missing positive
126168 return n + 1 ; // If all numbers are at their correct position return i+1
127169} ;
128170
171+ var spiralOrder = function ( matrix ) {
172+ const result = [ ] ;
173+ if ( matrix . length == 0 ) return result ;
174+ let top = 0 ,
175+ bottom = matrix . length - 1 ;
176+ let left = 0 ,
177+ right = matrix [ 0 ] . length - 1 ;
178+ while ( top <= bottom && left <= right ) {
179+ for ( let i = left ; i <= right ; i ++ ) {
180+ result . push ( matrix [ top ] [ i ] ) ;
181+ }
182+ top ++ ;
183+ for ( let i = top ; i <= bottom ; i ++ ) {
184+ result . push ( matrix [ i ] [ right ] ) ;
185+ }
186+ right -- ;
187+ if ( top <= bottom ) {
188+ for ( let i = right ; i >= left ; i -- ) {
189+ result . push ( matrix [ bottom ] [ i ] ) ;
190+ }
191+ }
192+ bottom -- ;
193+ if ( left <= right ) {
194+ for ( let i = bottom ; i >= top ; i -- ) {
195+ result . push ( matrix [ i ] [ left ] ) ;
196+ }
197+ }
198+ left ++ ;
199+ }
200+ return result ;
201+ } ;
202+
129203var trap = function ( height ) { // trapping rain water
130204 if ( height . length === 0 ) return 0 ;
131205 let left = 0 , // two pointer and correspoding variables
0 commit comments