ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

携程2019

2022-03-08 20:31:21  阅读:168  来源: 互联网

标签:携程 int parseInt 2019 key sc Integer public


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有