算法

两个超大正整数相加

	String a = "20394803294";
        String b = "20394803294";
        StringBuffer buffer = new StringBuffer();
        int bound = 0;
        int i = a.length()-1;
        int j = b.length()-1;
        
        while(i >=0 || j >= 0){
            int sum =i < a.length() ? Integer.valueOf(a[i]): 0 + j<b.length()? Integer.valueOf(b[j]):0 + bound;
            buffer.append(sum%10);
            bound = sum/10;
            i--;
            j--;
        }
        if(bound > 0){
            buffer.append(bound);
        }
        System.out.println(buffer.reverse().toString());

设计一个多线程下载模块,支持断点续下载

    public void main() {
        String url = "";
        String savePath = "";
        long total = getTotalLength(url);
        int size = 3;
        long interval = total / size;
        for (int i = 0; i < size; i++) {
            long start = interval * i + (i == 0 ? 0 : 1);
            long end = Math.min(interval * (i + 1), total);
            ChunkTask task = new ChunkTask(String.valueOf(i), url, savePath, total, start, end, new DownloadCallback() {
                @Override
                public void onSuccess(ChunkTask task) {
                    if (allChuckTaskSuccess()) {
                        renameFile();
                    }
                }
            });
            AsyncTask.execute(task);
        }
    }


    public class ChunkTask implements Runnable {
        public String id;
        public String url;
        public String savePath;
        public long total;
        public long start;
        public long end;
        public int status;
        public DownloadCallback callback;

        public ChunkTask(String id, String url, String savePath, long total, long start, long end, DownloadCallback callback) {
            this.id = id;
            this.url = url;
            this.savePath = savePath;
            this.total = total;
            this.start = start;
            this.end = end;
            this.callback = callback;
        }

        @Override
        public void run() {
            if (FileUtils.IsFileExist(savePath) && FileUtils.getFileLen(savePath) > 1024) {
                return;
            }
            InputStream is = null;
            RandomAccessFile raf = null;
            BufferedReader markIs = null;
            RandomAccessFile markOs = null;
            HttpURLConnection connection = null;
            try {
                String tempPath = savePath + ".temp";
                String markPath = savePath + id + ".mark";
                // 读取记录的当前进度
                markIs = new BufferedReader(new FileReader(markPath));
                long current = StringUtils.getIntValue(markIs.readLine(), start);
                // 创建下载
                URL netUrl = new URL(url);
                connection = (HttpURLConnection) netUrl.openConnection();
                // 设置断点下载
                connection.addRequestProperty("Range", "bytes=" + (current + start) + "-" + (end == 0 ? "" : end));
                connection.connect();
                raf = new RandomAccessFile(tempPath, "rwd");
                markOs = new RandomAccessFile(markPath, "rwd");
                raf.setLength(total);
                raf.seek(current);
                is = connection.getInputStream();
                int count = 0;
                byte[] buffer = new byte[1024];
                while ((count = is.read(buffer)) != -1) {
                    current += count;
                    raf.write(buffer, 0, count);
                    // 为了断点下载,记录当前线程的进度
                    markOs.seek(0);
                    markOs.write(String.valueOf(count).getBytes());
                }
                callback.onSuccess(this);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                closeIO();
            }
        }
    }

    interface DownloadCallback {
        void onSuccess(ChunkTask task);
    }

0到100打印输出,要求两个线程交替顺序打印

    static class SoulutionTask implements Runnable{
        static int value = 0;
        @Override
        public void run() {
            while (value <= 100){
                synchronized (SoulutionTask.class){
                    System.out.println(Thread.currentThread().getName() + ":" + value++);
                    SoulutionTask.class.notify();
                    try {
                        SoulutionTask.class.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

快速排序:

    public void qsort(int[] list, int left, int right) {
        if (left < right) {
            int pivot = left;
            int i = left;
            int j = right;
            while (i < j) {
                while (i < j && list[j] > list[pivot]) {
                    j--;
                }
                while (i < j && list[i] < list[pivot]) {
                    i++;
                }
                if (i < j) {
                    swap(list, i, j);
                }
            }
            swap(list, pivot, i);
            qsort(list, left, i - 1);
            qsort(list, j + 1, right);
        }
    }

冒泡排序:

    public void sort(int[] list) {
        for (int i = 0; i < list.length - 1; i++) {
            for (int j = 0; j < list.length - 1 - i; j++) {
                if (list[j] > list[j + 1]) {
                    int temp = list[j];
                    list[j] = list[j + 1];
                    list[j + 1] = temp;
                }
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值