- Hanoi:
public void move(int n,char a,char b,char c){
if(n==1){
System.out.println("move from "+a+" to "+c);
}else{
move(n-1, a,c,b);
move(1, a, b, c);
move(n-1, b, a, c);
}
}
- Queue:
public void move(int n,char a,char b,char c){
if(n==1){
System.out.println("move from "+a+" to "+c);
}else{
move(n-1, a,c,b);
move(1, a, b, c);
move(n-1, b, a, c);
}
}
These days, I have been asked by some questions in interviews. These are not quite difficult questions, but I suppose I didn’t behavior well. There are some reasons I think, and I have to improve it:
And below are some questions I have to review:
Question:
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.
Hint:
How many majority elements could it possibly have?
Thinking:
This question is similar with Majority Element which can be solved by Boyer–Moore majority vote algorithm.
Solution:
public List<Integer> majorityElement(int[] nums) {
List<Integer> res = new ArrayList<Integer>();
int count1 = 0, count2 = 0, can1 = 0, can2 = 1;
for (int num: nums){
if (num == can1)
count1++;
else if (num == can2)
count2++;
else if (count1 == 0){
can1 = num;
count1 = 1;
}
else if (count2 == 0){
can2 = num;
count2 = 1;
}
else{
count1--;
count2--;
}
}
count1 = 0;
count2 = 0;
for (int num: nums){
if (num == can1)
count1++;
else if (num == can2)
count2++;
}
if (count1 > nums.length / 3)
res.add(can1);
if (count2 > nums.length / 3 && can1 != can2)
res.add(can2);
return res;
}
Question:
Implement a trie with insert, search, and startsWith methods.
Note:
You may assume that all inputs are consist of lowercase letters a-z.
Thinking:
I assume every node has no val, but edges do. So I keep a HashMap for everynode. What’s more, in order to determine whether this node is a leave, I add another boolean value.
Solution:
import java.util.HashMap;
class TrieNode {
HashMap<Character, TrieNode> edges = new HashMap<Character, TrieNode>();
boolean leave = false;
// Initialize your data structure here.
public TrieNode() {
}
}
public class Trie {
private TrieNode root;
public Trie() {
root = new TrieNode();
}
// Inserts a word into the trie.
public void insert(String word) {
TrieNode cur = root;
for (char c: word.toCharArray()){
if (!cur.edges.containsKey(c)){
TrieNode newNode = new TrieNode();
cur.edges.put(c, newNode);
cur = newNode;
}
else {
cur = cur.edges.get(c);
}
}
cur.leave = true;
}
// Returns if the word is in the trie.
public boolean search(String word) {
TrieNode cur = root;
for (char c: word.toCharArray()){
if (!cur.edges.containsKey(c)){
return false;
}
else{
cur = cur.edges.get(c);
}
}
return cur.leave;
}
// Returns if there is any word in the trie
// that starts with the given prefix.
public boolean startsWith(String prefix) {
TrieNode cur = root;
for (char c: prefix.toCharArray()){
if (!cur.edges.containsKey(c)){
return false;
}
else{
cur = cur.edges.get(c);
}
}
return true;
}
}
// Your Trie object will be instantiated and called as such:
// Trie trie = new Trie();
// trie.insert("somestring");
// trie.search("key");
Question:
Implement int sqrt(int x).
Compute and return the square root of x.
Thinking:
We can get the answer using bianry search from the range of integer.
Solution:
public int mySqrt(int x) {
if ( x <= 1 )
return x;
int low = 1;
int high = Integer.MAX_VALUE;
while (true){
int mid = low + (high - low) / 2; //in order to avoid overflow
if (mid > x / mid){
high = mid - 1;
}else{
if ( (mid+1) > x/(mid+1))
return mid;
low = mid + 1;
}
}
}