文章

顯示從 5月, 2023 起發佈的文章

MATH - 5c2 , 6c3... combinations

  public int combinationOf(int n, int r) { return factorial(n) / factorial(r) / factorial(n - r); // int -> overflow, long } // ni. 4! = 4 x 3 x 2 x 1 public int factorial(int n ) { if (n <= 1) return 1; return n * factorial(n - 1); }

JAVA - Static Import

When use used static method m that you can call the method directly. import static org . junit . jupiter . api . Assertions . assertEquals ; assertEquals ( 1 , 1 );

Classics - 2 Pointer Solution

  class Solution { public int getCommon ( int [ ] nums1 , int [ ] nums2 ) { int p1 = 0 ; int p2 = 0 ; while ( p1 < nums1 . length && p2 < nums2 . length ) { if ( nums1 [ p1 ] == nums2 [ p2 ] ) { return nums1 [ p1 ] ; } if ( nums1 [ p1 ] < nums2 [ p2 ] ) { p1 ++ ; } else { p2 ++ ; } } return - 1 ; } }