排序之快排、java输入输出
public void mysort(int[] arr,int low, int high)
{
if(low >= high) return;
int tmp = low;
int key = arr[low];
int i = low,j = high;
while(i < j)
{
while(arr[j] >= key && i < j) j--;
while(arr[i] <= key && i < j) i++;
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
arr[low] = arr[j];
arr[j] = key;
mysort(arr,low,j-1);
mysort(arr,j+1,high);
}
//java的切片
java.util.Arrays.copyOf(arr,k);
输入输出
//输入
Scanner reader = new Scanner(System.in);
while(reader.hasNext())
System.out.println(reader.nextInt());
//输出串值,表达式的值,允许用+将串和数值一起输出
System.out.println();//输出后换行
System.out.print("");//输出后不换行
System.out.printf("%d+%c+%f+%s+%md+%m.nf",i,c,f,s,mcol,f);
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8xah2kEk-1616034196999)(https://raw.githubusercontent.com/Cassie317/cloudimg/main/img/image-20210317161534890.png)]
文件输入和输出
//按字节流读写
try{
// 创建指向文件的输入流
FileInputStream fis = new FileInputStream("aaa.txt");
}catch (IOException e)
{
System.out.println(e);
}
// 指定输入流的源
File f = new File("aaa.txt");
int n = -1;
byte[] a = new byte[100];
try {
FileInputStream in = new FileInputStream(f);
while((n = in.read(a,0,100))!= -1)
{
String s = new String(a,0,n);
System.out.println(s);
}
in.close();
}catch (IOException e)
{
System.out.println(e);
}
try {
// 第二个参数表示是否是append
FileOutputStream fos = new FileOutputStream(f,true);
fos.write("aaa".getBytes());
fos.write(123);
fos.write("aaa".getBytes(),0,"aaa".getBytes().length);
fos.close();
}catch (IOException e)
{
System.out.println(e);
}
//按字符读取文件流
char[] c = new char[20];
try{
Writer out = new FileWriter("targetfile",true);
Reader in = new FileReader("sourcefile");
int n = -1;
while((n = in.read(c))!=-1)
out.write(c,0,n);
out.flush();//冲洗缓冲区
out.close();
}catch (IOException e)
{
System.out.println(e);
}
public boolean isSymmetric(TreeNode root) {
return root == null ? true : recur(root.left, root.right);
}
boolean recur(TreeNode L, TreeNode R) {
if(L == null && R == null) return true;
if(L == null || R == null || L.val != R.val) return false;
return recur(L.left, R.right) && recur(L.right, R.left);
}