id
stringlengths
2
6
java
stringlengths
48
5.92k
python
stringlengths
33
11.1k
T40400
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 ; } } retur...
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_LI...
T40401
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_num...
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 ...
T40402
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 ; boole...
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
T40403
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 " )...
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 = ' ...
T40404
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 ...
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
T40405
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 DEDEN...
T40406
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 > l...
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_LI...
T40407
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 ...
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 ) ...
T40408
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 --...
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...
T40409
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...
T40410
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...
T40411
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_...
T40412
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
T40413
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 [...
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 ) : NE...
T40414
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 ) { ...
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 DE...
T40415
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 +...
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 o...
T40416
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 ] ; els...
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...
T40417
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 [ ...
T40418
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 ] ; ...
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 cou...
T40419
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 ...
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 NE...
T40420
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 ( ) )...
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 ( )...
T40421
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 Strin...
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 ( ) :...
T40422
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 D...