File tree Expand file tree Collapse file tree 2 files changed +77
-0
lines changed
Expand file tree Collapse file tree 2 files changed +77
-0
lines changed Original file line number Diff line number Diff line change 1+ // 2. Add Two Numbers
2+ // https://leetcode.com/problems/add-two-numbers/
3+
4+ var addTwoNumbers = function ( l1 , l2 ) {
5+ var extra = 0 ;
6+ var prevNode = null ;
7+ var resultList = null ;
8+
9+ do {
10+ var sum = ( ( l1 && l1 . val ) || 0 ) + ( ( l2 && l2 . val ) || 0 ) + extra ;
11+
12+ if ( sum >= 10 ) {
13+ extra = Math . floor ( sum / 10 ) ;
14+
15+ sum = sum % 10 ;
16+ } else {
17+ extra = 0 ;
18+ }
19+
20+ var newListItem = { val : sum , next : null }
21+
22+ if ( ! resultList ) {
23+ resultList = newListItem
24+ }
25+
26+ if ( prevNode ) {
27+ prevNode . next = newListItem
28+ }
29+
30+ prevNode = newListItem
31+
32+ l1 = l1 && l1 . next
33+ l2 = l2 && l2 . next
34+ } while ( l1 || l2 ) ;
35+
36+ if ( extra > 0 ) {
37+ prevNode . next = { val : extra , next : null }
38+ }
39+
40+ return resultList ;
41+ }
Original file line number Diff line number Diff line change 1+ # 3. Longest Substring Without Repeating Characters
2+ # https://leetcode.com/problems/longest-substring-without-repeating-characters
3+
4+ def longest_substring ( s )
5+ memo = ""
6+
7+ s . each_char do |char |
8+ break if memo . include? ( char )
9+
10+ memo . concat ( char )
11+ end
12+
13+ memo
14+ end
15+
16+ def length_of_longest_substring ( s )
17+ longest_size = 0
18+
19+ return s . size if s . size < 2
20+
21+ while longest_size < s . size
22+ substring = longest_substring ( s )
23+
24+ longest_size = substring . size if substring . size > longest_size
25+
26+ break if longest_size >= s . size
27+
28+ repetitive = s [ substring . size ]
29+ repetitive_index = substring . index ( repetitive )
30+ start_from = repetitive_index + 1
31+
32+ s = s [ start_from ..-1 ]
33+ end
34+
35+ longest_size
36+ end
You can’t perform that action at this time.
0 commit comments