LeetCode – Power of Four (Java)

Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Java Solution 1 – Naive Iteration public boolean isPowerOfFour(int num) { while(num>0){ if(num==1){ return true; }   if(num%4!=0){ return false; }else{ num=num/4; } }   return false; }public boolean isPowerOfFour(int num) { while(num>0){ if(num==1){ return true; } … Read more

LeetCode – Reverse Vowels of a String (Java)

Write a function that takes a string as input and reverse only the vowels of a string. Java Solution this is a simple problem which can be solved by using two pointers scanning from beginning and end of the array. public String reverseVowels(String s) { ArrayList<Character> vowList = new ArrayList<Character>(); vowList.add(’a’); vowList.add(’e’); vowList.add(’i’); vowList.add(’o’); vowList.add(’u’); … Read more

LeetCode – Coin Change (Java)

Given a set of coins and a total money amount. Write a method to compute the smallest number of coins to make up the given amount. If the amount cannot be made up by any combination of the given coins, return -1.

For example:
Given [2, 5, 10] and amount=6, the method should return -1.
Given [1, 2, 5] and amount=7, the method should return 2.

Read more