zoukankan      html  css  js  c++  java
  • 单向链表反转,二分查找法

        
        public static void main(String[] args){
            Node node = new Node();
            node.value = 1;
            
            Node node2 = new Node();
            node2.value = 2;
            
            Node node3 = new Node();
            node3.value = 3;
            
            
            Node node4 = new Node();
            node4.value = 4;
            
            node.next=node2;
            node2.next=node3;
            node3.next = node4;
            
            
            Node previous = null;                // 前一个
            Node next = node;                    // 后一个
            
            //单向链表反转
            while((node = next) != null){
                next = node.next;
                node.next = previous;    //  下一个指向自己
                previous = node;
            }
            System.out.println(previous);
            
            int[] is ={1,2,3,4,5,6,7,8,9,10};
            
            //二分查找法
            int low = 0;
            int hight =  9;
            int key = 10;
            while(low <= hight){
                int mid = (low + hight) / 2;
                System.out.println(low + ":" + hight);
                if(is[mid] > key){
                    hight = mid - 1;
                }else if(is[mid] < key){
                    low = mid + 1;
                }else{
                    System.out.println(mid);
                    return;
                }
            }
            
        }
        
        static class Node{
            int value;
            Node next;
            
            public String toString(){
                String value = this.value+",";
                Node node = this;
                while((node = node.next) != null){
                    value += node.value+",";
                }
                return value;
            }
        }
  • 相关阅读:
    ubuntu中KDE与GNOME安装切换
    前向算法的数学意义上的实现
    题目1023:EXCEL排序
    题目1022:游船出租
    php notice提示
    Php显示中文时乱码
    题目1021:统计字符
    题目1020:最小长方形
    题目1013:开门人和关门人
    题目1032:ZOJ
  • 原文地址:https://www.cnblogs.com/JimmyXie/p/3707636.html
Copyright © 2011-2022 走看看