其他分享
首页 > 其他分享> > 携程2019

携程2019

作者:互联网

1.输入一个long类型的数值, 求该数值的二进制表示中的1的个数 .

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            long n = sc.nextLong();
            long ans = 0;
            for (long i = n; i != 0; i-=lowbit(i)) {
                ans++;
            }
            System.out.println(ans);
        }
    }
    
    public static long lowbit(long x) {
        return x&(-x);
    }
}
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            long n = sc.nextLong();
            long ans = 0;
            while (n != 0) {
                n = n&(n-1);
                ans++;
            }
            System.out.println(ans);
        }
    }
}

 

2. LRU算法

// LRU 算法
import java.util.*;
public class Main {
    static class LRU {
        int capacity;
        Map<Integer, Integer> map;
        Deque<Integer> que;
        
        public LRU(int n) {
            capacity = n;
            map = new HashMap<Integer, Integer>();
            que = new LinkedList<Integer>();
        }
        public int get(int key) {
            int val = -1;
            if (map.containsKey(key)) {
                val = map.get(key);
                que.remove(key);
                que.offerFirst(key);
            }
            return val;
        }
        
        public void put(int key, int val) {
            if (!map.containsKey(key)) {    // no 
                if (!que.isEmpty() && que.size() >= capacity) {
                    int idx = que.pollLast();
                    map.remove(idx);                        
                }
                que.offerFirst(key);
            } 
            map.put(key, val);
        }
    }
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        LRU lru = new LRU(n);
        while (sc.hasNext()) {
            String[] lines = sc.nextLine().split(" ");
            String sign = lines[0];
            int key = 0 ,val = 0;
            if ("p".equals(sign)) {
                key = Integer.parseInt(lines[1]);
                val = Integer.parseInt(lines[2]);
                lru.put(key, val);
            } else if ("g".equals(sign)) {
                key = Integer.parseInt(lines[1]);
                System.out.println(lru.get(key));
            }
        }
    }
}

3. 有一批订单记录,数据有订单号,入店时间,离店时间;
输入一个时间值A,需要在这批记录中找到符合入离店时间范围(A大于等于入店时间,并且A小于等于离店时间)内的所有记录。 单次查询时间复杂度控制在O(logN)

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        List<Integer> ans = new ArrayList<>();
        int T = Integer.parseInt(sc.nextLine());
        int A = Integer.parseInt(sc.nextLine());
        while (T-- > 0) {
            String[] line = sc.nextLine().split(" ");
            int idx = Integer.parseInt(line[0]);
            int start = Integer.parseInt(line[1]);
            int end = Integer.parseInt(line[2]);
            if (start <= A && A <= end) {
                ans.add(idx);
            }
        }
        if (ans.isEmpty()) {
            System.out.println("null");
        } else {
            Collections.sort(ans);
            for (int x: ans) {
                System.out.println(x);
            }
        }
    }
}

 

标签:携程,int,parseInt,2019,key,sc,Integer,public
来源: https://www.cnblogs.com/xiazhenbin/p/15981925.html