<file_sep>package Leetcode;

/**
 * Created by qy on 2016/5/23.
 * task:
 * Given n non-negative integers a1,a2,...,an, where each represents a point at coordinate(i,ai).
 * n vertical lines are drawn such that the two endpoints of line i is at(i,ai) and (i,0).
 * Find two lines, which together with x-axis forms a container, such that the container contains the most water
 * note: You may not slant the container.
 */
public class ContainerWithMostWater {
    /**
     * created by qy
     * the last cast'time limit exceeded.
     *
     * understand the meaning of this question wrong...
     * really fool..
     * WTF!!
     *
     *
     */
    public int maxArea(int[] height){
        int count = 0;
        int left,right;
        int temp = 0;
        for (int i = 0; i < height.length;i++){
            left = i;
            right = i;
            while (height[left] >= height[i] || height[right] >= height[i]){
                temp = height[i] * (right - left);
                if (count < temp){
                    count = temp;
                }
                if (left ==0 && right== height.length -1 ){
                    break;
                }
                if (left != 0 && right != height.length -1 && height[right+1] < height[i] && height[left-1] < height[i]){

                    break;
                }
                if (left == 0 && right < height.length -1 && height[right+1] < height[i]) {

                    break;
                }
                if (right == height.length-1 && left >0 && height[left-1] < height[i]){
                    break;
                }

                if (left != 0 && height[left-1] >= height[i] ){
                    left--;
                }
                if (right != height.length - 1 && height[right+1] >= height[i]){
                    right ++;
                }
            }
        }


        return  count;
    }

    /**
     * really fast.
     * 2ms,99.86%
     * To find the biggest number, we don't neet to find it from the smallest,wo can begin with calculating from the result which wo can get easily.
     */
    public int maxArea1(int[] height) {
        int left=0;
        int right = height.length-1;
        int max=0,area;
        while(left<right) {
            int l = height[left];
            int r = height[right];
            if( l > r){
                area = (right-left) * r;
                while (height[--right] <= r);
            }else{
                area = (right-left) * l;
                while (height[++left] < l);
            }
            if (area > max) max = area;
        }
        return max;
    }

    /**
     * 4ms,78.29%
     */

    public int maxArea2(int[] height) {
        int max = 0, i = 0, j = height.length - 1;
        while(i < j)
            max = Math.max(max, (j - i) * (height[i] < height[j] ? height[i++] : height[j--]));
        return max;
    }


    public static void main(String[] args) {
        int[] arr = {1,2,3,4,2,1};
        int c = new ContainerWithMostWater().maxArea(arr);
        System.out.println(c);
    }
}

<file_sep>package Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by Yiy on 2016/7/6.
 */
