id
stringlengths 13
20
| java
sequence | python
sequence |
---|---|---|
leetcode_946_A | [
"class Solution { public boolean validateStackSequences ( int [ ] pushed , int [ ] popped ) { Stack < Integer > inStack = new Stack < > ( ) ; int posPush = 0 , posPop = 0 ; while ( posPush != pushed . length ) { int curr = pushed [ posPush ] ; while ( ! inStack . empty ( ) && popped . length > 0 && inStack . peek ( ) == popped [ posPop ] ) { inStack . pop ( ) ; posPop ++ ; } if ( popped . length == 0 ) break ; if ( curr == popped [ posPop ] ) posPop ++ ; else inStack . push ( curr ) ; posPush ++ ; } while ( ! inStack . empty ( ) && popped . length > 0 && inStack . peek ( ) == popped [ posPop ] ) { inStack . pop ( ) ; posPop ++ ; } if ( inStack . empty ( ) ) return true ; return false ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def validateStackSequences ( self , pushed , popped ) : NEW_LINE INDENT in_stack = [ ] NEW_LINE pos = 0 NEW_LINE while pos != len ( pushed ) : NEW_LINE INDENT curr = pushed [ pos ] NEW_LINE while len ( in_stack ) > 0 and len ( popped ) > 0 and in_stack [ - 1 ] == popped [ 0 ] : NEW_LINE INDENT in_stack . pop ( - 1 ) NEW_LINE popped . pop ( 0 ) NEW_LINE DEDENT if len ( popped ) == 0 : NEW_LINE INDENT break NEW_LINE DEDENT if curr == popped [ 0 ] : NEW_LINE INDENT popped . pop ( 0 ) NEW_LINE DEDENT else : NEW_LINE INDENT in_stack . append ( curr ) NEW_LINE DEDENT pos += 1 NEW_LINE DEDENT while len ( in_stack ) > 0 and len ( popped ) > 0 and in_stack [ - 1 ] == popped [ 0 ] : NEW_LINE INDENT in_stack . pop ( - 1 ) NEW_LINE popped . pop ( 0 ) NEW_LINE DEDENT if len ( in_stack ) == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = Solution ( ) NEW_LINE print s . validateStackSequences ( [ 1 , 0 , 3 , 2 ] , [ 0 , 1 , 2 , 3 ] ) NEW_LINE DEDENT"
] |
leetcode_253_A | [
"class Solution { public int minMeetingRooms ( Interval [ ] intervals ) { int ans = 0 , curr = 0 ; List < TimePoint > timeline = new ArrayList < > ( ) ; for ( Interval interval : intervals ) { timeline . add ( new TimePoint ( interval . start , 1 ) ) ; timeline . add ( new TimePoint ( interval . end , - 1 ) ) ; } timeline . sort ( new Comparator < TimePoint > ( ) { public int compare ( TimePoint a , TimePoint b ) { if ( a . time != b . time ) return a . time - b . time ; else return a . room - b . room ; } } ) ; for ( TimePoint t : timeline ) { curr += t . room ; if ( curr >= ans ) ans = curr ; } return ans ; } private class TimePoint { int time ; int room ; TimePoint ( int time , int room ) { this . time = time ; this . room = room ; } } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def minMeetingRooms ( self , intervals ) : NEW_LINE INDENT timeline = [ ] NEW_LINE for interval in intervals : NEW_LINE INDENT timeline . append ( ( interval . start , 1 ) ) NEW_LINE timeline . append ( ( interval . end , - 1 ) ) NEW_LINE DEDENT timeline . sort ( ) NEW_LINE ans = curr = 0 NEW_LINE for _ , v in timeline : NEW_LINE INDENT curr += v NEW_LINE ans = max ( ans , curr ) NEW_LINE DEDENT return ans NEW_LINE DEDENT DEDENT"
] |
leetcode_720_A | [
"class Solution { public String longestWord ( String [ ] words ) { Trie trie = new Trie ( ) ; int index = 0 ; for ( String word : words ) { trie . insert ( word , ++ index ) ; } trie . words = words ; return trie . dfs ( ) ; } } class Node { char c ; HashMap < Character , Node > children = new HashMap ( ) ; int end ; public Node ( char c ) { this . c = c ; } } class Trie { Node root ; String [ ] words ; public Trie ( ) { root = new Node ( '0' ) ; } public void insert ( String word , int index ) { Node cur = root ; for ( char c : word . toCharArray ( ) ) { cur . children . putIfAbsent ( c , new Node ( c ) ) ; cur = cur . children . get ( c ) ; } cur . end = index ; } public String dfs ( ) { String ans = \" \" ; Stack < Node > stack = new Stack ( ) ; stack . push ( root ) ; while ( ! stack . empty ( ) ) { Node node = stack . pop ( ) ; if ( node . end > 0 || node == root ) { if ( node != root ) { String word = words [ node . end - 1 ] ; if ( word . length ( ) > ans . length ( ) || word . length ( ) == ans . length ( ) && word . compareTo ( ans ) < 0 ) { ans = word ; } } for ( Node nei : node . children . values ( ) ) { stack . push ( nei ) ; } } } return ans ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def longestWord ( self , words ) : NEW_LINE INDENT Trie = lambda : collections . defaultdict ( Trie ) NEW_LINE trie = Trie ( ) NEW_LINE END = True NEW_LINE for i , word in enumerate ( words ) : NEW_LINE INDENT reduce ( dict . __getitem__ , word , trie ) [ END ] = i NEW_LINE DEDENT stack = trie . values ( ) NEW_LINE ans = \" \" NEW_LINE while stack : NEW_LINE INDENT cur = stack . pop ( ) NEW_LINE if END in cur : NEW_LINE INDENT word = words [ cur [ END ] ] NEW_LINE if len ( word ) > len ( ans ) or len ( word ) == len ( ans ) and word < ans : NEW_LINE INDENT ans = word NEW_LINE DEDENT stack . extend ( [ cur [ letter ] for letter in cur if letter != END ] ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT DEDENT"
] |
leetcode_155_A | [
"import java . util . ArrayList ; import java . util . List ; class MinStack { private Stack < Integer > stack ; private Stack < Integer > minStack ; public MinStack ( ) { stack = new Stack < > ( ) ; minStack = new Stack < > ( ) ; } public void push ( int x ) { stack . push ( x ) ; if ( minStack . size ( ) == 0 || x <= minStack . peek ( ) ) minStack . push ( x ) ; else minStack . push ( minStack . peek ( ) ) ; } public void pop ( ) { stack . pop ( ) ; minStack . pop ( ) ; } public int top ( ) { return stack . peek ( ) ; } public int getMin ( ) { return minStack . peek ( ) ; } }"
] | [
"class MinStack ( object ) : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . stack = [ ] NEW_LINE self . min_stack = [ ] NEW_LINE DEDENT def push ( self , x ) : NEW_LINE INDENT self . stack . append ( x ) NEW_LINE if len ( self . min_stack ) == 0 : NEW_LINE INDENT self . min_stack . append ( x ) NEW_LINE return NEW_LINE DEDENT if x <= self . min_stack [ - 1 ] : NEW_LINE INDENT self . min_stack . append ( x ) NEW_LINE DEDENT else : NEW_LINE INDENT self . min_stack . append ( self . min_stack [ - 1 ] ) NEW_LINE DEDENT DEDENT def pop ( self ) : NEW_LINE INDENT if len ( self . stack ) > 0 : NEW_LINE INDENT self . min_stack . pop ( ) NEW_LINE self . stack . pop ( ) NEW_LINE DEDENT DEDENT def top ( self ) : NEW_LINE INDENT if len ( self . stack ) > 0 : NEW_LINE INDENT return self . stack [ - 1 ] NEW_LINE DEDENT return None NEW_LINE DEDENT def getMin ( self ) : NEW_LINE INDENT if len ( self . min_stack ) > 0 : NEW_LINE INDENT return self . min_stack [ - 1 ] NEW_LINE DEDENT return None NEW_LINE DEDENT DEDENT"
] |
leetcode_945_A | [
"class Solution { public int minIncrementForUnique ( int [ ] A ) { if ( A . length == 0 ) return 0 ; HashSet < Integer > numSet = new HashSet < > ( ) ; List < Integer > duplicated = new ArrayList < > ( ) ; int res = 0 ; Arrays . sort ( A ) ; int left = A [ 0 ] ; int right = A [ A . length - 1 ] ; int holes = right - left + 1 ; for ( int v : A ) { if ( numSet . contains ( v ) ) duplicated . add ( v ) ; else numSet . add ( v ) ; } holes -= numSet . size ( ) ; for ( int i = left + 1 ; i < right ; i ++ ) { if ( holes == 0 || duplicated . size ( ) == 0 ) break ; if ( ! numSet . contains ( i ) && i > duplicated . get ( 0 ) ) { res += i - duplicated . get ( 0 ) ; holes -- ; duplicated . remove ( 0 ) ; } } if ( duplicated . size ( ) == 0 ) return res ; while ( duplicated . size ( ) != 0 ) { right += 1 ; res += right - duplicated . get ( 0 ) ; duplicated . remove ( 0 ) ; } return res ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def minIncrementForUnique ( self , A ) : NEW_LINE INDENT if A is None or len ( A ) == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT res = 0 NEW_LINE num_set = set ( ) NEW_LINE duplicate = [ ] NEW_LINE A . sort ( ) NEW_LINE left , right = A [ 0 ] , A [ - 1 ] NEW_LINE holes = right - left + 1 NEW_LINE for v in A : NEW_LINE INDENT if v in num_set : NEW_LINE INDENT duplicate . append ( v ) NEW_LINE DEDENT else : NEW_LINE INDENT num_set . add ( v ) NEW_LINE DEDENT DEDENT holes = holes - len ( num_set ) NEW_LINE for hole in range ( left + 1 , right ) : NEW_LINE INDENT if holes == 0 or len ( duplicate ) == 0 : NEW_LINE INDENT break NEW_LINE DEDENT if hole not in num_set and hole > duplicate [ 0 ] : NEW_LINE INDENT res += hole - duplicate . pop ( 0 ) NEW_LINE holes -= 1 NEW_LINE DEDENT DEDENT while len ( duplicate ) != 0 : NEW_LINE INDENT right += 1 NEW_LINE res += right - duplicate . pop ( 0 ) NEW_LINE DEDENT return res NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = Solution ( ) NEW_LINE DEDENT"
] |
leetcode_009_A | [
"class Solution { public boolean isPalindrome ( int x ) { if ( x < 0 ) return false ; int temp = x ; int len = 0 ; while ( temp != 0 ) { temp /= 10 ; len ++ ; } temp = x ; int left , right ; for ( int i = 0 ; i < len / 2 ; i ++ ) { right = temp % 10 ; left = temp / ( int ) Math . pow ( 10 , len - 2 * i - 1 ) ; left = left % 10 ; if ( left != right ) return false ; temp /= 10 ; } return true ; } public boolean isPalindrome ( int x ) { if ( x < 0 ) return false ; int div = 1 ; while ( x / div >= 10 ) { div *= 10 ; } while ( x != 0 ) { int l = x / div ; int r = x % 10 ; if ( l != r ) return false ; x = ( x % div ) / 10 ; div /= 100 ; } return true ; } } class Solution { public boolean isPalindrome ( int x ) { int r , s = 0 , number = x ; if ( number < 0 ) { return false ; } while ( number != 0 ) { r = number % 10 ; s = s * 10 + r ; number /= 10 ; } if ( s == x ) { return true ; } else { return false ; } } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def isPalindrome ( self , x ) : NEW_LINE INDENT if x < 0 : NEW_LINE INDENT return False NEW_LINE DEDENT ls = len ( str ( x ) ) NEW_LINE tmp = x NEW_LINE for i in range ( int ( ls / 2 ) ) : NEW_LINE INDENT right = int ( tmp % 10 ) NEW_LINE left = tmp / ( 10 ** ( ls - 2 * i - 1 ) ) NEW_LINE left = int ( left % 10 ) NEW_LINE if left != right : NEW_LINE INDENT return False NEW_LINE DEDENT tmp = tmp // 10 NEW_LINE DEDENT return True NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = Solution ( ) NEW_LINE print s . isPalindrome ( 1001 ) NEW_LINE DEDENT"
] |
leetcode_401_A | [
"class Solution { public List < String > readBinaryWatch ( int num ) { List < String > times = new ArrayList < > ( ) ; for ( int h = 0 ; h < 12 ; h ++ ) for ( int m = 0 ; m < 60 ; m ++ ) if ( Integer . bitCount ( h * 64 + m ) == num ) times . add ( String . format ( \" % d : %02d \" , h , m ) ) ; return times ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def readBinaryWatch ( self , num ) : NEW_LINE INDENT return [ ' % d : %02d ' % ( h , m ) for h in range ( 12 ) for m in range ( 60 ) if ( bin ( h ) + bin ( m ) ) . count ( '1' ) == num ] NEW_LINE DEDENT DEDENT"
] |
leetcode_1064_A | [
"class Solution { public int fixedPoint ( int [ ] A ) { int l = 0 ; int h = A . length ; while ( l <= h ) { int mid = ( l + h ) / 2 ; if ( A [ mid ] > mid ) h = mid - 1 ; else if ( A [ mid ] < mid ) l = mid + 1 ; else return mid ; } return - 1 ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def fixedPoint ( self , A ) : NEW_LINE INDENT l , h = 0 , len ( A ) - 1 NEW_LINE while l <= h : NEW_LINE INDENT mid = ( l + h ) // 2 NEW_LINE if A [ mid ] < mid : NEW_LINE INDENT l = mid + 1 NEW_LINE DEDENT elif A [ mid ] > mid : NEW_LINE INDENT h = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return mid NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT DEDENT"
] |
leetcode_412_A | [
"import java . util . ArrayList ; import java . util . List ; class Solution { public List < String > fizzBuzz ( int n ) { List < String > res = new ArrayList < > ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { String tmp = \" \" ; if ( i % 3 == 0 ) tmp += \" Fizz \" ; if ( i % 5 == 0 ) tmp += \" Buzz \" ; if ( tmp . length ( ) == 0 ) tmp += String . valueOf ( i ) ; res . add ( tmp ) ; } return res ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def fizzBuzz ( self , n ) : NEW_LINE INDENT return [ str ( i ) * ( i % 3 != 0 and i % 5 != 0 ) + \" Fizz \" * ( i % 3 == 0 ) + \" Buzz \" * ( i % 5 == 0 ) for i in range ( 1 , n + 1 ) ] NEW_LINE DEDENT DEDENT"
] |
leetcode_005_A | [
"public class Solution { public String longestPalindrome ( String s ) { int start = 0 , end = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int len1 = expandAroundCenter ( s , i , i ) ; int len2 = expandAroundCenter ( s , i , i + 1 ) ; int len = Math . max ( len1 , len2 ) ; if ( len > end - start ) { start = i - ( len - 1 ) / 2 ; end = i + len / 2 ; } } return s . substring ( start , end + 1 ) ; } private int expandAroundCenter ( String s , int left , int right ) { int L = left , R = right ; while ( L >= 0 && R < s . length ( ) && s . charAt ( L ) == s . charAt ( R ) ) { L -- ; R ++ ; } return R - L - 1 ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def longestPalindrome ( self , s ) : NEW_LINE INDENT ls = len ( s ) NEW_LINE if ls <= 1 or len ( set ( s ) ) == 1 : NEW_LINE INDENT return s NEW_LINE DEDENT temp_s = ' # ' . join ( ' { } ' . format ( s ) ) NEW_LINE tls = len ( temp_s ) NEW_LINE seed = range ( 1 , tls - 1 ) NEW_LINE len_table = [ 0 ] * tls NEW_LINE for step in range ( 1 , tls / 2 + 1 ) : NEW_LINE INDENT final = [ ] NEW_LINE for pos in seed : NEW_LINE INDENT if pos - step < 0 or pos + step >= tls : NEW_LINE INDENT continue NEW_LINE DEDENT if temp_s [ pos - step ] != temp_s [ pos + step ] : NEW_LINE INDENT continue NEW_LINE DEDENT final . append ( pos ) NEW_LINE if temp_s [ pos - step ] == ' # ' : NEW_LINE INDENT continue NEW_LINE DEDENT len_table [ pos ] = step NEW_LINE DEDENT seed = final NEW_LINE DEDENT max_pos , max_step = 0 , 0 NEW_LINE for i , s in enumerate ( len_table ) : NEW_LINE INDENT if s >= max_step : NEW_LINE INDENT max_step = s NEW_LINE max_pos = i NEW_LINE DEDENT DEDENT return temp_s [ max_pos - max_step : max_pos + max_step + 1 ] . translate ( None , ' # ' ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = Solution ( ) NEW_LINE print s . longestPalindrome ( \" abcbe \" ) NEW_LINE DEDENT"
] |
leetcode_717_A | [
"class Solution { public boolean isOneBitCharacter ( int [ ] bits ) { int pos = 0 ; while ( pos < bits . length - 1 ) { pos += bits [ pos ] + 1 ; } return pos == bits . length - 1 ; } }"
] | [
"class Solution : NEW_LINE INDENT def isOneBitCharacter ( self , bits : List [ int ] ) -> bool : NEW_LINE INDENT pos = 0 NEW_LINE while pos < len ( bits ) - 1 : NEW_LINE INDENT pos += bits [ pos ] + 1 NEW_LINE DEDENT return pos == len ( bits ) - 1 NEW_LINE DEDENT DEDENT"
] |
leetcode_008_A | [
"public class Solution { private static final int maxDiv10 = Integer . MAX_VALUE / 10 ; public int myAtoi ( String str ) { int i = 0 , n = str . length ( ) ; while ( i < n && Character . isWhitespace ( str . charAt ( i ) ) ) i ++ ; int sign = 1 ; if ( i < n && str . charAt ( i ) == ' + ' ) i ++ ; else if ( i < n && str . charAt ( i ) == ' - ' ) { sign = - 1 ; i ++ ; } int num = 0 ; while ( i < n && Character . isDigit ( str . charAt ( i ) ) ) { int digit = Character . getNumericValue ( str . charAt ( i ) ) ; if ( num > maxDiv10 || num == maxDiv10 && digit >= 8 ) return sign == 1 ? Integer . MAX_VALUE : Integer . MIN_VALUE ; num = num * 10 + digit ; i ++ ; } return sign * num ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def myAtoi ( self , str ) : NEW_LINE INDENT sign = 1 NEW_LINE max_int , min_int = 2147483647 , - 2147483648 NEW_LINE result , pos = 0 , 0 NEW_LINE ls = len ( str ) NEW_LINE while pos < ls and str [ pos ] == ' β ' : NEW_LINE INDENT pos += 1 NEW_LINE DEDENT if pos < ls and str [ pos ] == ' - ' : NEW_LINE INDENT sign = - 1 NEW_LINE pos += 1 NEW_LINE DEDENT elif pos < ls and str [ pos ] == ' + ' : NEW_LINE INDENT pos += 1 NEW_LINE DEDENT while pos < ls and ord ( str [ pos ] ) >= ord ( '0' ) and ord ( str [ pos ] ) <= ord ( '9' ) : NEW_LINE INDENT num = ord ( str [ pos ] ) - ord ( '0' ) NEW_LINE if result > max_int / 10 or ( result == max_int / 10 and num >= 8 ) : NEW_LINE INDENT if sign == - 1 : NEW_LINE INDENT return min_int NEW_LINE DEDENT return max_int NEW_LINE DEDENT result = result * 10 + num NEW_LINE pos += 1 NEW_LINE DEDENT return sign * result NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = Solution ( ) NEW_LINE print s . myAtoi ( \" + - 2\" ) NEW_LINE DEDENT"
] |
leetcode_819_A | [
"class Solution { public String mostCommonWord ( String paragraph , String [ ] banned ) { paragraph += \" . \" ; Set < String > banset = new HashSet ( ) ; for ( String word : banned ) banset . add ( word ) ; Map < String , Integer > count = new HashMap ( ) ; String ans = \" \" ; int ansfreq = 0 ; StringBuilder word = new StringBuilder ( ) ; for ( char c : paragraph . toCharArray ( ) ) { if ( Character . isLetter ( c ) ) { word . append ( Character . toLowerCase ( c ) ) ; } else if ( word . length ( ) > 0 ) { String finalword = word . toString ( ) ; if ( ! banset . contains ( finalword ) ) { count . put ( finalword , count . getOrDefault ( finalword , 0 ) + 1 ) ; if ( count . get ( finalword ) > ansfreq ) { ans = finalword ; ansfreq = count . get ( finalword ) ; } } word = new StringBuilder ( ) ; } } return ans ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def mostCommonWord ( self , paragraph , banned ) : NEW_LINE INDENT banned = set ( banned ) NEW_LINE count = collections . Counter ( word for word in re . split ( ' [ β ! ? \\ ' , ; . ] ' , paragraph . lower ( ) ) if word ) NEW_LINE return max ( ( item for item in count . items ( ) if item [ 0 ] not in banned ) , key = operator . itemgetter ( 1 ) ) [ 0 ] NEW_LINE DEDENT DEDENT"
] |
leetcode_572_A | [
"public class Solution { HashSet < String > trees = new HashSet < > ( ) ; public boolean isSubtree ( TreeNode s , TreeNode t ) { String tree1 = preorder ( s , true ) ; String tree2 = preorder ( t , true ) ; return tree1 . indexOf ( tree2 ) >= 0 ; } public String preorder ( TreeNode t , boolean left ) { if ( t == null ) { if ( left ) return \" lnull \" ; else return \" rnull \" ; } return \" # \" + t . val + \" β \" + preorder ( t . left , true ) + \" β \" + preorder ( t . right , false ) ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def isSubtree ( self , s , t ) : NEW_LINE INDENT s_res = self . preorder ( s , True ) NEW_LINE t_res = self . preorder ( t , True ) NEW_LINE return t_res in s_res NEW_LINE DEDENT def preorder ( self , root , isLeft ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT if isLeft : NEW_LINE INDENT return \" lnull \" NEW_LINE DEDENT else : NEW_LINE INDENT return \" rnull \" NEW_LINE DEDENT DEDENT return \" # \" + str ( root . val ) + \" β \" + self . preorder ( root . left , True ) + \" β \" + self . preorder ( root . right , False ) NEW_LINE DEDENT DEDENT"
] |
leetcode_347_A | [
"class Solution { public List < Integer > topKFrequent ( int [ ] nums , int k ) { HashMap < Integer , Integer > count = new HashMap ( ) ; for ( int n : nums ) { count . put ( n , count . getOrDefault ( n , 0 ) + 1 ) ; } PriorityQueue < Integer > heap = new PriorityQueue < Integer > ( ( n1 , n2 ) -> count . get ( n1 ) - count . get ( n2 ) ) ; for ( int n : count . keySet ( ) ) { heap . add ( n ) ; if ( heap . size ( ) > k ) heap . poll ( ) ; } List < Integer > top_k = new LinkedList ( ) ; while ( ! heap . isEmpty ( ) ) top_k . add ( heap . poll ( ) ) ; Collections . reverse ( top_k ) ; return top_k ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def topKFrequent ( self , nums , k ) : NEW_LINE INDENT counter = collections . Counter ( nums ) NEW_LINE return [ k for k , v in counter . most_common ( k ) ] NEW_LINE DEDENT DEDENT"
] |
leetcode_458_A | [
"class Solution { public int poorPigs ( int buckets , int minutesToDie , int minutesToTest ) { int n = minutesToTest / minutesToDie + 1 ; int pigs = 0 ; while ( Math . pow ( n , pigs ) < buckets ) pigs ++ ; return pigs ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def poorPigs ( self , buckets , minutesToDie , minutesToTest ) : NEW_LINE INDENT pigs = 0 NEW_LINE while ( minutesToTest / minutesToDie + 1 ) ** pigs < buckets : NEW_LINE INDENT pigs += 1 NEW_LINE DEDENT return pigs NEW_LINE DEDENT DEDENT"
] |
leetcode_937_A | [
"import java . util . List ; class Solution { public String [ ] reorderLogFiles ( String [ ] logs ) { Arrays . sort ( logs , ( log1 , log2 ) -> { String [ ] split1 = log1 . split ( \" β \" , 2 ) ; String [ ] split2 = log2 . split ( \" β \" , 2 ) ; boolean isDigit1 = Character . isDigit ( split1 [ 1 ] . charAt ( 0 ) ) ; boolean isDigit2 = Character . isDigit ( split2 [ 1 ] . charAt ( 0 ) ) ; if ( ! isDigit1 && ! isDigit2 ) { int cmp = split1 [ 1 ] . compareTo ( split2 [ 1 ] ) ; if ( cmp != 0 ) return cmp ; return split1 [ 0 ] . compareTo ( split2 [ 0 ] ) ; } return isDigit1 ? ( isDigit2 ? 0 : 1 ) : - 1 ; } ) ; return logs ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def reorderLogFiles ( self , logs ) : NEW_LINE INDENT letter_logs = [ ] NEW_LINE digit_logs = [ ] NEW_LINE for log in logs : NEW_LINE INDENT if log . split ( ' β ' ) [ 1 ] . isnumeric ( ) : NEW_LINE INDENT digit_logs . append ( log ) NEW_LINE DEDENT else : NEW_LINE INDENT letter_logs . append ( log ) NEW_LINE DEDENT DEDENT return sorted ( letter_logs , key = lambda x : x . split ( ' β ' ) [ 1 : ] + x . split ( ' β ' ) [ 0 ] ) + digit_logs NEW_LINE DEDENT DEDENT"
] |
leetcode_404_A | [
"import java . util . Stack ; import javax . swing . tree . TreeNode ; class Solution { public int sumOfLeftLeaves ( TreeNode root ) { int res = 0 ; Stack < TreeNode > stack = new Stack < > ( ) ; stack . push ( root ) ; while ( ! stack . isEmpty ( ) ) { TreeNode node = stack . pop ( ) ; if ( node != null ) { if ( node . left != null && node . left . left == null && node . left . right == null ) res += node . left . val ; stack . push ( node . right ) ; stack . push ( node . left ) ; } } return res ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def sumOfLeftLeaves ( self , root ) : NEW_LINE INDENT stack = [ root ] NEW_LINE res = 0 NEW_LINE while len ( stack ) > 0 : NEW_LINE INDENT curr = stack . pop ( 0 ) NEW_LINE if curr is not None : NEW_LINE INDENT if curr . left is not None : NEW_LINE INDENT if curr . left . left is None and curr . left . right is None : NEW_LINE INDENT res += curr . left . val NEW_LINE DEDENT DEDENT stack . insert ( 0 , curr . right ) NEW_LINE stack . insert ( 0 , curr . left ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT DEDENT"
] |
leetcode_204_A | [
"class Solution { public int countPrimes ( int n ) { boolean [ ] isPrime = new boolean [ n ] ; int count = 0 ; Arrays . fill ( isPrime , true ) ; for ( int i = 2 ; i < n ; i ++ ) { if ( i * i >= n ) break ; if ( ! isPrime [ i ] ) continue ; for ( int j = i * i ; j < n ; j += i ) isPrime [ j ] = false ; } for ( int i = 2 ; i < n ; i ++ ) if ( isPrime [ i ] ) count ++ ; return count ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def countPrimes ( self , n ) : NEW_LINE INDENT isPrime = [ True ] * n NEW_LINE for i in xrange ( 2 , n ) : NEW_LINE INDENT if i * i >= n : NEW_LINE INDENT break NEW_LINE DEDENT if not isPrime [ i ] : NEW_LINE INDENT continue NEW_LINE DEDENT for j in xrange ( i * i , n , i ) : NEW_LINE INDENT isPrime [ j ] = False NEW_LINE DEDENT DEDENT count = 0 NEW_LINE for i in xrange ( 2 , n ) : NEW_LINE INDENT if isPrime [ i ] : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT DEDENT"
] |
leetcode_844_A | [
"class Solution { public boolean backspaceCompare ( String S , String T ) { return trans ( S ) . equals ( trans ( T ) ) ; } private String trans ( String str ) { StringBuilder sb = new StringBuilder ( ) ; for ( char c : str . toCharArray ( ) ) { if ( c != ' # ' ) { sb . append ( c ) ; } else if ( sb . length ( ) > 0 ) { sb . deleteCharAt ( sb . length ( ) - 1 ) ; } } return sb . toString ( ) ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def backspaceCompare ( self , S , T ) : NEW_LINE INDENT if S == T : NEW_LINE INDENT return True NEW_LINE DEDENT s_stack = [ ] NEW_LINE t_stack = [ ] NEW_LINE for c in S : NEW_LINE INDENT if c != ' # ' : NEW_LINE INDENT s_stack . append ( c ) NEW_LINE DEDENT elif len ( s_stack ) != 0 : NEW_LINE INDENT s_stack . pop ( - 1 ) NEW_LINE DEDENT DEDENT for c in T : NEW_LINE INDENT if c != ' # ' : NEW_LINE INDENT t_stack . append ( c ) NEW_LINE DEDENT elif len ( t_stack ) != 0 : NEW_LINE INDENT t_stack . pop ( - 1 ) NEW_LINE DEDENT DEDENT return ' ' . join ( s_stack ) == ' ' . join ( t_stack ) NEW_LINE DEDENT DEDENT"
] |
leetcode_743_A | [
"class Solution { Map < Integer , Integer > dist ; public int networkDelayTime ( int [ ] [ ] times , int N , int K ) { Map < Integer , List < int [ ] > > graph = new HashMap ( ) ; for ( int [ ] edge : times ) { if ( ! graph . containsKey ( edge [ 0 ] ) ) graph . put ( edge [ 0 ] , new ArrayList < int [ ] > ( ) ) ; graph . get ( edge [ 0 ] ) . add ( new int [ ] { edge [ 1 ] , edge [ 2 ] } ) ; } dist = new HashMap ( ) ; for ( int node = 1 ; node <= N ; ++ node ) dist . put ( node , Integer . MAX_VALUE ) ; dist . put ( K , 0 ) ; boolean [ ] seen = new boolean [ N + 1 ] ; while ( true ) { int candNode = - 1 ; int candDist = Integer . MAX_VALUE ; for ( int i = 1 ; i <= N ; ++ i ) { if ( ! seen [ i ] && dist . get ( i ) < candDist ) { candDist = dist . get ( i ) ; candNode = i ; } } if ( candNode < 0 ) break ; seen [ candNode ] = true ; if ( graph . containsKey ( candNode ) ) for ( int [ ] info : graph . get ( candNode ) ) dist . put ( info [ 0 ] , Math . min ( dist . get ( info [ 0 ] ) , dist . get ( candNode ) + info [ 1 ] ) ) ; } int ans = 0 ; for ( int cand : dist . values ( ) ) { if ( cand == Integer . MAX_VALUE ) return - 1 ; ans = Math . max ( ans , cand ) ; } return ans ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def networkDelayTime ( self , times , N , K ) : NEW_LINE INDENT graph = collections . defaultdict ( list ) NEW_LINE for u , v , w in times : NEW_LINE INDENT graph [ u ] . append ( ( v , w ) ) NEW_LINE DEDENT dist = { node : float ( ' inf ' ) for node in xrange ( 1 , N + 1 ) } NEW_LINE seen = [ False ] * ( N + 1 ) NEW_LINE dist [ K ] = 0 NEW_LINE while True : NEW_LINE INDENT cand_node = - 1 NEW_LINE cand_dist = float ( ' inf ' ) NEW_LINE for i in xrange ( 1 , N + 1 ) : NEW_LINE INDENT if not seen [ i ] and dist [ i ] < cand_dist : NEW_LINE INDENT cand_dist = dist [ i ] NEW_LINE cand_node = i NEW_LINE DEDENT DEDENT if cand_node < 0 : break NEW_LINE seen [ cand_node ] = True NEW_LINE for nei , d in graph [ cand_node ] : NEW_LINE INDENT dist [ nei ] = min ( dist [ nei ] , dist [ cand_node ] + d ) NEW_LINE DEDENT DEDENT ans = max ( dist . values ( ) ) NEW_LINE return ans if ans < float ( ' inf ' ) else - 1 NEW_LINE DEDENT DEDENT"
] |
leetcode_273_A | [
"class Solution { private final String [ ] LESS_THAN_20 = { \" \" , \" One \" , \" Two \" , \" Three \" , \" Four \" , \" Five \" , \" Six \" , \" Seven \" , \" Eight \" , \" Nine \" , \" Ten \" , \" Eleven \" , \" Twelve \" , \" Thirteen \" , \" Fourteen \" , \" Fifteen \" , \" Sixteen \" , \" Seventeen \" , \" Eighteen \" , \" Nineteen \" } ; private final String [ ] TENS = { \" \" , \" Ten \" , \" Twenty \" , \" Thirty \" , \" Forty \" , \" Fifty \" , \" Sixty \" , \" Seventy \" , \" Eighty \" , \" Ninety \" } ; private final String [ ] THOUSANDS = { \" \" , \" Thousand \" , \" Million \" , \" Billion \" } ; public String numberToWords ( int num ) { if ( num == 0 ) return \" Zero \" ; int i = 0 ; String words = \" \" ; while ( num > 0 ) { if ( num % 1000 != 0 ) words = helper ( num % 1000 ) + THOUSANDS [ i ] + \" β \" + words ; num /= 1000 ; i ++ ; } return words . trim ( ) ; } private String helper ( int num ) { if ( num == 0 ) return \" \" ; else if ( num < 20 ) return LESS_THAN_20 [ num ] + \" β \" ; else if ( num < 100 ) return TENS [ num / 10 ] + \" β \" + helper ( num % 10 ) ; else return LESS_THAN_20 [ num / 100 ] + \" β Hundred β \" + helper ( num % 100 ) ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def numberToWords ( self , num ) : NEW_LINE INDENT to19 = ' One β Two β Three β Four β Five β Six β Seven β Eight β Nine β Ten β Eleven β Twelve β ' ' Thirteen β Fourteen β Fifteen β Sixteen β Seventeen β Eighteen β Nineteen ' . split ( ) NEW_LINE tens = ' Twenty β Thirty β Forty β Fifty β Sixty β Seventy β Eighty β Ninety ' . split ( ) NEW_LINE def words ( n ) : NEW_LINE INDENT if n < 20 : NEW_LINE INDENT return to19 [ n - 1 : n ] NEW_LINE DEDENT if n < 100 : NEW_LINE INDENT return [ tens [ n / 10 - 2 ] ] + words ( n % 10 ) NEW_LINE DEDENT if n < 1000 : NEW_LINE INDENT return [ to19 [ n / 100 - 1 ] ] + [ ' Hundred ' ] + words ( n % 100 ) NEW_LINE DEDENT for p , w in enumerate ( ( ' Thousand ' , ' Million ' , ' Billion ' ) , 1 ) : NEW_LINE INDENT if n < 1000 ** ( p + 1 ) : NEW_LINE INDENT return words ( n / 1000 ** p ) + [ w ] + words ( n % 1000 ** p ) NEW_LINE DEDENT DEDENT DEDENT return ' β ' . join ( words ( num ) ) or ' Zero ' NEW_LINE DEDENT DEDENT"
] |
leetcode_876_A | [
"class Solution { public ListNode middleNode ( ListNode head ) { ListNode fast , slow ; fast = slow = head ; while ( fast != null && fast . next != null ) { slow = slow . next ; fast = fast . next . next ; } return slow ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def middleNode ( self , head ) : NEW_LINE INDENT fast = slow = head NEW_LINE while fast and fast . next : NEW_LINE INDENT slow = slow . next NEW_LINE fast = fast . next . next NEW_LINE DEDENT return slow NEW_LINE DEDENT DEDENT"
] |
leetcode_463_A | [
"class Solution { public int islandPerimeter ( int [ ] [ ] grid ) { int islands = 0 , neighbours = 0 ; for ( int i = 0 ; i < grid . length ; i ++ ) { for ( int j = 0 ; j < grid [ i ] . length ; j ++ ) { if ( grid [ i ] [ j ] == 1 ) { islands ++ ; if ( i < grid . length - 1 && grid [ i + 1 ] [ j ] == 1 ) neighbours ++ ; if ( j < grid [ i ] . length - 1 && grid [ i ] [ j + 1 ] == 1 ) neighbours ++ ; } } } return islands * 4 - neighbours * 2 ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def islandPerimeter ( self , grid ) : NEW_LINE INDENT row_num = len ( grid ) NEW_LINE if row_num == 0 | | len ( grid [ 0 ] ) == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT islands , overlaps = 0 , 0 NEW_LINE col_num = len ( grid [ 0 ] ) NEW_LINE for i in range ( row_num ) : NEW_LINE INDENT for j in range ( col_num ) : NEW_LINE INDENT if ( grid [ i ] [ j ] == 1 ) : NEW_LINE INDENT islands += 1 NEW_LINE if ( i < row_num - 1 & & grid [ i + 1 ] [ j ] == 1 ) : NEW_LINE INDENT overlaps += 1 NEW_LINE DEDENT if ( j < col_num - 1 & & grid [ i ] [ j + 1 ] == 1 ) : NEW_LINE INDENT overlaps += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return islands * 4 - overlaps * 2 NEW_LINE DEDENT DEDENT"
] |
leetcode_001_A | [
"public class Solution { public int [ ] twoSum ( int [ ] nums , int target ) { Map < Integer , Integer > map = new HashMap < > ( ) ; for ( int i = 0 ; i < nums . length ; i ++ ) { int x = nums [ i ] ; if ( map . containsKey ( target - x ) ) { return new int [ ] { map . get ( target - x ) , i } ; } map . put ( x , i ) ; } throw new IllegalArgumentException ( \" No β two β sum β solution \" ) ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def twoSum ( self , nums , target ) : NEW_LINE INDENT nums_index = [ ( v , index ) for index , v in enumerate ( nums ) ] NEW_LINE nums_index . sort ( ) NEW_LINE begin , end = 0 , len ( nums ) - 1 NEW_LINE while begin < end : NEW_LINE INDENT curr = nums_index [ begin ] [ 0 ] + nums_index [ end ] [ 0 ] NEW_LINE if curr == target : NEW_LINE INDENT return [ nums_index [ begin ] [ 1 ] , nums_index [ end ] [ 1 ] ] NEW_LINE DEDENT elif curr < target : NEW_LINE INDENT begin += 1 NEW_LINE DEDENT else : NEW_LINE INDENT end -= 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = Solution ( ) NEW_LINE print s . twoSum ( [ 3 , 2 , 4 ] , 6 ) NEW_LINE DEDENT"
] |
leetcode_003_A | [
"public class Solution { public int lengthOfLongestSubstring ( String s ) { int [ ] charMap = new int [ 256 ] ; Arrays . fill ( charMap , - 1 ) ; int i = 0 , maxLen = 0 ; for ( int j = 0 ; j < s . length ( ) ; j ++ ) { if ( charMap [ s . charAt ( j ) ] >= i ) { i = charMap [ s . charAt ( j ) ] + 1 ; } charMap [ s . charAt ( j ) ] = j ; maxLen = Math . max ( j - i + 1 , maxLen ) ; } return maxLen ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def lengthOfLongestSubstring ( self , s ) : NEW_LINE INDENT charMap = { } NEW_LINE for i in range ( 256 ) : NEW_LINE INDENT charMap [ i ] = - 1 NEW_LINE DEDENT ls = len ( s ) NEW_LINE i = max_len = 0 NEW_LINE for j in range ( ls ) : NEW_LINE INDENT if charMap [ ord ( s [ j ] ) ] >= i : NEW_LINE INDENT i = charMap [ ord ( s [ j ] ) ] + 1 NEW_LINE DEDENT charMap [ ord ( s [ j ] ) ] = j NEW_LINE max_len = max ( max_len , j - i + 1 ) NEW_LINE DEDENT return max_len NEW_LINE DEDENT DEDENT"
] |
leetcode_771_A | [
"import java . util . HashSet ; class Solution { public int numJewelsInStones ( String J , String S ) { int result = 0 ; HashSet jHash = new HashSet < > ( ) ; for ( int j = 0 ; j < J . length ( ) ; j ++ ) { jHash . add ( J . charAt ( j ) ) ; } for ( int s = 0 ; s < S . length ( ) ; s ++ ) { if ( jHash . contains ( S . charAt ( s ) ) ) { result ++ ; } } return result ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def numJewelsInStones ( self , J , S ) : NEW_LINE INDENT if len ( J ) == 0 or len ( S ) == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT j_set = set ( J ) NEW_LINE ans = 0 NEW_LINE for c in S : NEW_LINE INDENT if c in j_set : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT DEDENT"
] |
leetcode_538_A | [
"class Solution { public TreeNode convertBST ( TreeNode root ) { int sum = 0 ; TreeNode node = root ; Stack < TreeNode > stack = new Stack < TreeNode > ( ) ; while ( ! stack . isEmpty ( ) || node != null ) { while ( node != null ) { stack . add ( node ) ; node = node . right ; } node = stack . pop ( ) ; sum += node . val ; node . val = sum ; node = node . left ; } return root ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def convertBST ( self , root ) : NEW_LINE INDENT total = 0 NEW_LINE node = root NEW_LINE stack = [ ] NEW_LINE while stack or node is not None : NEW_LINE INDENT while node is not None : NEW_LINE INDENT stack . append ( node ) NEW_LINE node = node . right NEW_LINE DEDENT node = stack . pop ( ) NEW_LINE total += node . val NEW_LINE node . val = total NEW_LINE node = node . left NEW_LINE DEDENT return root NEW_LINE DEDENT DEDENT"
] |
leetcode_605_A | [
"public class Solution { public boolean canPlaceFlowers ( int [ ] flowerbed , int n ) { int count = 0 , curr ; for ( int i = 0 ; i < flowerbed . length ; i ++ ) { curr = flowerbed [ i ] ; if ( i - 1 >= 0 ) curr += flowerbed [ i - 1 ] ; if ( i + 1 < flowerbed . length ) curr += flowerbed [ i + 1 ] ; if ( curr == 0 ) { count ++ ; flowerbed [ i ] = 1 ; } if ( count >= n ) return true ; } return false ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def canPlaceFlowers ( self , flowerbed , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( len ( flowerbed ) ) : NEW_LINE INDENT curr = flowerbed [ i ] NEW_LINE if i - 1 >= 0 : NEW_LINE INDENT curr += flowerbed [ i - 1 ] NEW_LINE DEDENT if i + 1 < len ( flowerbed ) : NEW_LINE INDENT curr += flowerbed [ i + 1 ] NEW_LINE DEDENT if curr == 0 : NEW_LINE INDENT count += 1 NEW_LINE flowerbed [ i ] = 1 NEW_LINE if count >= n : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT DEDENT"
] |
leetcode_1310_A | [
"class Solution { public int [ ] xorQueries ( int [ ] arr , int [ ] [ ] queries ) { int [ ] res = new int [ queries . length ] , q ; for ( int i = 1 ; i < arr . length ; i ++ ) arr [ i ] ^= arr [ i - 1 ] ; for ( int i = 0 ; i < queries . length ; i ++ ) { q = queries [ i ] ; res [ i ] = q [ 0 ] > 0 ? arr [ q [ 0 ] - 1 ] ^ arr [ q [ 1 ] ] : arr [ q [ 1 ] ] ; } return res ; } }"
] | [
"class Solution : NEW_LINE INDENT def xorQueries ( self , arr : List [ int ] , queries : List [ List [ int ] ] ) -> List [ int ] : NEW_LINE INDENT pref = [ 0 ] NEW_LINE for e in arr : NEW_LINE INDENT pref . append ( e ^ pref [ - 1 ] ) NEW_LINE DEDENT ans = [ ] NEW_LINE for [ l , r ] in queries : NEW_LINE INDENT ans . append ( pref [ r + 1 ] ^ pref [ l ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT DEDENT"
] |
leetcode_437_A | [
"class Solution { private int result ; private HashMap < Integer , Integer > cache ; public int pathSum ( TreeNode root , int sum ) { result = 0 ; cache = new HashMap < Integer , Integer > ( ) ; cache . put ( 0 , 1 ) ; pathSumHelper ( root , sum , 0 ) ; return result ; } private void pathSumHelper ( TreeNode root , int target , int soFar ) { if ( root != null ) { int complement = soFar + root . val - target ; if ( cache . containsKey ( complement ) ) result += cache . get ( complement ) ; cache . put ( soFar + root . val , cache . getOrDefault ( soFar + root . val , 0 ) + 1 ) ; pathSumHelper ( root . left , target , soFar + root . val ) ; pathSumHelper ( root . right , target , soFar + root . val ) ; cache . put ( soFar + root . val , cache . get ( soFar + root . val ) - 1 ) ; } } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def pathSumHelper ( self , root , target , so_far , cache ) : NEW_LINE INDENT if root : NEW_LINE INDENT complement = so_far + root . val - target NEW_LINE if complement in cache : NEW_LINE INDENT self . result += cache [ complement ] NEW_LINE DEDENT cache [ so_far + root . val ] = cache . get ( so_far + root . val , 0 ) + 1 NEW_LINE self . pathSumHelper ( root . left , target , so_far + root . val , cache ) NEW_LINE self . pathSumHelper ( root . right , target , so_far + root . val , cache ) NEW_LINE cache [ so_far + root . val ] -= 1 NEW_LINE DEDENT return NEW_LINE DEDENT def pathSum ( self , root , sum ) : NEW_LINE INDENT self . result = 0 NEW_LINE self . pathSumHelper ( root , sum , 0 , { 0 : 1 } ) NEW_LINE return self . result NEW_LINE DEDENT DEDENT"
] |
leetcode_1323_A | [
"class Solution { public int maximum69Number ( int num ) { return Integer . valueOf ( String . valueOf ( num ) . replaceFirst ( \"6\" , \"9\" ) ) ; } }"
] | [
"class Solution : NEW_LINE INDENT def maximum69Number ( self , num : int ) -> int : NEW_LINE INDENT return ( str ( num ) . replace ( '6' , '9' , 1 ) ) NEW_LINE DEDENT DEDENT"
] |
leetcode_929_A | [
"import java . util . HashSet ; class Solution { public int numUniqueEmails ( String [ ] emails ) { HashSet < String > emailSet = new HashSet < > ( ) ; for ( String email : emails ) { String firstSplit [ ] = email . split ( \" @ \" ) ; String secondSplit [ ] = firstSplit [ 0 ] . replaceAll ( \" . \" , \" \" ) . split ( \" [ + ] \" ) ; emailSet . add ( secondSplit [ 0 ] + firstSplit [ 1 ] ) ; } return emailSet . size ( ) ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def numUniqueEmails ( self , emails ) : NEW_LINE INDENT email_set = set ( ) NEW_LINE for email in emails : NEW_LINE INDENT elements = email . split ( ' @ ' ) NEW_LINE email_set . add ( elements [ 0 ] . split ( ' + ' ) [ 0 ] . replace ( ' . ' , ' ' ) + elements [ 1 ] ) NEW_LINE DEDENT return len ( email_set ) NEW_LINE DEDENT DEDENT"
] |
leetcode_836_A | [
"class Solution { public boolean isRectangleOverlap ( int [ ] rec1 , int [ ] rec2 ) { return ( Math . min ( rec1 [ 2 ] , rec2 [ 2 ] ) > Math . max ( rec1 [ 0 ] , rec2 [ 0 ] ) && Math . min ( rec1 [ 3 ] , rec2 [ 3 ] ) > Math . max ( rec1 [ 1 ] , rec2 [ 1 ] ) ) ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def isRectangleOverlap ( self , rec1 , rec2 ) : NEW_LINE INDENT return not ( rec1 [ 2 ] <= rec2 [ 0 ] or rec1 [ 3 ] <= rec2 [ 1 ] or rec1 [ 0 ] >= rec2 [ 2 ] or rec1 [ 1 ] >= rec2 [ 3 ] ) NEW_LINE DEDENT DEDENT"
] |
leetcode_461_A | [
"class Solution { public int hammingDistance ( int x , int y ) { return Integer . bitCount ( x ^ y ) ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def hammingDistance ( self , x , y ) : NEW_LINE INDENT return bin ( x ^ y ) . count ( '1' ) NEW_LINE DEDENT DEDENT"
] |
leetcode_1089_A | [
"class Solution { public void duplicateZeros ( int [ ] arr ) { int movePos = 0 ; int lastPos = arr . length - 1 ; for ( int i = 0 ; i <= lastPos - movePos ; i ++ ) { if ( arr [ i ] == 0 ) { if ( i == lastPos - movePos ) { arr [ lastPos ] = 0 ; lastPos -- ; break ; } movePos ++ ; } } lastPos = lastPos - movePos ; for ( int i = lastPos ; i >= 0 ; i -- ) { if ( arr [ i ] == 0 ) { arr [ i + movePos ] = 0 ; movePos -- ; arr [ i + movePos ] = 0 ; } else { arr [ i + movePos ] = arr [ i ] ; } } } }"
] | [
"class Solution : NEW_LINE INDENT def duplicateZeros ( self , arr : List [ int ] ) -> None : NEW_LINE INDENT move_pos = 0 NEW_LINE last_pos = len ( arr ) - 1 NEW_LINE for i in range ( last_pos + 1 ) : NEW_LINE INDENT if i > last_pos - move_pos : NEW_LINE INDENT break NEW_LINE DEDENT if arr [ i ] == 0 : NEW_LINE INDENT if i == last_pos - move_pos : NEW_LINE INDENT arr [ last_pos ] = 0 NEW_LINE last_pos -= 1 NEW_LINE break NEW_LINE DEDENT move_pos += 1 NEW_LINE DEDENT DEDENT last_pos -= move_pos NEW_LINE for i in range ( last , - 1 , - 1 ) : NEW_LINE INDENT if arr [ i ] == 0 : NEW_LINE INDENT arr [ i + move_pos ] = 0 NEW_LINE move_pos -= 1 NEW_LINE arr [ i + move_pos ] = 0 NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i + move_pos ] = arr [ i ] NEW_LINE DEDENT DEDENT DEDENT DEDENT"
] |
leetcode_541_A | [
"class Solution { public String reverseStr ( String s , int k ) { char [ ] a = s . toCharArray ( ) ; for ( int start = 0 ; start < a . length ; start += 2 * k ) { int i = start , j = Math . min ( start + k - 1 , a . length - 1 ) ; while ( i < j ) { char tmp = a [ i ] ; a [ i ++ ] = a [ j ] ; a [ j -- ] = tmp ; } } return new String ( a ) ; } }"
] | [
"class Solution : NEW_LINE INDENT def reverseStr ( self , s : str , k : int ) -> str : NEW_LINE INDENT N = len ( s ) NEW_LINE ans = \" \" NEW_LINE position = 0 NEW_LINE while position < N : NEW_LINE INDENT nx = s [ position : position + k ] NEW_LINE ans = ans + nx [ : : - 1 ] + s [ position + k : position + 2 * k ] NEW_LINE position += 2 * k NEW_LINE DEDENT return ans NEW_LINE DEDENT DEDENT s1 = Solution ( ) NEW_LINE s = \" abcdefg \" NEW_LINE k = 2 NEW_LINE print ( s1 . reverseStr ( s , k ) ) NEW_LINE"
] |
leetcode_004_A | [
"public class Solution { public double findMedianSortedArrays ( int [ ] nums1 , int [ ] nums2 ) { int p1 = 0 , p2 = 0 , pos = 0 ; int ls1 = nums1 . length , ls2 = nums2 . length ; int [ ] all_nums = new int [ ls1 + ls2 ] ; double median = 0.0 ; while ( p1 < ls1 && p2 < ls2 ) { if ( nums1 [ p1 ] <= nums2 [ p2 ] ) all_nums [ pos ++ ] = nums1 [ p1 ++ ] ; else all_nums [ pos ++ ] = nums2 [ p2 ++ ] ; } while ( p1 < ls1 ) all_nums [ pos ++ ] = nums1 [ p1 ++ ] ; while ( p2 < ls2 ) all_nums [ pos ++ ] = nums2 [ p2 ++ ] ; if ( ( ls1 + ls2 ) % 2 == 1 ) median = all_nums [ ( ls1 + ls2 ) / 2 ] ; else median = ( all_nums [ ( ls1 + ls2 ) / 2 ] + all_nums [ ( ls1 + ls2 ) / 2 - 1 ] ) / 2.0 ; return median ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def findMedianSortedArrays ( self , nums1 , nums2 ) : NEW_LINE INDENT ls1 , ls2 = len ( nums1 ) , len ( nums2 ) NEW_LINE if ls1 < ls2 : NEW_LINE INDENT return self . findMedianSortedArrays ( nums2 , nums1 ) NEW_LINE DEDENT l , r = 0 , ls2 * 2 NEW_LINE while l <= r : NEW_LINE INDENT mid2 = ( l + r ) >> 1 NEW_LINE mid1 = ls1 + ls2 - mid2 NEW_LINE L1 = - sys . maxint - 1 if mid1 == 0 else nums1 [ ( mid1 - 1 ) >> 1 ] NEW_LINE L2 = - sys . maxint - 1 if mid2 == 0 else nums2 [ ( mid2 - 1 ) >> 1 ] NEW_LINE R1 = sys . maxint if mid1 == 2 * ls1 else nums1 [ mid1 >> 1 ] NEW_LINE R2 = sys . maxint if mid2 == 2 * ls2 else nums2 [ mid2 >> 1 ] NEW_LINE if L1 > R2 : NEW_LINE INDENT l = mid2 + 1 NEW_LINE DEDENT elif L2 > R1 : NEW_LINE INDENT r = mid2 - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return ( max ( L1 , L2 ) + min ( R1 , R2 ) ) / 2.0 NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = Solution ( ) NEW_LINE print s . findMedianSortedArrays ( [ 1 , 1 ] , [ 1 , 2 ] ) NEW_LINE DEDENT"
] |
leetcode_728_A | [
"class Solution { public List < Integer > selfDividingNumbers ( int left , int right ) { LinkedList list = new LinkedList ( ) ; for ( int i = left ; i <= right ; i ++ ) { if ( isSelfDiving ( i ) ) list . add ( i ) ; } return list ; } public boolean isSelfDiving ( int num ) { int digit = num % 10 ; int temp = num ; boolean isTrue = true ; while ( temp != 0 ) { if ( digit == 0 || num % digit != 0 ) { isTrue = false ; break ; } else { temp /= 10 ; digit = temp % 10 ; } } return isTrue ; } }"
] | [
"class Solution : NEW_LINE INDENT def selfDividingNumbers ( self , left : int , right : int ) -> List [ int ] : NEW_LINE INDENT return [ x for x in range ( left , right + 1 ) if all ( [ int ( i ) != 0 and x % int ( i ) == 0 for i in str ( x ) ] ) ] NEW_LINE DEDENT DEDENT"
] |
leetcode_012_A | [
"class Solution { public String intToRoman ( int num ) { Map < Integer , String > map = new HashMap ( ) ; map . put ( 1 , \" I \" ) ; map . put ( 5 , \" V \" ) ; map . put ( 10 , \" X \" ) ; map . put ( 50 , \" L \" ) ; map . put ( 100 , \" C \" ) ; map . put ( 500 , \" D \" ) ; map . put ( 1000 , \" M \" ) ; map . put ( 4 , \" IV \" ) ; map . put ( 9 , \" IX \" ) ; map . put ( 40 , \" XL \" ) ; map . put ( 90 , \" XC \" ) ; map . put ( 400 , \" CD \" ) ; map . put ( 900 , \" CM \" ) ; int [ ] sequence = { 1000 , 900 , 500 , 400 , 100 , 90 , 50 , 40 , 10 , 9 , 5 , 4 , 1 } ; StringBuffer sb = new StringBuffer ( ) ; for ( int i = 0 ; i < sequence . length ; i ++ ) { int base = sequence [ i ] ; while ( num >= base ) { sb . append ( map . get ( base ) ) ; num -= base ; } } return sb . toString ( ) ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def intToRoman ( self , num ) : NEW_LINE INDENT values = [ 1000 , 900 , 500 , 400 , 100 , 90 , 50 , 40 , 10 , 9 , 5 , 4 , 1 ] NEW_LINE symbols = [ \" M \" , \" CM \" , \" D \" , \" CD \" , \" C \" , \" XC \" , \" L \" , \" XL \" , \" X \" , \" IX \" , \" V \" , \" IV \" , \" I \" ] NEW_LINE roman = ' ' NEW_LINE i = 0 NEW_LINE while num > 0 : NEW_LINE INDENT k = num / values [ i ] NEW_LINE for j in range ( k ) : NEW_LINE INDENT roman += symbols [ i ] NEW_LINE num -= values [ i ] NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return roman NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = Solution ( ) NEW_LINE print s . intToRoman ( 90 ) NEW_LINE DEDENT"
] |
leetcode_973_A | [
"class Solution { private int dist ( int [ ] point ) { return point [ 0 ] * point [ 0 ] + point [ 1 ] * point [ 1 ] ; } public int [ ] [ ] kClosest ( int [ ] [ ] points , int K ) { PriorityQueue < int [ ] > pq = new PriorityQueue < int [ ] > ( ( p1 , p2 ) -> dist ( p2 ) - dist ( p1 ) ) ; for ( int [ ] p : points ) { pq . offer ( p ) ; if ( pq . size ( ) > K ) { pq . poll ( ) ; } } int [ ] [ ] res = new int [ K ] [ 2 ] ; while ( K > 0 ) { res [ -- K ] = pq . poll ( ) ; } return res ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def kClosest ( self , points , K ) : NEW_LINE INDENT return heapq . nsmallest ( K , points , key = lambda x : x [ 0 ] ** 2 + x [ 1 ] ** 2 ) NEW_LINE DEDENT DEDENT"
] |
leetcode_1304_A | [
"public int [ ] sumZero ( int n ) { int [ ] res = new int [ n ] ; for ( int i = 1 ; i < n ; i ++ ) { res [ i ] = i ; res [ 0 ] -= i ; } return res ; }"
] | [
"class Solution : NEW_LINE INDENT def sumZero ( self , n : int ) -> List [ int ] : NEW_LINE INDENT prefix_sum = 0 NEW_LINE res = [ ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT res . append ( i ) NEW_LINE prefix_sum = prefix_sum + i NEW_LINE DEDENT res . append ( - prefix_sum ) NEW_LINE return res NEW_LINE DEDENT DEDENT"
] |
leetcode_872_A | [
"class Solution { public boolean leafSimilar ( TreeNode root1 , TreeNode root2 ) { List < Integer > leaves1 = new ArrayList ( ) ; List < Integer > leaves2 = new ArrayList ( ) ; dfs ( root1 , leaves1 ) ; dfs ( root2 , leaves2 ) ; return leaves1 . equals ( leaves2 ) ; } public void dfs ( TreeNode node , List < Integer > leafValues ) { if ( node != null ) { if ( node . left == null && node . right == null ) leafValues . add ( node . val ) ; dfs ( node . left , leafValues ) ; dfs ( node . right , leafValues ) ; } } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def leafSimilar ( self , root1 , root2 ) : NEW_LINE INDENT if not root1 and not root2 : NEW_LINE INDENT return True NEW_LINE DEDENT leaf1 = [ ] NEW_LINE leaf2 = [ ] NEW_LINE self . dfs ( root1 , leaf1 ) NEW_LINE self . dfs ( root2 , leaf2 ) NEW_LINE if leaf1 == leaf2 : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def dfs ( self , node , leavels ) : NEW_LINE INDENT if not node : NEW_LINE INDENT return NEW_LINE DEDENT if not node . left and not node . right : NEW_LINE INDENT leavels . append ( node . val ) NEW_LINE DEDENT self . dfs ( node . left , leavels ) NEW_LINE self . dfs ( node . right , leavels ) NEW_LINE DEDENT DEDENT"
] |
leetcode_692_A | [
"class Solution { public List < String > topKFrequent ( String [ ] words , int k ) { Map < String , Integer > count = new HashMap ( ) ; for ( String word : words ) { count . put ( word , count . getOrDefault ( word , 0 ) + 1 ) ; } PriorityQueue < String > heap = new PriorityQueue < String > ( ( w1 , w2 ) -> count . get ( w1 ) . equals ( count . get ( w2 ) ) ? w2 . compareTo ( w1 ) : count . get ( w1 ) - count . get ( w2 ) ) ; for ( String word : count . keySet ( ) ) { heap . offer ( word ) ; if ( heap . size ( ) > k ) heap . poll ( ) ; } List < String > ans = new ArrayList ( ) ; while ( ! heap . isEmpty ( ) ) ans . add ( heap . poll ( ) ) ; Collections . reverse ( ans ) ; return ans ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def topKFrequent ( self , words , k ) : NEW_LINE INDENT count = collections . Counter ( words ) NEW_LINE heap = [ ( - freq , word ) for word , freq in count . items ( ) ] NEW_LINE heapq . heapify ( heap ) NEW_LINE return [ heapq . heappop ( heap ) [ 1 ] for _ in xrange ( k ) ] NEW_LINE DEDENT DEDENT"
] |
leetcode_011_A | [
"class Solution { public int maxArea ( int [ ] height ) { int maxArea = 0 ; int left = 0 ; int right = height . length - 1 ; while ( left < right ) { maxArea = Math . max ( maxArea , ( right - left ) * Math . min ( height [ left ] , height [ right ] ) ) ; if ( height [ left ] < height [ right ] ) left ++ ; else right -- ; } return maxArea ; } }"
] | [
"class Solution : NEW_LINE INDENT def maxArea ( self , height : List [ int ] ) -> int : NEW_LINE INDENT left , right = 0 , len ( height ) - 1 NEW_LINE result = 0 NEW_LINE while left < right : NEW_LINE INDENT result = max ( min ( height [ left ] , height [ right ] ) * ( right - left ) , result ) NEW_LINE if height [ left ] > height [ right ] : NEW_LINE INDENT right -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT left += 1 NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT DEDENT"
] |
leetcode_674_A | [
"class Solution { public int findLengthOfLCIS ( int [ ] nums ) { if ( nums . length == 0 ) return 0 ; int curr = 1 , ans = 1 ; for ( int i = 0 ; i < nums . length - 1 ; i ++ ) { if ( nums [ i ] < nums [ i + 1 ] ) { curr ++ ; if ( curr >= ans ) ans = curr ; } else { curr = 1 ; } } return ans ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def findLengthOfLCIS ( self , nums ) : NEW_LINE INDENT if not nums or len ( nums ) == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT ans = curr = 1 NEW_LINE for i in range ( len ( nums ) - 1 ) : NEW_LINE INDENT if nums [ i ] < nums [ i + 1 ] : NEW_LINE INDENT curr += 1 NEW_LINE ans = max ( ans , curr ) NEW_LINE DEDENT else : NEW_LINE INDENT curr = 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT DEDENT"
] |
leetcode_485_A | [
"class Solution { public int findMaxConsecutiveOnes ( int [ ] nums ) { int ans = 0 ; int curr = 0 ; for ( int i = 0 ; i < nums . length ; i ++ ) { if ( nums [ i ] == 1 ) { curr ++ ; if ( curr > ans ) ans = curr ; } else { curr = 0 ; } } return ans ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def findMaxConsecutiveOnes ( self , nums ) : NEW_LINE INDENT ans = 0 NEW_LINE curr = 0 NEW_LINE for n in nums : NEW_LINE INDENT if n == 1 : NEW_LINE INDENT curr += 1 NEW_LINE if curr > ans : NEW_LINE INDENT ans = curr NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT curr = 0 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT DEDENT"
] |
leetcode_007_A | [
"class Solution { public int reverse ( int x ) { if ( x == 0 ) return 0 ; long res = 0 ; while ( x != 0 ) { res = res * 10 + x % 10 ; if ( res > Integer . MAX_VALUE || res < Integer . MIN_VALUE ) return 0 ; x /= 10 ; } return ( int ) res ; } }"
] | [
"class Solution : NEW_LINE INDENT def reverse ( self , x ) : NEW_LINE INDENT res , isPos = 0 , 1 NEW_LINE if x < 0 : NEW_LINE INDENT isPos = - 1 NEW_LINE x = - 1 * x NEW_LINE DEDENT while x != 0 : NEW_LINE INDENT res = res * 10 + x % 10 NEW_LINE if res > 2147483647 : NEW_LINE INDENT return 0 NEW_LINE DEDENT x /= 10 NEW_LINE DEDENT return res * isPos NEW_LINE DEDENT DEDENT"
] |
leetcode_557_A | [
"public class Solution { public String reverseWords ( String s ) { String words [ ] = s . split ( \" β \" ) ; StringBuilder ans = new StringBuilder ( ) ; for ( String word : words ) ans . append ( new StringBuffer ( word ) . reverse ( ) . toString ( ) + \" β \" ) ; return ans . toString ( ) . trim ( ) ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def reverseWords ( self , s ) : NEW_LINE INDENT return ' β ' . join ( [ word [ : : - 1 ] for word in s . split ( ' β ' ) ] ) NEW_LINE DEDENT DEDENT"
] |
leetcode_238_A | [
"public class Solution { public int [ ] productExceptSelf ( int [ ] nums ) { int n = nums . length ; int [ ] res = new int [ n ] ; res [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { res [ i ] = res [ i - 1 ] * nums [ i - 1 ] ; } int right = 1 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { res [ i ] *= right ; right *= nums [ i ] ; } return res ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def productExceptSelf ( self , nums ) : NEW_LINE INDENT ans = [ 1 ] * len ( nums ) NEW_LINE for i in range ( 1 , len ( nums ) ) : NEW_LINE INDENT ans [ i ] = ans [ i - 1 ] * nums [ i - 1 ] NEW_LINE DEDENT right = 1 NEW_LINE for i in range ( len ( nums ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT ans [ i ] *= right NEW_LINE right *= nums [ i ] NEW_LINE DEDENT return ans NEW_LINE DEDENT DEDENT"
] |
leetcode_962_A | [
"import java . awt . Point ; class Solution { public int maxWidthRamp ( int [ ] A ) { int N = A . length ; Integer [ ] B = new Integer [ N ] ; for ( int i = 0 ; i < N ; ++ i ) B [ i ] = i ; Arrays . sort ( B , ( i , j ) -> ( ( Integer ) A [ i ] ) . compareTo ( A [ j ] ) ) ; int ans = 0 ; int m = N ; for ( int i : B ) { ans = Math . max ( ans , i - m ) ; m = Math . min ( m , i ) ; } return ans ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def maxWidthRamp ( self , A ) : NEW_LINE INDENT ans = 0 NEW_LINE m = float ( ' inf ' ) NEW_LINE for i in sorted ( range ( len ( A ) ) , key = A . __getitem__ ) : NEW_LINE INDENT ans = max ( ans , i - m ) NEW_LINE m = min ( m , i ) NEW_LINE DEDENT return ans NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = Solution ( ) NEW_LINE print s . maxWidthRamp ( [ 6 , 0 , 8 , 2 , 1 , 5 ] ) NEW_LINE print s . maxWidthRamp ( [ 9 , 8 , 1 , 0 , 1 , 9 , 4 , 0 , 4 , 1 ] ) NEW_LINE DEDENT"
] |
leetcode_002_A | [
"public class Solution { public ListNode addTwoNumbers ( ListNode l1 , ListNode l2 ) { ListNode dummyHead = new ListNode ( 0 ) ; ListNode p = l1 , q = l2 , curr = dummyHead ; int carry = 0 ; while ( p != null || q != null ) { int x = ( p != null ) ? p . val : 0 ; int y = ( q != null ) ? q . val : 0 ; int digit = carry + x + y ; carry = digit / 10 ; curr . next = new ListNode ( digit % 10 ) ; curr = curr . next ; if ( p != null ) p = p . next ; if ( q != null ) q = q . next ; } if ( carry > 0 ) { curr . next = new ListNode ( carry ) ; } return dummyHead . next ; } }"
] | [
"class ListNode ( object ) : NEW_LINE INDENT def __init__ ( self , x ) : NEW_LINE INDENT self . val = x NEW_LINE self . next = None NEW_LINE DEDENT DEDENT class Solution ( object ) : NEW_LINE INDENT def addTwoNumbers ( self , l1 , l2 ) : NEW_LINE INDENT carry = 0 NEW_LINE head = curr = ListNode ( 0 ) NEW_LINE while l1 or l2 : NEW_LINE INDENT val = carry NEW_LINE if l1 : NEW_LINE INDENT val += l1 . val NEW_LINE l1 = l1 . next NEW_LINE DEDENT if l2 : NEW_LINE INDENT val += l2 . val NEW_LINE l2 = l2 . next NEW_LINE DEDENT curr . next = ListNode ( val % 10 ) NEW_LINE curr = curr . next NEW_LINE carry = val / 10 NEW_LINE DEDENT if carry > 0 : NEW_LINE INDENT curr . next = ListNode ( carry ) NEW_LINE DEDENT return head . next NEW_LINE DEDENT DEDENT"
] |
leetcode_665_A | [
"class Solution { public boolean checkPossibility ( int [ ] nums ) { int brokenPoint = 0 ; for ( int i = 0 ; i < nums . length - 1 ; i ++ ) { if ( nums [ i ] > nums [ i + 1 ] ) { brokenPoint ++ ; if ( brokenPoint >= 2 ) return false ; if ( i - 1 < 0 || nums [ i - 1 ] <= nums [ i + 1 ] ) nums [ i ] = nums [ i + 1 ] ; else nums [ i + 1 ] = nums [ i ] ; } } return true ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def checkPossibility ( self , nums ) : NEW_LINE INDENT broken_num = 0 NEW_LINE for i in range ( len ( nums ) - 1 ) : NEW_LINE INDENT if ( nums [ i ] > nums [ i + 1 ] ) : NEW_LINE INDENT broken_num += 1 NEW_LINE if broken_num >= 2 : NEW_LINE INDENT return False NEW_LINE DEDENT if ( i - 1 < 0 or nums [ i - 1 ] <= nums [ i + 1 ] ) : NEW_LINE INDENT nums [ i ] = nums [ i + 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT nums [ i + 1 ] = nums [ i ] NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT DEDENT"
] |
leetcode_905_A | [
"class Solution { public int [ ] sortArrayByParity ( int [ ] A ) { int lo = 0 , hi = A . length - 1 ; while ( lo < hi ) { if ( A [ lo ] % 2 > A [ hi ] % 2 ) { int tmp = A [ hi ] ; A [ hi ] = A [ lo ] ; A [ lo ] = tmp ; } if ( A [ lo ] % 2 == 0 ) lo ++ ; if ( A [ hi ] % 2 == 1 ) hi -- ; } return A ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def sortArrayByParity ( self , A ) : NEW_LINE INDENT lo , hi = 0 , len ( A ) - 1 NEW_LINE while lo < hi : NEW_LINE INDENT if A [ lo ] % 2 > A [ hi ] % 2 : NEW_LINE INDENT A [ lo ] , A [ hi ] = A [ hi ] , A [ lo ] NEW_LINE DEDENT if A [ lo ] % 2 == 0 : lo += 1 NEW_LINE if A [ hi ] % 2 == 1 : hi -= 1 NEW_LINE DEDENT return A NEW_LINE DEDENT DEDENT"
] |
leetcode_547_A | [
"public class Solution { public void dfs ( int [ ] [ ] M , int [ ] visited , int i ) { for ( int j = 0 ; j < M . length ; j ++ ) { if ( M [ i ] [ j ] == 1 && visited [ j ] == 0 ) { visited [ j ] = 1 ; dfs ( M , visited , j ) ; } } } public int findCircleNum ( int [ ] [ ] M ) { int [ ] visited = new int [ M . length ] ; int count = 0 ; for ( int i = 0 ; i < M . length ; i ++ ) { if ( visited [ i ] == 0 ) { dfs ( M , visited , i ) ; count ++ ; } } return count ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def findCircleNum ( self , M ) : NEW_LINE INDENT visited = [ 0 ] * len ( M ) NEW_LINE count = 0 NEW_LINE for i in range ( len ( M ) ) : NEW_LINE INDENT if visited [ i ] == 0 : NEW_LINE INDENT self . dfs ( M , visited , i ) NEW_LINE count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT def dfs ( self , M , visited , i ) : NEW_LINE INDENT for j in range ( len ( M ) ) : NEW_LINE INDENT if M [ i ] [ j ] == 1 and visited [ j ] == 0 : NEW_LINE INDENT visited [ j ] = 1 NEW_LINE self . dfs ( M , visited , j ) NEW_LINE DEDENT DEDENT DEDENT DEDENT"
] |
leetcode_1260_A | [
"class Solution { public List < List < Integer > > shiftGrid ( int [ ] [ ] grid , int k ) { int [ ] [ ] newGrid = new int [ grid . length ] [ grid [ 0 ] . length ] ; int m = grid . length ; int n = grid [ 0 ] . length ; int true_k = k % ( m * n ) ; int move_i = true_k / n ; int move_j = true_k % n ; for ( int i = 0 ; i < grid . length ; i ++ ) { for ( int j = 0 ; j < grid [ i ] . length ; j ++ ) { int new_i = i + move_i ; int new_j = ( j + move_j ) % n ; if ( move_j + j >= n ) new_i ++ ; new_i %= m ; newGrid [ new_i ] [ new_j ] = grid [ i ] [ j ] ; } } List < List < Integer > > result = new ArrayList < > ( ) ; for ( int [ ] row : newGrid ) { List < Integer > listRow = new ArrayList < > ( ) ; result . add ( listRow ) ; for ( int v : row ) listRow . add ( v ) ; } return result ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def shiftGrid ( self , grid , k ) : NEW_LINE INDENT new_grid = [ [ 0 ] * len ( grid [ 0 ] ) for _ in range ( len ( grid ) ) ] NEW_LINE m = len ( grid ) NEW_LINE n = len ( grid [ 0 ] ) NEW_LINE true_k = k % ( m * n ) NEW_LINE move_i = true_k / n NEW_LINE move_j = true_k % n NEW_LINE for i in range ( m ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT new_i = i + move_i NEW_LINE if move_j + j >= n : NEW_LINE INDENT new_i += 1 NEW_LINE DEDENT new_i %= m NEW_LINE new_j = ( j + move_j ) % n NEW_LINE new_grid [ new_i ] [ new_j ] = grid [ i ] [ j ] NEW_LINE DEDENT DEDENT return new_grid NEW_LINE DEDENT DEDENT"
] |
leetcode_581_A | [
"public class Solution { public int findUnsortedSubarray ( int [ ] nums ) { Stack < Integer > stack = new Stack < Integer > ( ) ; int l = nums . length , r = 0 ; for ( int i = 0 ; i < nums . length ; i ++ ) { while ( ! stack . isEmpty ( ) && nums [ stack . peek ( ) ] > nums [ i ] ) l = Math . min ( l , stack . pop ( ) ) ; stack . push ( i ) ; } stack . clear ( ) ; for ( int i = nums . length - 1 ; i >= 0 ; i -- ) { while ( ! stack . isEmpty ( ) && nums [ stack . peek ( ) ] < nums [ i ] ) r = Math . max ( r , stack . pop ( ) ) ; stack . push ( i ) ; } return r - l > 0 ? r - l + 1 : 0 ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def findUnsortedSubarray ( self , nums ) : NEW_LINE INDENT stack = [ ] NEW_LINE l , r = len ( nums ) , 0 NEW_LINE for i in range ( len ( nums ) ) : NEW_LINE INDENT while len ( stack ) != 0 and nums [ stack [ - 1 ] ] > nums [ i ] : NEW_LINE INDENT l = min ( l , stack . pop ( ) ) NEW_LINE DEDENT stack . append ( i ) NEW_LINE DEDENT stack = [ ] NEW_LINE for i in range ( len ( nums ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT while len ( stack ) != 0 and nums [ stack [ - 1 ] ] < nums [ i ] : NEW_LINE INDENT r = max ( r , stack . pop ( ) ) NEW_LINE DEDENT stack . append ( i ) NEW_LINE DEDENT if r > l : NEW_LINE INDENT return r - l + 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT DEDENT"
] |
leetcode_784_A | [
"class Solution { public List < String > letterCasePermutation ( String S ) { List < StringBuilder > ans = new ArrayList ( ) ; ans . add ( new StringBuilder ( ) ) ; for ( char c : S . toCharArray ( ) ) { int n = ans . size ( ) ; if ( Character . isLetter ( c ) ) { for ( int i = 0 ; i < n ; ++ i ) { ans . add ( new StringBuilder ( ans . get ( i ) ) ) ; ans . get ( i ) . append ( Character . toLowerCase ( c ) ) ; ans . get ( n + i ) . append ( Character . toUpperCase ( c ) ) ; } } else { for ( int i = 0 ; i < n ; ++ i ) ans . get ( i ) . append ( c ) ; } } List < String > finalans = new ArrayList ( ) ; for ( StringBuilder sb : ans ) finalans . add ( sb . toString ( ) ) ; return finalans ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def letterCasePermutation ( self , S ) : NEW_LINE INDENT B = sum ( letter . isalpha ( ) for letter in S ) NEW_LINE ans = [ ] NEW_LINE for bits in xrange ( 1 << B ) : NEW_LINE INDENT b = 0 NEW_LINE word = [ ] NEW_LINE for letter in S : NEW_LINE INDENT if letter . isalpha ( ) : NEW_LINE INDENT if ( bits >> b ) & 1 : NEW_LINE INDENT word . append ( letter . lower ( ) ) NEW_LINE DEDENT else : NEW_LINE INDENT word . append ( letter . upper ( ) ) NEW_LINE DEDENT b += 1 NEW_LINE DEDENT else : NEW_LINE INDENT word . append ( letter ) NEW_LINE DEDENT DEDENT ans . append ( \" \" . join ( word ) ) NEW_LINE DEDENT return ans NEW_LINE DEDENT DEDENT"
] |
leetcode_922_A | [
"class Solution { public int [ ] sortArrayByParityII ( int [ ] A ) { int j = 1 ; for ( int i = 0 ; i < A . length ; i += 2 ) if ( A [ i ] % 2 == 1 ) { while ( A [ j ] % 2 == 1 ) j += 2 ; int tmp = A [ i ] ; A [ i ] = A [ j ] ; A [ j ] = tmp ; } return A ; } }"
] | [
"class Solution ( object ) : NEW_LINE INDENT def sortArrayByParityII ( self , A ) : NEW_LINE INDENT odd = 1 NEW_LINE for i in xrange ( 0 , len ( A ) , 2 ) : NEW_LINE INDENT if A [ i ] % 2 : NEW_LINE INDENT while A [ odd ] % 2 : NEW_LINE INDENT odd += 2 NEW_LINE DEDENT A [ i ] , A [ odd ] = A [ odd ] , A [ i ] NEW_LINE DEDENT DEDENT return A NEW_LINE DEDENT DEDENT"
] |