From d705bdbac2c31ee32f3c3017e0bcbafcd6316720 Mon Sep 17 00:00:00 2001 From: cbowu Date: Mon, 4 Sep 2017 19:03:08 +0800 Subject: [PATCH] add some new file --- src/NestedIterator.java | 122 ++++++++++++++++++++++++++++++++++++++++ src/Solution108.java | 19 +++++++ src/Solution217.java | 18 ++++++ src/Solution334.java | 17 ++++++ 4 files changed, 176 insertions(+) create mode 100644 src/NestedIterator.java create mode 100644 src/Solution108.java create mode 100644 src/Solution217.java create mode 100644 src/Solution334.java diff --git a/src/NestedIterator.java b/src/NestedIterator.java new file mode 100644 index 0000000..5a9ddeb --- /dev/null +++ b/src/NestedIterator.java @@ -0,0 +1,122 @@ +/** + * Created by cuibowu on 2017/9/4. + */ + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Stack; + +/** + * // This is the interface that allows for creating nested lists. + * // You should not implement it, or speculate about its implementation + * public interface NestedInteger { + * + * // @return true if this NestedInteger holds a single integer, rather than a nested list. + * public boolean isInteger(); + * + * // @return the single integer that this NestedInteger holds, if it holds a single integer + * // Return null if this NestedInteger holds a nested list + * public Integer getInteger(); + * + * // @return the nested list that this NestedInteger holds, if it holds a nested list + * // Return null if this NestedInteger holds a single integer + * public List getList(); + * } + */ +public class NestedIterator implements Iterator { + Stack stack = new Stack<>(); + public NestedIterator(List nestedList) { + for(int i=nestedList.size()-1;i>=0;i--) + stack.push(nestedList.get(i)); + } + + @Override + public Integer next() { + hasNext(); + return stack.pop().getInteger(); + } + + @Override + public boolean hasNext() { + while (!stack.isEmpty()){ + NestedInteger temp = stack.peek(); + if(temp.isInteger()) + return true; + stack.pop(); + for(int i=temp.getList().size()-1;i>=0;i--){ + stack.push(temp.getList().get(i)); + } + } + return false; + } +} + + + +//NestedInteger + class NestedInteger { + private List list; + private Integer integer; + + public NestedInteger(List list){ + this.list = list; + } + + public void add(NestedInteger nestedInteger) { + if(this.list != null){ + this.list.add(nestedInteger); + } else { + this.list = new ArrayList(); + this.list.add(nestedInteger); + } + } + + public void setInteger(int num) { + this.integer = num; + } + + public NestedInteger(Integer integer){ + this.integer = integer; + } + + public NestedInteger() { + this.list = new ArrayList(); + } + + public boolean isInteger() { + return integer != null; + } + + public Integer getInteger() { + return integer; + } + + public List getList() { + return list; + } + + public String printNi(NestedInteger thisNi, StringBuilder sb){ + if(thisNi.isInteger()) { + sb.append(thisNi.integer); + sb.append(","); + } + sb.append("["); + for(NestedInteger ni : thisNi.list){ + if(ni.isInteger()) { + sb.append(ni.integer); + sb.append(","); + } + else { + printNi(ni, sb); + } + } + sb.append("]"); + return sb.toString(); + } +} +/** + * Your NestedIterator object will be instantiated and called as such: + * NestedIterator i = new NestedIterator(nestedList); + * while (i.hasNext()) v[f()] = i.next(); + */ \ No newline at end of file diff --git a/src/Solution108.java b/src/Solution108.java new file mode 100644 index 0000000..53e4192 --- /dev/null +++ b/src/Solution108.java @@ -0,0 +1,19 @@ +import java.util.Stack; + +/** + * Created by cuibowu on 2017/9/4. + */ +public class Solution108 { + public TreeNode sortedArrayToBST(int[] nums) { + return sortedArrayToBST(nums,0,nums.length-1); + } + private TreeNode sortedArrayToBST(int[] nums,int low,int high){ + if(low>high) + return null; + int mid =(int)Math.ceil(low +(double)(high-low)/2); + TreeNode root = new TreeNode(nums[mid]); + root.left=sortedArrayToBST(nums,low,mid-1); + root.right=sortedArrayToBST(nums,mid+1,high); + return root; + } +} diff --git a/src/Solution217.java b/src/Solution217.java new file mode 100644 index 0000000..865ba63 --- /dev/null +++ b/src/Solution217.java @@ -0,0 +1,18 @@ +import java.util.HashSet; +import java.util.Set; + +/** + * Created by cuibowu on 2017/9/4. + */ +public class Solution217 { + public boolean containsDuplicate(int[] nums) { + Set set = new HashSet<>(); + for(int i:nums){ + if(set.contains(i)) + return true; + else + set.add(i); + } + return false; + } +} diff --git a/src/Solution334.java b/src/Solution334.java new file mode 100644 index 0000000..6dcafd5 --- /dev/null +++ b/src/Solution334.java @@ -0,0 +1,17 @@ +/** + * Created by cuibowu on 2017/9/4. + */ +public class Solution334 { + public boolean increasingTriplet(int[] nums) { + int i=Integer.MAX_VALUE,j=Integer.MAX_VALUE; + for(int k=0;k