Java | Learn for Master - Part 2
  • Java for Loop

    In this post, I describe how to do for loop on an Java ArrayList. Array, and HashMap.

    Java Array for loop

    In the following example, we create an int array, then loop it to add numbers from 1 to 100.  Then we loop it again to calculate the sum of the numbers in the array. 

    Java List for loop

    We provide four ways to show how to loop a java List. 

    iterator.hasNext()
    Value 1
    Value 2
    Value 3
    for loop
    Value 1
    Value 2
    Value 3
    for loop advance
    Value 1
    Value 2
    Value 3
    while advance
    Value 1
    Value 2
    Value 3

    Java HashMap for loop

    If you’re only interested in the keys,

    [Read More...]
  • Remove Element from an Array (Java)

    LeetCode – Remove Element

    Given an array and a value, remove all instances of that value in place and return the new length.

    Do not allocate extra space for another array, you must do this in place with constant memory.

    The order of elements can be changed. It doesn’t matter what you leave beyond the new length.

    Example:
    Given input array nums = [3,2,2,3], val = 3

    Your function should return length = 2, with the first two elements of nums being 2.

    Analysis

    This problem is similar to another leetcode problem: Remove Duplicates from Sorted Array

    [Read More...]
  • Remove Duplicates from Sorted Array (Java)

    Leetcode: Remove Duplicates from Sorted Array

    Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

    Do not allocate extra space for another array, you must do this in place with constant memory.

    For example,
    Given input array nums = [1,1,2],

    Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn’t matter what you leave beyond the new length.

     Analysis
     

    We defined two indexes: pre and cur.

    [Read More...]
  • Leetcode – Longest common prefix

    Longest common prefix

    Write a function to find the longest common prefix string amongst an array of strings.

    Pay attention to the corner case: strs can be empty. 

    Analysis

    We define cur to record the char at current round that is recorded by si. 

    If si equals to the current string’s length, we return the substring from 0 to si. 

    At the beginning of each round, cur is set as null. 

    So when cur is null, we know this is the first string to check in current round. We set cur as the letter of the current string at index si 

    For the following string,

    [Read More...]
  • Leetcode – Roman to Integer (Java)

    Given a roman numeral, convert it to an integer.

    Input is guaranteed to be within the range from 1 to 3999.

    Analysis

    The rules to transfer a roman to an integer can be understood using the following examples:

    So for any two Roman letters in the form: Left Right

    if the left is smaller than the right, the result will be right – left.

    if the left is larger or equal to the right, the result will right + left.

    See this link for more examples of the roman numbers.

    [Read More...]
  • binary search – first bad version

    You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.

    Suppose you have n versions [1, 2, …, n] and you want to find out the first bad one, which causes all the following ones to be bad.

    You are given an API bool isBadVersion(version) which will return whether version is bad. Implement a function to find the first bad version.

    [Read More...]
  • System design interview- consistent hash

    consistent hash virtual node

    Consistent hashing is a simple yet powerful solution to a popular problem: how to locate a server in a distributed environment to store or get a value identified by a key, while handle server failures and network change?

    A simple method is number the servers from 0 to N – 1. To store or retrieve a value V, we can use Hash(V) % N to get the id of the server. 

    However, this method does not work well when 1) some servers failed in the network (e.g, N changes) or 2)  new machines are added to the server.

    [Read More...]
  • Quicksort algorithm

    Quicksort is one of the most famous sort algorithms because of its average good performance. Because of its importance and popularity, it is usually asked in technique interviews. It is also important to master QuickSort as its partitioning technique can also be used to find the Kth largest or smallest element of an array in O(n) time with O(1) space complexity. 

    How quickSort algorithm works

    Given an array A[s … e], a pivot is chosen to rearrange the array into two parts: ALeft and Aright. All the elements in ALeft are less than or equal to the pivot,

    [Read More...]
  • LeetCode – Merge Sorted Array without extra space

    Given two sorted integer arrays A and B, merge B into A as one sorted array.

    Note:
    You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B are m and n respectively.

    Analysis

    We have described how to merge two sorted arrays into a third sorted array. For this problem, as A is assumed to have enough space, we are not allowed to create a third array. 

    We cannot start the merge from the beginning of the two arrays,

    [Read More...]
  • Merge two sorted arrays algorithm (Java)

    Given two sorted arrays or lists, how to merge them into a sorted array?  

    This is a fundamental problem as it is a common requirement to merge two sorted arrays into one sorted array. The algorithm of merging two sorted arrays is also the basics of the one of the most famous sort algorithms: merge sort.

    Suppose there are two sorted arrays A and B, and we want to merge them into a third Array C.

    We define three indexes a, b, c, which points to the beginning of the three arrays A, B,

    [Read More...]
Page 2 of 41234