Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Administrator committed Aug 21, 2015
0 parents commit 82a4297
Show file tree
Hide file tree
Showing 72 changed files with 7,996 additions and 0 deletions.
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/copyright/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions CodingInterviews.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
143 changes: 143 additions & 0 deletions src/Test02.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/**
* Author: 王俊超
* Date: 2015-04-21
* Time: 13:58
* Declaration: All Rights Reserved !!!
*/
public class Test02 {

/**
* 单例模式,懒汉式,线程安全
*/
public static class Singleton {
private final static Singleton INSTANCE = new Singleton();

private Singleton() {

}

public static Singleton getInstance() {
return INSTANCE;
}
}

/**
* 单例模式,饿汉式,线程不安全
*/
public static class Singleton2 {
private static Singleton2 instance = null;

private Singleton2() {

}

public static Singleton2 getInstance() {
if (instance == null) {
instance = new Singleton2();
}

return instance;
}
}


/**
* 单例模式,饿汉式,线程安全,多线程环境下效率不高
*/
public static class Singleton3 {
private static Singleton3 instance = null;

private Singleton3() {

}

public static synchronized Singleton3 getInstance() {
if (instance == null) {
instance = new Singleton3();
}

return instance;
}
}

/**
* 单例模式,饿汉式,变种,线程安全
*/
public static class Singleton4 {
private static Singleton4 instance = null;

static {
instance = new Singleton4();
}

private Singleton4() {

}

public static Singleton4 getInstance() {
return instance;
}
}

/**
* 单例模式,懒汉式,使用静态内部类,线程安全【推荐】
*/
public static class Singleton5 {
private final static class SingletonHolder {
private static final Singleton5 INSTANCE = new Singleton5();
}

private Singleton5() {

}

public static Singleton5 getInstance() {
return SingletonHolder.INSTANCE;
}
}

/**
* 静态内部类,使用枚举方式,线程安全【推荐】
*/
public enum Singleton6 {
INSTANCE;

public void whateverMethod() {

}
}

/**
* 静态内部类,使用双重校验锁,线程安全【推荐】
*/
public static class Singleton7 {
private volatile static Singleton7 instance = null;

private Singleton7() {

}

public static Singleton7 getInstance() {
if (instance == null) {
synchronized (Singleton7.class) {
if (instance == null) {
instance = new Singleton7();
}
}
}

return instance;
}
}

public static void main(String[] args) {
System.out.println(Singleton.getInstance() == Singleton.getInstance());
System.out.println(Singleton2.getInstance() == Singleton2.getInstance());
System.out.println(Singleton3.getInstance() == Singleton3.getInstance());
System.out.println(Singleton4.getInstance() == Singleton4.getInstance());
System.out.println(Singleton5.getInstance() == Singleton5.getInstance());
System.out.println(Singleton6.INSTANCE == Singleton6.INSTANCE);
System.out.println(Singleton7.getInstance() == Singleton7.getInstance());
}

}
63 changes: 63 additions & 0 deletions src/Test03.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Author: 王俊超
* Date: 2015-04-21
* Time: 18:43
* Declaration: All Rights Reserved !!!
*/
public class Test03 {
/**
* 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。
* 请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
* <p/>
* 规律:首先选取数组中右上角的数字。如果该数字等于要查找的数字,查找过程结束:
* 如果该数字大于要查找的数字,剔除这个数字所在的列:如果该数字小于要查找的数字,剔除这个数字所在的行。
* 也就是说如果要查找的数字不在数组的右上角,则每-次都在数组的查找范围中剔除)行或者一列,这样每一步都可以缩小
* 查找的范围,直到找到要查找的数字,或者查找范围为空。
*
* @param matrix 待查找的数组
* @param number 要查找的数
* @return 查找结果,true找到,false没有找到
*/
public static boolean find(int[][] matrix, int number) {

// 输入条件判断
if (matrix == null || matrix.length < 1 || matrix[0].length < 1) {
return false;
}

int rows = matrix.length; // 数组的行数
int cols = matrix[1].length; // 数组行的列数

int row = 0; // 起始开始的行号
int col = cols - 1; // 起始开始的列号

// 要查找的位置确保在数组之内
while (row >= 0 && row < rows && col >= 0 && col < cols) {
if (matrix[row][col] == number) { // 如果找到了就直接退出
return true;
} else if (matrix[row][col] > number) { // 如果找到的数比要找的数大,说明要找的数在当前数的左边
col--; // 列数减一,代表向左移动
} else { // 如果找到的数比要找的数小,说明要找的数在当前数的下边
row++; // 行数加一,代表向下移动
}
}

return false;
}

public static void main(String[] args) {
int[][] matrix = {
{1, 2, 8, 9},
{2, 4, 9, 12},
{4, 7, 10, 13},
{6, 8, 11, 15}
};
System.out.println(find(matrix, 7)); // 要查找的数在数组中
System.out.println(find(matrix, 5)); // 要查找的数不在数组中
System.out.println(find(matrix, 1)); // 要查找的数是数组中最小的数字
System.out.println(find(matrix, 15)); // 要查找的数是数组中最大的数字
System.out.println(find(matrix, 0)); // 要查找的数比数组中最小的数字还小
System.out.println(find(matrix, 16)); // 要查找的数比数组中最大的数字还大
System.out.println(find(null, 16)); // 健壮性测试,输入空指针
}
}
78 changes: 78 additions & 0 deletions src/Test04.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* Author: 王俊超
* Date: 2015-04-21
* Time: 19:09
* Declaration: All Rights Reserved !!!
*/
public class Test04 {
/**
* 请实现一个函数,把字符串中的每个空格替换成"%20",例如“We are happy.“,则输出”We%20are%20happy.“。
*
* @param string 要转换的字符数组
* @param usedLength 已经字符数组中已经使用的长度
* @return 转换后使用的字符长度,-1表示处理失败
*/
public static int replaceBlank(char[] string, int usedLength) {
// 判断输入是否合法
if (string == null || string.length < usedLength) {
return -1;
}

// 统计字符数组中的空白字符数
int whiteCount = 0;
for (int i = 0; i < usedLength; i++) {
if (string[i] == ' ') {
whiteCount++;
}
}

// 计算转换后的字符长度是多少
int targetLength = whiteCount * 2 + usedLength;
int tmp = targetLength; // 保存长度结果用于返回
if (targetLength > string.length) { // 如果转换后的长度大于数组的最大长度,直接返回失败
return -1;
}

// 如果没有空白字符就不用处理
if (whiteCount == 0) {
return usedLength;
}

usedLength--; // 从后向前,第一个开始处理的字符
targetLength--; // 处理后的字符放置的位置

// 字符中有空白字符,一直处理到所有的空白字符处理完
while (usedLength >= 0 && usedLength < targetLength) {
// 如是当前字符是空白字符,进行"%20"替换
if (string[usedLength] == ' ') {
string[targetLength--] = '0';
string[targetLength--] = '2';
string[targetLength--] = '%';
} else { // 否则移动字符
string[targetLength--] = string[usedLength];
}
usedLength--;
}

return tmp;
}

public static void main(String[] args) {
char[] string = new char[50];
string[0] = ' ';
string[1] = 'e';
string[2] = ' ';
string[3] = ' ';
string[4] = 'r';
string[5] = 'e';
string[6] = ' ';
string[7] = ' ';
string[8] = 'a';
string[9] = ' ';
string[10] = 'p';
string[11] = ' ';

int length = replaceBlank(string, 12);
System.out.println(new String(string, 0, length));
}
}
Loading

0 comments on commit 82a4297

Please sign in to comment.