File tree 2 files changed +34
-0
lines changed
2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 185
185
405|[ Convert a Number to Hexadecimal] ( ./0405-convert-a-number-to-hexadecimal.js ) |Easy|
186
186
412|[ Fizz Buzz] ( ./0412-fizz-buzz.js ) |Easy|
187
187
414|[ Third Maximum Number] ( ./0414-third-maximum-number.js ) |Easy|
188
+ 415|[ Add Strings] ( ./0415-add-strings.js ) |Easy|
188
189
419|[ Battleships in a Board] ( ./0419-battleships-in-a-board.js ) |Medium|
189
190
435|[ Non-overlapping Intervals] ( ./0435-non-overlapping-intervals.js ) |Medium|
190
191
442|[ Find All Duplicates in an Array] ( ./0442-find-all-duplicates-in-an-array.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 415. Add Strings
3
+ * https://leetcode.com/problems/add-strings/
4
+ * Difficulty: Easy
5
+ *
6
+ * Given two non-negative integers, num1 and num2 represented as string, return the sum of num1 and
7
+ * num2 as a string.
8
+ *
9
+ * You must solve the problem without using any built-in library for handling large integers (such
10
+ * as BigInteger). You must also not convert the inputs to integers directly.
11
+ */
12
+
13
+ /**
14
+ * @param {string } num1
15
+ * @param {string } num2
16
+ * @return {string }
17
+ */
18
+ var addStrings = function ( num1 , num2 ) {
19
+ let result = '' ;
20
+
21
+ for ( let i = num1 . length - 1 , j = num2 . length - 1 , carry = 0 ; i > - 1 || j > - 1 || carry === 1 ; ) {
22
+ if ( i > - 1 ) {
23
+ carry += num1 . charCodeAt ( i -- ) - 48 ;
24
+ }
25
+ if ( j > - 1 ) {
26
+ carry += num2 . charCodeAt ( j -- ) - 48 ;
27
+ }
28
+ result = ( carry % 10 ) + result ;
29
+ carry = Math . floor ( carry / 10 ) ;
30
+ }
31
+
32
+ return result ;
33
+ } ;
You can’t perform that action at this time.
0 commit comments