Saturday, August 20, 2016

LeetCode Online Judge-3. Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring"pwke" is a subsequence and not a substring.

//Java Program: Longest Substring Without Repeating Characters
public class Solution {
    public int lengthOfLongestSubstring(String s) {
        
        int longestLength = 0;
ArrayList<Character> list = new ArrayList<Character>();
for(int i=0;i<s.length();++i) {
if (list.contains(s.charAt(i))) {
while(list.get(0)!=s.charAt(i)) {
list.remove(0);
}
list.remove(0);
}
list.add(s.charAt(i));
if (list.size() > longestLength) {
   longestLength = list.size();
}
}
return longestLength;
    }
}

Thursday, August 18, 2016

Java Program: To Reconstruct A Binary Tree

This program is to reconstruct a binary tree according to its preorder and inorder traversals.

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
        if (pre==null)
             return null;
        TreeNode treeNode = new TreeNode(pre[0]);
        int i = 0;
        while (i<in.length&&in[i]!=pre[0])
            ++i;
        if (i>=in.length) {
        return null;
        }
        if (i>=1) {
        int[] preleft = new int[i];
        int[] inleft = new int[i];
        for(int j=0;j<i;++j) {
        preleft[j] = pre[j+1];
        inleft[j] = in[j];
        }
        treeNode.left = reConstructBinaryTree(preleft, inleft);
        } else {
        treeNode.left = null;
        }
        if (i<pre.length-1) {
       int[] preright = new int[pre.length-i-1];
       int[] inright = new int[pre.length-i-1];
       for(int j=0;j<pre.length-i-1;++j) {
           preright[j] = pre[i+j+1];
           inright[j] = in[i+j+1];
       }
       treeNode.right = reConstructBinaryTree(preright, inright);
        } else {
        treeNode.right = null;
        }
        return treeNode;
    }
}

Wednesday, August 17, 2016

Java Program: Replace Space With "%20" In A String

Here is a java program to replace space with "%20" in a string. For example, the string "We Are Happy" will be changed to "We%20Are%20Happy".
Note that a space is a character while "%20" is a string.

//Replace Space With "%20" In A String
public class Solution {
    public String replaceSpace(StringBuffer str) {
     StringBuffer s = new StringBuffer();
 
  for(int i=0;i<str.length();++i) {
   if (str.charAt(i)==' ') {
    s.append("%20");
   } else {
    s.append(str.charAt(i));
   }
  }
        return s.toString();
    }
}

Monday, August 8, 2016

Android Studio Project: Outside News

The previous projects I have made are mainly about logical thinking. Today, I share this project, Outside News to all of you, which is full of beautiful user interfaces. I'm sorry that it's only a demo without news recources and strong features. If you want to have a try, just do it!

Installation package

Project site

Is this project with the use of Viewpager, ListView, PopupWindow, PopupMenu and many other views more colorful? What's more, it uses SharedPreferences and SQLite database to store some data persistently. And it involves multithread programming and asynchronous message processing. The use of vector is also one of my harvests.