Skip to content

Commit

Permalink
add some new file
Browse files Browse the repository at this point in the history
  • Loading branch information
cbowu committed Sep 4, 2017
1 parent 4e375b2 commit d705bdb
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 0 deletions.
122 changes: 122 additions & 0 deletions src/NestedIterator.java
Original file line number Diff line number Diff line change
@@ -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<NestedInteger> getList();
* }
*/
public class NestedIterator implements Iterator<Integer> {
Stack<NestedInteger> stack = new Stack<>();
public NestedIterator(List<NestedInteger> 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<NestedInteger> list;
private Integer integer;

public NestedInteger(List<NestedInteger> 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<NestedInteger> 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();
*/
19 changes: 19 additions & 0 deletions src/Solution108.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
18 changes: 18 additions & 0 deletions src/Solution217.java
Original file line number Diff line number Diff line change
@@ -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<Integer> set = new HashSet<>();
for(int i:nums){
if(set.contains(i))
return true;
else
set.add(i);
}
return false;
}
}
17 changes: 17 additions & 0 deletions src/Solution334.java
Original file line number Diff line number Diff line change
@@ -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<nums.length;k++){
if(nums[k]<=i)
i=nums[k];
else if(nums[k]<=j)
j=nums[k];
else
return true;
}
return false;
}
}

0 comments on commit d705bdb

Please sign in to comment.