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 ; } } 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
|
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_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
|
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 ; 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
|
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 " ) ; 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
|
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 . 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
|
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 DEDENT DEDENT
|
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 > 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
|
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 ( 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
|
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 -- ; } 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
|
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 ans = max ( ans , curr ) NEW_LINE DEDENT else : NEW_LINE INDENT curr = 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT DEDENT
|
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_LINE DEDENT DEDENT return ans NEW_LINE DEDENT DEDENT
|
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_LINE DEDENT return res * isPos NEW_LINE DEDENT DEDENT
|
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 [ 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
|
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 ) { 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
|
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 + 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
|
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 ] ; 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
|
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 [ hi ] % 2 == 1 : hi -= 1 NEW_LINE DEDENT return A NEW_LINE DEDENT DEDENT
|
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 ] ; 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
|
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 < 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
|
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 ( ) ) ; 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
|
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 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
|
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 DEDENT DEDENT return A NEW_LINE DEDENT DEDENT
|