Monday, July 25, 2016

[LeetCode] 344. Reverse String

Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
Solution 1:
Approach: use StringBuilder.reverse() method
public class Solution {
    public String reverseString(String s) {
        StringBuilder sb=new StringBuilder(s);
        return sb.reverse().toString();
    }
}
Solution 2:
public class Solution {
    public String reverseString(String s) {
        StringBuilder sb=new StringBuilder();
        for(int i=s.length()-1; i>=0; i--){
            sb.append(s.charAt(i));
        }
        return sb.toString();
    }
}

[LeetCode] 3. Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring"pwke" is a subsequence and not a substring.
Solution 1: 
Approach: brute force
Time Complexity: O(n^2)
Space Complexity: O(n)
public class Solution {
    public int lengthOfLongestSubstring(String s) {
        if(s.length()==0||s==null) return 0;
        boolean[] exist=new boolean[256];
        int max=0;
        int start=0;
        for(int i=0; i<s.length(); i++){
            if(exist[s.charAt(i)]){
                for(int j=start; j<i; j++){
                    if(s.charAt(j)==s.charAt(i)){
                        start=j+1;
                        break;
                    }
                    exist[s.charAt(j)]=false;
                }
            }else{
                exist[s.charAt(i)]=true;
                max=Math.max(max, i-start+1);
            }
        }
        return max;
    }
}
Solution 2:
Approach: Hash Table
Time complexity: O(n)
Space complexity: O(n)
public class Solution {
    public int lengthOfLongestSubstring(String s) {
        if(s.length()==0||s==null) return 0;
        HashMap<Character, Integer> hashMap=new HashMap<Character, Integer>();
        int max=0;
        int start=0;
        for(int i=0; i<s.length(); i++){
            if(hashMap.containsKey(s.charAt(i))){
                max=Math.max(max, hashMap.size());
                while(hashMap.containsKey(s.charAt(i))){
                    hashMap.remove(s.charAt(start));
                    start++;
                }
                hashMap.put(s.charAt(i), i);
            }else{
                hashMap.put(s.charAt(i), i);
                max=Math.max(max, hashMap.size());
            }
        }
        return max;  
    }
}

[LeetCode] 2. Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Time Complexity: O(max(m,n))
Space Complexity: O(max(m,n))
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummy=new ListNode(0);
        ListNode tail=dummy;
        int carry=0;
        if(l1==null) return l2;
        if(l2==null) return l1;
        
        while(l1!=null||l2!=null){
            if(l1!=null){
                carry+=l1.val;
                l1=l1.next;
            }
            if(l2!=null){
                carry+=l2.val;
                l2=l2.next;
            }
            tail.next=new ListNode(carry%10);
            tail=tail.next;
            carry/=10;
        }
        if(carry==1){
            tail.next=new ListNode(1);
        }
        return dummy.next;
    }
}

Sunday, July 24, 2016

Helpful Job Hunting Sites

北美IT求职有用的网站总结

Coding practice sites
Technical interview questions sites
Resume apply sites
Startup company jobs posting sites
Code share sites
Online compiler sites
Online course sites
Other helpful sites

Saturday, July 23, 2016

[LeetCode] 1. Two Sum


Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution.
Example:
Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].


Solution 1:
Approach: Brute force
Time complexity: O(n^2)
Space complexity: O(1)

public class Solution {
    public int[] twoSum(int[] nums, int target) {
    
        int[] result=new int[2];
        if(nums==null||nums.length==0) return result;
        for(int i=0; i<nums.length; i++){
            for(int j=i+1; j<nums.length; j++){
                if(nums[i]+nums[j]==target){
                    result[0]=i;
                    result[1]=j;
                }
            }
        }
        return result;       
    }
}
Solution 2:
Approach: Hash Table
Time Complexity: O(n)
Space Complexity: O(n)

public class Solution {
    public int[] twoSum(int[] nums, int target) {
       
        int[] result=new int[2];
        if(nums==null||nums.length==0) return result;
        HashMap<Integer, Integer> hashMap=new HashMap<Integer, Integer>();
        for(int i=0; i<nums.length; i++){
            if(hashMap.containsKey(target-nums[i])){
                result[0]=i;
                result[1]=hashMap.get(target-nums[i]);
            }else{
                hashMap.put(nums[i], i);
            }
        }
        return result;
    }
}

블로그 시작!

오늘부터 블로그 시작하기로 함.
별거없음
그냥 내일상, 하루하루를 기록하고싶었을뿐.
근데 이거 어떻게 하는건지 아직은 잘 모르겠다
이상 첫 블로그는 끄으으으으읕!