leetcode-month3-week10
来源:互联网 发布:js input 自定义属性 编辑:程序博客网 时间:2024/05/29 18:59
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(); }}
阅读全文
0 0
- leetcode-month3-week10
- Leetcode Week10
- Month3
- LeetCode题解 week10
- week10
- [leetcode 377. Combination Sum IV]medium|week10
- week10-leetcode #24-Swap-Nodes-in-Pairs
- LeetCode Week10: Edit Distance, Unique Binary Search Tree(I & II)
- 算法设计Week10 LeetCode Algorithms Problem #213 House Robber II
- [LeetCode-Algorithms-55] "Jump Game" (2017.11.7-WEEK10)
- [LeetCode-Algorithms-24] "Swap Nodes in Pairs" (2017.11.7-WEEK10)
- [LeetCode-Algorithms-647] "Palindromic Substrings" (2017.11.7-WEEK10)
- LeetCode#712 Minimum ASCII Delete Sum for Two Strings (week10)
- Algorithm-week10
- Leetcode: 515.Find Largest Value in Each Tree Row(Week10, Medium)
- C程序设计week10作业
- week10--作业第二题~
- Week10-乘法表
- 程序员读《三体》后看到了什么?
- 清风遥寄
- 【LeetCode】419.Battleships in a Board(Medium)解题报告
- leetcode Palindrome Partitioning
- Redis Cluster集群的搭建与实践
- leetcode-month3-week10
- toString()方法对数字与关键字parseInt()方法对带有数字的字符串中数字进制之间转换-学习笔记
- java String 中 startsWith() 对字符串开头进行过滤 endsWith()对字符串结尾进行过滤
- LeetCode 279. Perfect Squares--动态规划
- 添加自定义vue全局方法,同时给自定义的方法 传递component调用其方法
- 计算一个整数各个位的和,直到和<10输出
- Linux下开发-守护进程(daemon)
- 【NOIP2017提高A组冲刺11.5】轰炸
- Luogu P2722 总分 Score Inflation