public class internQuestionTest {
    public static void main(String[] args) {
        System.out.print("请输入1-9之间的任一数字");
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
        try {
            System.out.println(r.readLine());
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.print(1 + " ");
        System.out.print("\n");
        System.out.println("3");

    }
}
<file_sep>package JDBC.com.qy;

import java.sql.*;

/**
 * Created by Yiy on 2016/4/12.
 */
public class DBTest {
    private static  final String URL = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=GB2312";
    private static final String USER = "root";
    private static final String PASSWORD = "<PASSWORD>";
    private static Connection conn = null;
    static {
        try {
            //加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //获得连接
            conn = DriverManager.getConnection( URL , USER, PASSWORD);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static Connection getConn(){
        return conn;
    }
    public static void main(String[] args) {

        try {
            //加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //获得连接
            Connection conn = DriverManager.getConnection( URL , USER, PASSWORD);
            //通过数据库的连接操作数据库
            Statement stmt = conn.createStatement();
            ResultSet rs =  stmt.executeQuery("select * from admin;");
            while (rs.next()){
                System.out.println(rs.getString("adminname")+ "," + rs.getString("password") );
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
<file_sep>package Test;

/**
 * Created by Yiy on 2016/3/7.
 */
public class quickSort{
    static int[] a = {2, 7,4,6,9,0,8,5,3,1};
    public quickSort(){
        // a = this.a;
    }
    public static void sort(int[] a, int left ,int right){
        // int[] a = b;
        if (left < right){
            int temp = a[left];
            int i = left;
            int j = right;
            while (i != j) {
                while ( a[right] > temp && i <j){
                    j--;
                }
                while ( a[left] < temp && i < j ) {
                    i++;
                }
                if (i < j) {
                    int t = a[j];
                    a[j] = a[i];
                    a[i] = t;
                }
                a[left] = a[i];
                a[i] = temp;
                if (left != i-1) {
                    sort(a, left, i-1);
                }
                if (right != i+1) {
                    sort(a, i+1, right);
                }

            }
        }

    }
    public static void main(String[] args) {


//        for (int i = 0; i < N; i++) {
//            a[i] = Integer.valueOf(args[i]);
//        }
        int N = a.length;

        quickSort.sort(a,0, N-1);
        for (int i = 0; i < N;i++ ) {
            System.out.print(a[i]);
            System.out.print(" ");
        }

    }
}
<file_sep>package JavaNet;

/**
 * Created by Yiy on 2016/3/22.
 */
public class Net_exp2 {
    public static void main(String[] args) {
        try {
//            InputStream in = (new URL("http://www.baidu.com/img/baidu_sylogol.gif")).openStream();
//            FileOutputStream fo = new FileOutputStream("D://桌面/logo.gif");
//            BufferedOutputStream bo = new BufferedOutputStream(fo);
//            DataOutputStream dao = new DataOutputStream(bo);
//            int ch;
//            while ((ch = in.read()) != -1){
//                dao.write(ch);
//            }
//            bo.flush();
//            bo.close();
//            fo.close();
//            in.close();
            System.out.println(System.getProperty("file.encoding"));
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}
<file_sep>package ThinkingInJava;

import java.io.File;

/**
 * Created by Yiy on 2016/3/12.
 */
public class IOtest {
    public static void main(String[] args) {
        File file = new File("D:/桌面/test.txt");
        File file1 = new File("D:/桌面/ceshi");
        try{
//            file1.mkdirs();
//            file.createNewFile();
//            file1.createNewFile();
//            file1.mkdirs();
//            file1.delete();
//            System.out.println("该磁盘总容量为" +
//                    file.getTotalSpace()/(1024*1024*1024) + "G");
//            file.mkdirs();
//            System.out.println("文件名" + file.getName());
//            System.out.println("fulujing:" + file1.getParent());
        } catch (Exception e){
            e.printStackTrace();
        }
    }

}
<file_sep>package ThinkingInJava;

import java.io.*;

/**
 * Created by Yiy on 2016/3/13.
 */
class student implements Serializable{
    private String name;
    private int age;
    public student(String name, int age){
        super();
        this.name = name;
        this.age = age;
    }
    public String toString(){
        return "Student:[ name = " + name + "][ age = " + age + " ]";
    }
}
public class objectstreaam {
    public static void main(String[] args) {
        ObjectOutputStream output = null;
        ObjectInputStream input = null;
        try {
            output = new ObjectOutputStream(new FileOutputStream("D:/桌面/student.txt"));
            output.writeObject(new student("qinyi", 18));
            output.writeObject(new student("qinhi", 12));
            output.writeObject(new student("dddd", 232));
            input = new ObjectInputStream(new FileInputStream("D:/桌面/student.txt"));
            for (int i = 0; i < 3; i++){
                System.out.println(input.readObject());
            }

        } catch (IOException | ClassNotFoundException e){
            e.printStackTrace();
        } finally {
            try {
                output.close();
                input.close();
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
<file_sep>package JavaNet;

import java.io.*;

/**
 * Created by Yiy on 2016/3/19.
 */
public class outTest {
    public static void main(String[] args) throws IOException{
        File file = new File("D:/桌面/a1.txt");
        file.createNewFile();
        BufferedOutputStream buffOut = new BufferedOutputStream(new FileOutputStream(file));
        buffOut.write("java写文件测试".getBytes());
        buffOut.flush();
        buffOut.close();
        BufferedInputStream buffIn = new BufferedInputStream(new FileInputStream(file));
        byte[] buffer = new byte[1024];
        int in;
        while ( (in = buffIn.read(buffer)) != -1){
            String str = new String(buffer, 0 ,in);
            System.out.println(str);
        }
        if (buffIn != null){
            buffIn.close();
        }
        File file1 = new File("D:/桌面/a2.txt");
        file1.createNewFile();
        FileOutputStream fileOutputStream = new FileOutputStream(file1);
        fileOutputStream.write("java文件测完2".getBytes());
        fileOutputStream.close();
        FileInputStream fileInputStream = new FileInputStream(file1);
        byte[] bufferIn = new byte[1024];
        int in1;
        while ((in1 = fileInputStream.read(bufferIn)) != -1){
            String str1 = new String(bufferIn, 0 ,in1);
            System.out.println(str1);
        }
        fileInputStream.close();
        File file3 = new File("D:/桌面/a3.txt");
        FileWriter fw = new FileWriter(file3);
        fw.write("java文件测试3");
        fw.close();
        FileReader fr = new FileReader(file3);
        char[] ch = new char[100];
        int in2;
        while ((in2 = fr.read(ch)) != -1){
            String str2 = new String(ch, 0 ,in2);
            System.out.println(str2);
        }
        fr.close();
        File file2 = new File("D:/桌面/a2.txt");
        file2.createNewFile();
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(file2));
        outputStreamWriter.write("java文件测试1");
        outputStreamWriter.close();
        InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file2));
        char[] ch1 = new char[100];
        int in3;
        while ((in3 = inputStreamReader.read(ch1)) != -1){
            String str3 = new String(ch1, 0 ,in3);
            System.out.println(str3);
        }
        File file4 = new File("D:/桌面/a4.txt");
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file4)));
        bw.write("java文件测试4");
        bw.close();
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file4)));
        char[] ch3 = new char[100];
        int in4;
        while ((in4 = br.read(ch3)) != -1){
            String str3 = new String(ch3, 0, in4);
            System.out.println(str3);
        }






    }
}
<file_sep>package JDBC.com.qy.dao;

import JDBC.com.qy.DBTest;
import JDBC.com.qy.model.Goddess;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Yiy on 2016/4/12.
 */
public class GoddessDao {
    public void addGoddess(Goddess g){
        try {
            Connection conn = DBTest.getConn();
            String sql = "" +
                    "insert into goddess" +
                    "(user_name,sex,age,birthday,email,mobile," +
                    "create_user,create_date,update_user,update_date,isdel)" +
                    "values(" +
                    "?,?,?,?,?,?,?,current_date(),?,current_date(),?);";
            PreparedStatement ptmt = conn.prepareStatement(sql);
            ptmt.setString(1, g.getUser_name() );
            ptmt.setInt(2, g.getSex());
            ptmt.setInt(3, g.getAge());
            ptmt.setDate(4, new Date(g.getBirthday().getTime()));
            ptmt.setString(5, g.getEmail());
            ptmt.setString(6, g.getMobile());
            ptmt.setString(7,g.getCreate_user());
            ptmt.setString(8, g.getUpdate_user());
            ptmt.setInt(9, g.getIdel());

            ptmt.execute();
            System.out.println("新增女神成功");
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("新增女神失败");
        }

    }
    public void updateGoddess(Goddess g){
        try {
            Connection conn = DBTest.getConn();
            String sql = "" +
                    " update goddess " +
                    " set user_name = ?,sex = ?,age = ?,birthday = ?,email = ?,mobile = ?," +
                    " update_user = ?,update_date = current_date(),isdel = ?" +
                    " where id = ?;";
            PreparedStatement ptmt = conn.prepareStatement(sql);
            ptmt.setString(1, g.getUser_name() );
            ptmt.setInt(2, g.getSex());
            ptmt.setInt(3, g.getAge());
            ptmt.setDate(4, new Date(g.getBirthday().getTime()));
            ptmt.setString(5, g.getEmail());
            ptmt.setString(6, g.getMobile());
            ptmt.setString(7, g.getUpdate_user());
            ptmt.setInt(8, g.getIdel());
            ptmt.setInt(9,g.getId());
            ptmt.execute();

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
    public void delGoddess(Integer id){
        try {

            Connection conn = DBTest.getConn();
            String sql = " delete from goddess " +
                    " where id = ?;";
            PreparedStatement ptmt = conn.prepareStatement(sql);

            ptmt.setInt(1,id);

            ptmt.execute();

        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
    public List<Goddess> query(){
        List<Goddess> gs = new ArrayList<>();
        Goddess g = null;
        try {
            Connection conn = DBTest.getConn();
            StringBuilder sb = new StringBuilder();
            sb.append("select * from goddess; ");
            PreparedStatement ptmt = conn.prepareStatement(sb.toString());
            ResultSet rs = ptmt.executeQuery();
            while (rs.next()){
                g = new Goddess();
                g.setId(rs.getInt("id"));
                g.setUser_name(rs.getString("user_name"));
                g.setAge(rs.getInt("age"));
                g.setSex(rs.getInt("sex"));
                g.setBirthday(rs.getDate("birthday"));
                g.setEmail(rs.getString("email"));
                g.setMobile(rs.getString("mobile"));
                g.setCreate_user(rs.getString("create_user"));
                g.setCreate_date(rs.getDate("create_date"));
                g.setUpdate_user(rs.getString("update_user"));
                g.setUpdate_date(rs.getDate("update_date"));
                g.setIdel(rs.getInt("isdel"));
                gs.add(g);
//                System.out.println(rs.getString("user_name") + "," + rs.getInt("age"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return gs;
    }
    public List<Goddess> query(String name){
        List<Goddess> gs = new ArrayList<>();
        Goddess g = null;
        try {
            Connection conn = DBTest.getConn();
            StringBuilder sb = new StringBuilder();
            sb.append("select * from goddess ");
            sb.append(" where user_name  like ?; ");

            PreparedStatement ptmt = conn.prepareStatement(sb.toString());
            ptmt.setString(1, "%" +name+"%");
            ResultSet rs = ptmt.executeQuery();
            while (rs.next()) {
                g = new Goddess();
                g.setId(rs.getInt("id"));
                g.setAge(rs.getInt("age"));
                g.setSex(rs.getInt("sex"));
                g.setBirthday(rs.getDate("birthday"));
                g.setEmail(rs.getString("email"));
                g.setMobile(rs.getString("mobile"));
                g.setCreate_user(rs.getString("create_user"));
                g.setCreate_date(rs.getDate("create_date"));
                g.setUpdate_user(rs.getString("update_user"));
                g.setUpdate_date(rs.getDate("update_date"));
                g.setIdel(rs.getInt("isdel"));
                gs.add(g);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return gs;
    }
    public List<Goddess> query(List<Map<String,Object>> params){
        List<Goddess> gs = new ArrayList<>();
        Goddess g = null;
        try {
            Connection conn = DBTest.getConn();
            StringBuilder sb = new StringBuilder();
            sb.append("select * from goddess where 1=1 ");
            if (params != null && params.size() >0){
                for (int i = 0; i < params.size(); i++){
                    Map<String,Object> map = params.get(i);
                    sb.append(" and " + map.get("name") + " " + map.get("rela") + " " + map.get("value"));
                }
                sb.append(";");

            }
            System.out.println(sb.toString());
            PreparedStatement ptmt = conn.prepareStatement(sb.toString());

            ResultSet rs = ptmt.executeQuery();
            while (rs.next()) {
                g = new Goddess();
                g.setId(rs.getInt("id"));
                g.setUser_name(rs.getString("user_name"));
                g.setAge(rs.getInt("age"));
                g.setSex(rs.getInt("sex"));
                g.setBirthday(rs.getDate("birthday"));
                g.setEmail(rs.getString("email"));
                g.setMobile(rs.getString("mobile"));
                g.setCreate_user(rs.getString("create_user"));
                g.setCreate_date(rs.getDate("create_date"));
                g.setUpdate_user(rs.getString("update_user"));
                g.setUpdate_date(rs.getDate("update_date"));
                g.setIdel(rs.getInt("isdel"));
                gs.add(g);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return gs;
    }
    public Goddess get(Integer id){
        Goddess g = null;
        try {

            Connection conn = DBTest.getConn();
            String sql = ""+
                    " select * from goddess" +
                    " where id = ?;";
            PreparedStatement ptmt = conn.prepareStatement(sql);
            ptmt.setInt(1,id);
            ResultSet rs = ptmt.executeQuery();
            while (rs.next()){
                g = new Goddess();
                g.setId(rs.getInt("id"));
                g.setAge(rs.getInt("age"));
                g.setSex(rs.getInt("sex"));
                g.setBirthday(rs.getDate("birthday"));
                g.setEmail(rs.getString("email"));
                g.setMobile(rs.getString("mobile"));
                g.setCreate_user(rs.getString("create_user"));
                g.setCreate_date(rs.getDate("create_date"));
                g.setUpdate_user(rs.getString("update_user"));
                g.setUpdate_date(rs.getDate("update_date"));
                g.setIdel(rs.getInt("isdel"));



            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return g;
    }

}
<file_sep>package Leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Yiy on 2016/8/18.
 */
public class CombinationSum2 {
    /**
     * created by qy
     * 参考CombinationSum， 利用深度优先进行递归，直至找到所有符合条件的list,将list存入list<list>内饰判断是否list已经存在
     * 但又较大重复，故浪费一定的时间
     * 如果按照第二中方法进行修改，则将重复消除，时间变快
     */
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        List<List<Integer>> result = new ArrayList<>();
        addElement(candidates,result,new ArrayList<Integer>(),target,0);
        return result;

    }
    public void addElement(int[] candidates, List<List<Integer>> result, List<Integer> list, int target,int start){
        if (target > 0 ){
            for (int i = start; i < candidates.length && target>=candidates[i]; i++){
                if (i > start && candidates[i] == candidates[i-1]){
                    continue;
                }
                list.add(candidates[i]);
                addElement(candidates,result,list,target-candidates[i],i+1);
                list.remove(list.size() - 1);
            }
        } else if (target == 0){
//            List<Integer> newList = new ArrayList<>(list);
//            if (!result.contains(newList)){
//                result.add(newList);
//            }
            result.add(new ArrayList<Integer>(list));
        }
    }
// 78.84%，5ms
    public List<List<Integer>> combinationSum22(int[] candidates, int target) {
        List<List<Integer>> ans = new ArrayList<>();
        List<Integer> comb = new ArrayList<>();
        Arrays.sort(candidates); // need sort to make this work.
        combination(candidates, target, 0, comb, ans);
        return ans;
    }

    private void combination(int[] candi, int target, int start,
                             List<Integer> comb, List<List<Integer>> ans) {
        for (int i = start; i < candi.length; i++) {
            if (i > start && candi[i] == candi[i - 1]) //remove duplicates.
                continue;
            if (candi[i] == target) {
                //recursion exit.
                List<Integer> newComb = new ArrayList<>(comb);
                newComb.add(candi[i]);
                ans.add(newComb);
            } else if (candi[i] < target) {
                //continue to look for the rest.
                List<Integer> newComb = new ArrayList<>(comb);
                newComb.add(candi[i]);
                combination(candi, target - candi[i], i + 1, newComb, ans);
            } else
                break; //invalid path, return nothing.
        }
    }
    public static void main(String[] args) {
        int[] can = {10,1,2,7,6,1,5};
        List<List<Integer>> result = new CombinationSum2().combinationSum2(can,8);
        Iterator it = result.iterator();
        while (it.hasNext()){
            Iterator it1 = ((List<Integer>)it.next()).iterator();
            while (it1.hasNext()){
                System.out.print(it1.next() + ",");
            }
            System.out.println();
        }
    }
}
