排序之快排、java输入输出

排序之快排、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);

image-20210317161455441

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(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);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值