This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | (Binary search Solution) | |
| class Solution { | |
| public int searchInsert(int[] nums, int target) { | |
| //handle edge cases | |
| if(nums.length == 0 || nums == null){ | |
| return 0; | |
| } | |
| //do binary search on this problem | |
| int low = 0; | |
| int high = nums.length-1; | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | Explanation: | |
| 1) To find a tree tilt, we need to look for the difference between the leftSum and rightSum. | |
| 2) To find the value of leftSum and rightSum, we need to recursively pass in the left node and the right node into the method call valueSum | |
| 3) We create a gobal variable call result; | |
| 4) Then we create a method call valueSum. In this value sum, we declare a variable call leftSum and we pass in the current node.left and | |
| a rightSum and we pass in the current node.right. | |
| If the node is equal to null just return 0. | |
| 5) now we have the value of leftsum and right sum, we can find the tilt of the current tree. | |
| 6) The tilt is math.abs(leftsum-rightsum) | |
| 7) then, we need to return the value of the leftsum+rightsum+currentnode.value | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | Explanation | |
| 1) We can use a ArrayList to solve this problems | |
| 2) whenever a hit come, we store ths timestamp of the hit inside a ArrayList, letsay first hit is 400, we store 400, second hit 6000 we store 6000 | |
| third hit at 620, we store 620 and 4th hit at 623, we store at 623 | |
| 3) now, we request a count at 740. Our get hit function must loop through the arrayList size, we check if the timstamp minus the current i is greater than 300 or not. | |
| 4) If the item is greater than 300, we break the loop and return the size. | |
| 5) But this type of arrayList is O(n) time, which is not very efficient | |
| public class HitCounter { | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | Explanation: | |
| 1) We make a loop through the words array, we make a variable call len and a variable call numberOfWords | |
| 2) The len is the one use to compare with the maxWidth variable and the numberOfWords is use to determins the position of the string in the original array | |
| 3) use a while loop to check whether the string array reach the end of the array or not | |
| 4) make a check, if the i is still smaller than the length of the string and the len + numberOfWords + words[i].length is smaller than the maxWidth | |
| Here, the numberOfWords is use as the space between each string because we need at least a space between each string | |
| 5) if the length is still within, we increase the len by adding the length by the words[i].length | |
| 6) we increase the numOfWords by one and we also increase the i by one | |
| 7) after exiting this loop, where the condition now is the len exceed the maxWidth for the first time | |
| 8) now we need to handle the edge condition first, if the numerOfWords == 1 or the i==words.length | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | class Solution { | |
| public int findLength(int[] A, int[] B) { | |
| int dp[][] = new int[A.length+1][B.length+1]; | |
| int ans = 0; | |
| for(int i = 1; i<=A.length; i++ ) { | |
| for(int j = 1; j<=B.length; j++) { | |
| if( A[i-1] == B[j-1]){ | |
| dp[i][j] = 1 + dp[i-1][j-1]; | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | Explanation: | |
| 1) Make a list to hold the answer | |
| 2) Have a variable to store the length of the nums | |
| 3) Make a for loop where the length is the length of the nums | |
| 4) Then, make a while loop to check whether the nums reach the end of the string or not and whether the nums[i+1] == nums[i]+1 | |
| 5) If the condition is not met, we must make a check whether this is a edge case or not, the edge case is when the start == nums[i] where the | |
| range only contain of one number (if this condition is met, then we add only a space and the start number) | |
| 6) if the condition is not met, we add the patter according to the question | |
| class Solution { | |
| public List<String> summaryRanges(int[] nums) { | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | Explanation: | |
| 1) First, we do a for loop through the array, then for each of the item, we make a string.split(" ") to seperate the number and integer | |
| 2) we do a Integer.parseInt to store the number in a temporary variable and put the next full domain in another temp variable | |
| 3) then, we do checking, check whether the domain already inside the hashmap or not by using the containKey method, if it exist, we put the | |
| domain as the key and the value is the current map.get(domain) + temp num value | |
| 4) after doing this, we do a second for loop to loop through the domain to get the subdomain | |
| 5) by making a for loop, we check whether the current character is "." or not, if it is a dot, then we get the next char position position to the end of the domain length | |
| 6) using the substring method, then we do the same checking method. | |
| 7) Last, we make a linked list and make an entry set inside the java to loop throught and put it inside a linked list, then return the head of the linked list | |
| class Solution { | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | Explanation: | |
| 1) This is just a 35% faster solution | |
| 2) First, we create a set, then we create two pointers, one use to point at the head of the set and one use to point to the head of the string | |
| 3) We keep adding the char of the string to the set, and keep increasing the pointer pointed to the head of the string as we add the char to the set | |
| 4) when the next char we want to add into the set is already in the set, we dont add the char into the set, instead, we remove the char reapeated in the set | |
| 5) then we increase the pointer in the set to next. | |
| 6) then, we add the character that is pointed by the pointer a. | |
| class Solution { | |
| public int lengthOfLongestSubstring(String s) { | |
| if(s.equals("")) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | class Solution { | |
| public boolean isPalindrome(int x) { | |
| int original = x; | |
| int reversedNum = 0; | |
| while(x>0){ | |
| reversedNum = reversedNum*10 + x%10; | |
| x = x/10; | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | class Solution { | |
| public int reverse(int x) { | |
| long reverseNum = 0; | |
| while(x!=0){ | |
| reverseNum = reverseNum*10 + (x%10); | |
| x = x/10; | |
| } | |
| if(reverseNum >= Integer.MIN_VALUE && reverseNum <= Integer.MAX_VALUE) | |
| return (int) reverseNum; | |
| return 0; | 
NewerOlder