Posts

Showing posts with the label Code

Arithmetic Triangle

Image
Arithmetic Triangle Difficulty: Easy Given an integer  numRows , return the first numRows of Arithmetic 's triangle . In  Arithmetic's triangle , each number is the sum of the two numbers directly above it as shown: Solution:  Java class   Solution   {      List < List < Integer >>   list ;      public   List < List < Integer >>   generate ( int   numRows )   {          list = new   ArrayList <> ();          if ( numRows == 0 )              return   list ;          list . add ( new   ArrayList < Integer >( Arrays . asList ( 1 )));          for ( int   i = 1 ; i < numRows ; i ++){        ...

Code Of The Day: Monk and Rotation

Monk and Rotation Monk loves to preform different operations on arrays, and so being the principal of Hackerearth School, he assigned a task to his new student Mishki. Mishki will be provided with an integer array  A  of size  N  and an integer  K  , where she needs to rotate the array in the right direction by K steps and then print the resultant array. As she is new to the school, please help her to complete the task. Input : The first line will consists of one integer  T  denoting the number of test cases. For each test case: 1) The first line consists of two integers  N  and  K ,  N  being the number of elements in the array and  K  denotes the number of steps of rotation. 2) The next line consists of  N  space separated integers , denoting the elements of the array  A . Output : Print the required array. Constraints : 1 ≤ T ≤ 20 1 ≤ N ≤ 10 5 0 ≤ K ≤ 10 6 0 ≤ A [ i ] ≤ 10 6 Sample Input 1 5 2 1...

Code of the Day: String to Integer (ATOI)

  String to Integer (atoi) Implement the  myAtoi(string s)  function, which converts a string to a 32-bit signed integer (similar to C/C++'s  atoi  function). The algorithm for  myAtoi(string s)  is as follows: Read in and ignore any leading whitespace. Check if the next character (if not already at the end of the string) is  '-'  or  '+' . Read this character in if it is either. This determines if the final result is negative or positive respectively. Assume the result is positive if neither is present. Read in next the characters until the next non-digit charcter or the end of the input is reached. The rest of the string is ignored. Convert these digits into an integer (i.e.  "123" -> 123 ,  "0032" -> 32 ). If no digits were read, then the integer is  0 . Change the sign as necessary (from step 2). If the integer is out of the 32-bit signed integer range  [-2 31 , 2 31  - 1] , then clamp the integer so th...