1、实现将数据读到指定文件内。
题目给出一部分代码,好像说是要补充什么的。
public void method(String filename;byte[] contentbuf){
FileOutputStream out=new FileOutputStream(filename);
out.write(contentbuf);
}
2、找出最大子字符串
3、122345,第三位不是4,3和5不互联。
4、实现字符串反转,使时间和空间复杂度最小。5、12个球,怎么在3次内通过天平找到不同重量的那个?13个呢?6、JVM heap内部结构及各部分功能。7、数据库连接池的工作原理。8、String、StringBuffer、StringBuilder的区别。
9、public class Test{
private static int x=5;
static{
x+=10;
}
public void main(String[] args){
System.out.println("x="+x);
}
static{
x/=3;
}
}
题目给出一部分代码,好像说是要补充什么的。
public void method(String filename;byte[] contentbuf){
FileOutputStream out=new FileOutputStream(filename);
out.write(contentbuf);
}
2、找出最大子字符串
3、122345,第三位不是4,3和5不互联。
4、实现字符串反转,使时间和空间复杂度最小。5、12个球,怎么在3次内通过天平找到不同重量的那个?13个呢?6、JVM heap内部结构及各部分功能。7、数据库连接池的工作原理。8、String、StringBuffer、StringBuilder的区别。
9、public class Test{
private static int x=5;
static{
x+=10;
}
public void main(String[] args){
System.out.println("x="+x);
}
static{
x/=3;
}
}
既然考的是main方法不是static
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s1=sc.nextLine();
String s2=sc.nextLine();
String subString = getSubString(s1, s2);
System.out.println("两个串中的最大子串是:"+subString);
}
private static String getSubString(String s1, String s2) {
String maxString, minString;
maxString = s1.length() > s2.length() ? s1 : s2;
minString = (maxString.equals(s1)) ? s2 : s1;
for (int x = 0; x < minString.length(); x++) {
for (int start = 0, end = minString.length() - x; end <= minString
.length(); start++, end++) {
String temp = minString.substring(start, end);
if (maxString.contains(temp)) {
return temp;
}
}
}
return null;
}
}这种方法性能可能不是特别好
public class StringRevese { /**
* @param args
*/
public static String reverse(String s){
char[] arr=s.toCharArray();
int length=arr.length;
for(int i=0;i<length/2;i++){
arr[i]=(char) (arr[i]+arr[length-i-1]);
arr[length-i-1]=(char) (arr[i]-arr[length-i-1]);
arr[i]=(char) (arr[i]-arr[length-i-1]);
}
s=String.valueOf(arr);
return s;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String str="abcdefghigk";
str=reverse(str);
System.out.println(str);
}}
这种写法知识运用了在交换两个数组元素位置时,没有引入第三方变量,节省了空间,不知道这样性能算怎样?
import java.util.TreeSet;public class MyTest { public static Set<String> set = new TreeSet<String>(); public static void perm(char[] n, int beg, int end) {
if (beg == end) {
addNumber(String.valueOf(n));
} else {
for (int i = beg; i <= end; ++i) {
swap(n, beg, i);
perm(n, beg + 1, end);
swap(n, beg, i);
}
}
} public static void swap(char[] n, int x, int y) {
if (x == y || n[x] == n[y]) {
return;
}
char temp = n[x];
n[x] = n[y];
n[y] = temp;
} public static void addNumber(String str) {
if (str.charAt(2) == '4' || str.contains("35") || str.contains("53")) {
return;
}
set.add(str);
} public static void main(String args[]) {
char[] number = new char[] { '1', '2', '2', '3', '4', '5' };
perm(number, 0, number.length - 1);
System.out.println(set.size());
int cols = 10;
for (String s : set) {
System.out.print(s + " ");
if (cols-- == 1) {
System.out.println();
cols = 10;
}
}
}
static int[] bits = new int[] { 1, 2, 3, 4, 5 };
/**
* @param args
*/
public static void main(String[] args) {
sort("", bits);
}
private static void sort(String prefix, int[] a) {
if (a.length == 1) {
System.out.println(prefix + a[0]);
}
for (int i = 0; i < a.length; i++) {
sort(prefix + a[i], copy(a, i));
}
}
private static int[] copy(int[] a,int index){
int[] b = new int[a.length-1];
System.arraycopy(a, 0, b, 0, index);
System.arraycopy(a, index+1, b, index, a.length-index-1);
return b;
}
}
2. 对第一题增加难度,用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连。 基本思路:
2-1. 把问题归结为图结构的遍历问题。图遍历思想:实际上6个数字就是六个结点,把六个结点连接成无向连通图,对于每一个结点求这个图形的遍历路径,所有结点的遍历路径就是最后对这6个数字的排列组合结果集。 2-2. 显然这个结果集还未达到题目的要求。从以下几个方面考虑: 2-2-1. 3,5不能相连:实际要求这个连通图的结点3,5之间不能连通, 可在构造图结构时就满足改条件,然后再遍历图。
2-2-2. 不能有重复: 考虑到有两个2,明显会存在重复结果,可以把结果集放在TreeSet中过滤重复结果
2-2-3. 4不能在第三位: 仍旧在结果集中去除满足此条件的结果。 采用二维数组定义图结构,最后的代码是:
import java.util.Iterator;
import java.util.TreeSet;
public class TestQuestion {
private String[] b = new String[]{"1", "2", "2", "3", "4", "5"};
private int n = b.length;
private boolean[] visited = new boolean[n];
private int[][] a = new int[n][n];
private String result = "";
private TreeSet set = new TreeSet();
public static void main(String[] args) {
new TestQuestion().start();
}
private void start() {
// Initial the map a[][]
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
a[i][j] = 0;
} else {
a[i][j] = 1;
}
}
}
// 3 and 5 can not be the neighbor.
a[3][5] = 0;
a[5][3] = 0;
// Begin to depth search.
for (int i = 0; i < n; i++) {
this.depthFirstSearch(i);
}
// Print result treeset.
Iterator it = set.iterator();
while (it.hasNext()) {
String string = (String) it.next();
// "4" can not be the third position.
if (string.indexOf("4") != 2) {
System.out.println(string);
}
}
}
private void depthFirstSearch(int startIndex) {
visited[startIndex] = true;
result = result + b[startIndex];
if (result.length() == n) {
// Filt the duplicate value.
set.add(result);
}
for(int j = 0; j < n; j++) {
if (a[startIndex][j] == 1 && visited[j] == false) {
depthFirstSearch(j);
} else {
continue;
}
}
// restore the result value and visited value after listing a node.
result = result.substring(0, result.length() -1);
visited[startIndex] = false;
}
}
public class Compare {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in) ;
System.out.println("please input");
String a=scan.nextLine();
String b=scan.nextLine();
a=a.toLowerCase();
b=b.toLowerCase();
if(a.length()!=b.length()){System.out.println("字符串不相等"); return;}
int[] arr1 =new int[26];
int[] arr2 =new int[26];
for(int i=0;i<a.length();i++){
arr1[a.charAt(i)-'a']++;
arr2[b.charAt(i)-'a']++;
}
for(int i=0;i<arr1.length;i++){
if(arr1[i]!=arr2[i]){System.out.println("字符串不相等"); return;}
}
System.out.println("字符串相等");
}
}
package paper;
//1,2,2,3,4,5,第三位不能是4,35不想连。import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;/**
* 1,2,2,3,4,5 排列
* 不能有重复,比如 122345和122345是相同的
* 4不能再第三位,比如:124325
* 3和5不能挨着,比如:142235和142253
* @author wdai
*
*/
public class ArrangeDemo {
public static void main(String[] args){
Integer[] arrSource={1,2,2,3,4,5};
combination(Arrays.asList(arrSource),new ArrayList<Integer>());
}
/**
* 递归方法
* @param listSource
* @param listLeft
*/
public static void combination(List<Integer> listSource,List<Integer> listLeft){
int index=listLeft.size()-1;
if(index==2&&listLeft.get(index)==4)
return;
if(index>0){
if(listLeft.get(index)==3&&listLeft.get(index-1)==5)
return;
if(listLeft.get(index)==5&&listLeft.get(index-1)==5)
return;
}
if(listSource.size()==0){
for(int n:listLeft)
System.out.print(n);
System.out.println();
return;
}
for(int i=0;i<listSource.size();i++){
int currentNum=listSource.get(i);
List<Integer> newListSource=new ArrayList<Integer>(listSource);
List<Integer> newListLeft=new ArrayList<Integer>(listLeft);
newListLeft.add(currentNum);
newListSource.remove(i);
if(i<listSource.size()-1&&listSource.get(i+1)==currentNum)
i++;
combination(newListSource,newListLeft);
}
}
}