leetcode-month3-week10

Two Sum II Input array is sorted

package ygy.test.week10;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by guoyao on 2017/11/4.
 */
public class TwoSumIIInputarrayissorted {

    public int[] twoSum(int[] numbers, int target) {

        if(numbers == null || numbers.length == 0 ) return  null ;

        Map<Integer, Integer> map=new HashMap<>();
        for(int i = 0 ; i < numbers.length ; i ++) {
            int remain = target - numbers[i] ;
            if (map.containsKey(remain)) {
                return new int[]{map.get(remain)+1,i+1};
            }
            map.put(numbers[i], i);
        }
        return null ;
    }


    //leetcode answer
    public int[] twoSum_1(int[] numbers, int target) {
        int l = 0, r = numbers.length - 1;
        while (numbers[l] + numbers[r] != target) {
            if (numbers[l] + numbers[r] > target) r--;
            else l++;
        }
        return new int[]{l + 1, r + 1};
    }

    //leetcode answer
    public int[] twoSum_2(int[] num, int target) {
        int[] indice = new int[2];
        if (num == null || num.length < 2) return indice;
        int left = 0, right = num.length - 1;
        while (left < right) {
            int v = num[left] + num[right];
            if (v == target) {
                indice[0] = left + 1;
                indice[1] = right + 1;
                break;
            } else if (v > target) {
                right --;
            } else {
                left ++;
            }
        }
        return indice;
    }
}

Min Stack

package ygy.test.week10;

import java.util.Stack;

/**
 * Created by guoyao on 2017/11/3.
 */
public class MinStack {

    Integer min = Integer.MAX_VALUE ;
    Stack<Integer> stack=new Stack<>();

    /** initialize your data structure here. */
    public MinStack() {

    }

    public void push(int x) {
        if (x <= min) {
            stack.push(min);
            min = x ;
        }
        stack.push(x);
    }

    public void pop() {
        if(stack.pop().equals(min))
            min=stack.pop();
    }

    public int top() {
        return stack.peek() ;
    }

    public int getMin() {
        return  min ;
    }

    public static void main(String[] args) {
        MinStack minStack=new MinStack();
        minStack.push(512);
        minStack.push(-1024);
        minStack.push(-1024);
        minStack.push(512);
        minStack.pop();
        //System.out.println(minStack.top());
        System.out.println(minStack.getMin());
        minStack.pop();
        //System.out.println(minStack.top());
        System.out.println(minStack.getMin());
        minStack.pop();
        System.out.println(minStack.getMin());

    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(x);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */

Intersection of Two Linked Lists

package ygy.test.week10;

/**
 * Created by guoyao on 2017/11/3.
 */
public class IntersectionofTwoLinkedLists {


    public static void main(String[] agrs) {

    }

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {

        ListNode reverseA=reverse(headA);
        ListNode reverseB=reverse(headB);
        ListNode temp = null ;
        while (reverseA != null && reverseB != null) {
            if (reverseA == reverseB) {
                temp = reverseA ;
                reverseA =reverseA.next;
                reverseB = reverseB.next ;
                continue;
            }
        }
        return reverse(temp);
    }

    public  ListNode reverse(ListNode head) {
        ListNode cacheNode = null ;
        while (head != null) {
            ListNode temp = head.next ;
            head.next = cacheNode ;
            cacheNode = head ;
            head = temp;
        }
        return cacheNode;
    }

    //leetcode answer
    public ListNode getIntersectionNode_1(ListNode headA, ListNode headB) {
        //boundary check
        if(headA == null || headB == null) return null;

        ListNode a = headA;
        ListNode b = headB;

        //if a & b have different len, then we will stop the loop after second iteration
        while( a != b){
            //for the end of first iteration, we just reset the pointer to the head of another linkedlist
            a = a == null? headB : a.next;
            b = b == null? headA : b.next;
        }

        return a;
    }


    //leetcode answer
    public ListNode getIntersectionNode_2(ListNode headA, ListNode headB) {
        int lenA = length(headA), lenB = length(headB);
        // move headA and headB to the same start point
        while (lenA > lenB) {
            headA = headA.next;
            lenA--;
        }
        while (lenA < lenB) {
            headB = headB.next;
            lenB--;
        }
        // find the intersection until end
        while (headA != headB) {
            headA = headA.next;
            headB = headB.next;
        }
        return headA;
    }

    private int length(ListNode node) {
        int length = 0;
        while (node != null) {
            node = node.next;
            length++;
        }
        return length;
    }

}


class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val=x;
        next=null;
    }
}

/**
 * Definition for singly-linked list.
 */

Excel Sheet Column Title

package ygy.test.week10;

/**
 * Created by guoyao on 2017/11/5.
 */
public class ExcelSheetColumnTitle {

    public static void main(String[] agrs) {
        System.out.println(convertToTitle(30));
    }

    public  static  String convertToTitle(int n) {
        StringBuilder result = new StringBuilder();
        while(n>0){
            n--;
            result.insert(0, (char)('A' + n % 26));
            n /= 26;
        }
        return result.toString();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章