Skip to content

Instantly share code, notes, and snippets.

@yunho0130
Created October 11, 2016 06:01
Show Gist options
  • Save yunho0130/371db36384de0eddcefe16d4d14c3d01 to your computer and use it in GitHub Desktop.
Save yunho0130/371db36384de0eddcefe16d4d14c3d01 to your computer and use it in GitHub Desktop.
package yunho.com;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
try {
System.out.println("<Cracking The Coding Interview>");
int randomValue = (int)(Math.random()*300);
System.out.println(randomValue);
// Vector는 동기화에 안전하다.
ArrayList<String> myArr = new ArrayList<String>();
myArr.add("하나");
myArr.add("둘");
System.out.println(myArr.get(1));
// LinkedList에서의 iterator 사용
LinkedList<String> myLinkedList = new LinkedList<String>();
myLinkedList.add("둘2");
myLinkedList.addFirst("하나1");
Iterator<String> iter = myLinkedList.iterator();
while(iter.hasNext()){
System.out.println(iter.next());
}
//Hashmap 활용
HashMap<String, String> map = new HashMap<String, String>();
map.put("스미스", "010-2815-9190");
map.put("마이클", "+82-2933-9991");
System.out.println("스미스의 번호: ");
System.out.println(map.get("스미스"));
Scanner sc = new Scanner(System.in);
System.out.print("이름을 입력하세요: ");
System.out.println(map.get(sc.nextLine().toString()));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
// TODO: close all
}
};
// P.89 해시 테이블 Key를 Value에 대응 시키는 테이블
// 해시함수가 키를 정수값으로 대응시켜야 하는데, 이 경우에 정수값이 유일unique해야 함. sequence
//
// public HashMap<Integer, Student> buildMap(Student[] students){
// HashMap<Integer, Student> map = new HashMap<Integer, Student>();
// for(Student s : students) map.put(s.getId(), s);
// return map;
// };
//
// public HashMap<Integer, Target> builMap(Target[] targets){
// HashMap<Integer, Target> map = new HashMap<Integer, Target>();
// for(Target t : targets) map.put(t.getId(), t);
// return null;
// };
void mergesort(int[] array, int low, int high) {
if(low < high){
int middle = (low+high)/2;
mergesort(array, low, middle); // 왼쪽 절반을 정렬
mergesort(array, middle+1, high); // 오른쪽 절반을 정렬
merge(array, low, middle, high); // 병합
}
}
private void merge(int[] array, int low, int middle, int high) {
// TODO Auto-generated method stub
int[] temp = new int[array.length];
// 두 부분을 temp에 복사
for (int i = low; i <= high; i++ ){
temp[i] = array[i];
}
int tempLeft = low;
int tempRight = middle + 1;
int current = low;
// 본격적인 비교
while (tempLeft <= middle && tempRight <= high){
if (temp[tempLeft] <= temp[tempRight]){
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment