Skip to content

Instantly share code, notes, and snippets.

@adrianlee
Last active December 10, 2015 09:08
Show Gist options
  • Save adrianlee/4412213 to your computer and use it in GitHub Desktop.
Save adrianlee/4412213 to your computer and use it in GitHub Desktop.

Answering the question

First answer should be easiest/simple. Either brute force or first thing that comes to mind

Any optimzation problem

  • Brute Force
  • loops
  • hashing
  • sorting
  • space vs time complexity trade offs

Sorting

Merge Sort External Merge Sort Quick Sort

Review

Code snippets

int number = Integer.parseInt("1234");
String str = String.valueOf(1234);

str.charAt(a) // n=0 to length()-1 

char[] characters = sentence.toCharArray();
byte[] byets = sentence.getBytes();

new StringBuilder().append(a).append(b).toString().toString();

String str = str.trim();

String sentence = "Hello World";
String[] words = sentence.split(" ");

"mesquite in your cellar".replace('e', 'o'); // replace by char
str.matches(regex)
str.replaceFirst(regex, repl) 
str.replaceAll(regex, repl)

"unhappy".substring(2) returns "happy"
"hamburger".substring(4, 8) returns "urge"
"to".concat("get").concat("her") returns "together"
  • StringBuffer is synchronized, StringBuilder is not.
  • If your text is going to change and use by multiple thread better to use StringBuffer. if your text is going to change but use by single thread then use StringBuilder.

Java is always pass-by-value. The difficult thing can be to understand that Java passes objects as references passed by value.

public void foo(Dog d) {
  d.name.equals("Max"); // true
  d = new Dog("Fifi");
  d.name.equals("Fifi"); // true
}

Dog aDog = new Dog("Max");
foo(aDog);
aDog.name.equals("Max"); // true

Links

Sites Careercup geeksforgeeks glassdoor

Questions

Articles

Guides

Maps

Blogs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment