博文

155. Min Stack

这题居然想了半天没想出来…… 讲解: https://www.cnblogs.com/grandyang/p/4091064.html 关于stack.pop()返回值类型不能直接==的一些参考: 关于Integer的==和equals: https://stackoverflow.com/questions/3637936/java-integer-equals-vs

20. Valid Parentheses

OA: class Solution { public boolean isValid (String s) { char [] stack = new char [s.length()]; int head = 0 ; for ( char c : s.toCharArray()) { if (c == '(' ) { stack[head++] = c; } else if (c == '[' ) { stack[head++] = c; } else if (c == '{' ) { stack[head++] = c; } else if (c == ')' ) { if (head == 0 ) return false ; if (stack[--head] != '(' ) return false ; } else if (c == ']' ) { if (head == 0 ) return false ; if (stack[--head] != '[' ) return false ; } else if (c == '}' ) { if (head == 0 ) return false ; if (stack[--head] != '{' ) return false ; } } return head == 0 ; } } OA比MA快一倍。 String有个toCharArray()...

225 Implement Stack using Queues

OA: class MyStack { Queue<Integer> queue; /** Initialize your data structure here. */ public MyStack () { queue = new LinkedList<>(); } /** Push element x onto stack. */ public void push ( int x) { queue.offer(x); int k = queue.size() - 1 ; while (k > 0 ) { queue.offer(queue.poll()); k--; } } /** Removes the element on top of the stack and returns that element. */ public int pop () { return queue.poll(); } /** Get the top element. */ public int top () { return queue.peek(); } /** Returns whether the stack is empty. */ public boolean empty () { return queue.isEmpty(); } } oa是push一次就逆序排一次(相当于每次push一个之后都把新push进去的那个排到最前面,这样下次pop的时候就是pop的这一个。) ma在top和pop的时候才把最后一个调到前面来,而且每次都要重新排。(感觉其实差不多,要看这个这个stack是pop和top用得多还是push用得多) MA: class MyStack { ArrayDeque<Integer> ...

232. Implement Queue using Stacks

MA: class MyQueue {     Stack<Integer> simQ=new Stack<Integer>();     Stack<Integer> buffer=new Stack<Integer>();     /** Initialize your data structure here. */     public MyQueue() {     }         /** Push element x to the back of queue. */     public void push(int x) {         simQ.push(x);     }         /** Removes the element from in front of queue and returns that element. */     public int pop() {         while(!simQ.empty()){             buffer.push(simQ.pop());         }         int temp=buffer.pop();         while(!buffer.empty()){             simQ.push(buffer.pop());         }         return temp;     } ...

167. Two Sum II - Input array is sorted

class Solution {     public int[] twoSum(int[] numbers, int target) {         int[] outcome=new int[2];         int outlp;         outlp=Integer.MAX_VALUE;         for(int i=0;i<numbers.length;i++){             if(numbers[i]==outlp) continue;             else outlp=numbers[i];             for(int j=i+1;j<numbers.length;j++){                 if(numbers[i]+numbers[j]==target){                     outcome[0]=i+1;                     outcome[1]=j+1;                     return outcome;                 }             }       ...

head first设计模式 总结

第一章:策略模式 所有子类都有的方法放在共有的抽象父类中;所有子类继承这个父类。 部分子类共有的方法,假如有不同实现,就声明一个接口,用多个专门的类来实现这个接口,并作为该部分子类的属性。子类用到哪个类的实现就在自身实例化那个类。

Java HashMap详解

http://blog.csdn.net/caihaijiang/article/details/6280251

496. Next Greater Element I

MA: class Solution {     public int[] nextGreaterElement(int[] nums1, int[] nums2) {         int[] nums3=new int[nums1.length];         for(int i=0;i<nums1.length;i++){             int max=-1;             nums3[i]=max;             int target=nums1[i];             boolean findTarget=false;             for(int j=0;j<nums2.length;j++){                 if(nums2[j]==nums1[i] && findTarget==false){                     findTarget=true;                     continue;                 }                 if(nums2[j]>target&&findTarget==true){ ...

Java 自动打包解包

http://blog.csdn.net/u011599609/article/details/40921543 这里的装箱应该理解为 封装对象 ,即把基础数据类型(如 int)转换成基础类型封装类的对象(如 new Integer()) 拆箱就是装箱的反过程,即把基础类型封装类的对象(如 new Integer())转换为基础数据类型(如 int)。 例子如下: 装箱: Integer a = new Integer() ; a = 100 ; //1.5以前不支持为对象如此赋值 拆箱: int b = new Integer(100) ; 自动拆装箱 是JDK1.5中新增加的内容 ,它可以将基本数据类型自动转换为它的包装类。或者相反。 基本数据类型与包装类对应关系如下: short Short int Integer long Long char Char float Float double Double boolean Boolean 注意的问题: 众所周知java对原始数据类型如int、char、long等基本数据类型有自动打包成相应的复合类型Integer、Character、Long等的机制;也可以将复合类型自动转换为原始类型。 这取决于程序要进行怎样的处理。如: int i = 0; Integer obj = i; ----- Integer obj = new Integer(0); int i = obj; 但是并不是所有的地方都会进行自动的打包和解包。有些地方存在“陷阱”,不注意的话,会产生问题。如: java.util包中的List接口有两个remove方法,一个接收int参数,表示删除指定位置的元素;一个接收Object参数,表示删除指定的元素(内部使用equals进行元素相等的判断)。 如果有一个List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); 如果你调用list.remove(0);表示删除的是位置0上的元素即1。 如果你调用list.remove(new Integer(0));表...

682. Baseball Game

MA: class Solution {     public int calPoints(String[] ops) {         Integer score=0;         Stack<Integer> stack=new Stack<Integer>();         for(int i=0;i<ops.length;i++){             switch(ops[i]){             case "D":                 stack.push(2*stack.peek());                 break;             case "+":                 int temp=stack.pop();                 int insertBuffer=temp+stack.peek();                 stack.push(temp);                 stack.push(insertBuffer);                 break;       ...

java Sting 比较

// These two have the same value new String ( "test" ). equals ( "test" ) // --> true // ... but they are not the same object new String ( "test" ) == "test" // --> false // ... neither are these new String ( "test" ) == new String ( "test" ) // --> false // ... but these are because literals are interned by // the compiler and thus refer to the same object "test" == "test" // --> true // ... string literals are concatenated by the compiler // and the results are interned. "test" == "te" + "st" // --> true // ... but you should really just call Objects.equals() Objects . equals ( "test" , new String ( "test" )) // --> true Objects . equals ( null , "test" ) // --> false