字节跳动面试

一面

1.将ip地址转成32位数   ip地址可能会有空格      192.168  .0    .236    转32位数。

思路:可以先去掉空格,然后用小数点分隔,分别转换为二进制再连接起来。

public static String ipToLong(String strIP) {
        long[] ip = strIP.replace(" ","").split("\\.")
        // 将每个.之间的字符串转换成整型
        ip[0] = Long.parseLong(strIP.substring(0, position1));
        ip[1] = Long.parseLong(strIP.substring(position1 + 1, position2));
        ip[2] = Long.parseLong(strIP.substring(position2 + 1, position3));
        ip[3] = Long.parseLong(strIP.substring(position3 + 1));
        return Long.toBinaryString((ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3]);
    }

2.给定两个排序后的数组 A 和 B,其中 A 的末端有足够的缓冲空间容纳 B。 编写一个方法,将 B 合并入 A 并排序。

初始化 A 和 B 的元素数量分别为 m 和 n。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/sorted-merge-lcci
思路:类似于归并排序,倒着进行,从A的(m+n-1)处开始。比较A[m]和B[n]的大小。

public void merge(int[] A, int m, int[] B, int n) {
        int p1=m-1,p2=n-1,index=m+n-1;
        while(p1>=0&&p2>=0){
            if(A[p1]<B[p2])
                A[index--]=B[p2--];
            else
                A[index--]=A[p1--];
        }
        while(p2>=0)
            A[index--]=B[p2--];
        while(p1>=0)
            A[index--]=A[p1--];
    }

3.链表 每K个元素逆序    leetcode25

public ListNode reverseKGroup(ListNode head, int k) {
       ListNode cur = head;
       int count = 0;
       while(cur!=null&&count!=k){
           count++;
           cur=cur.next;
       }
       if(count==k){
           cur=reverseKGroup(cur,k);
           while(count--!=0){
               ListNode next = head.next;
               head.next=cur;
               cur=head;
               head=next;
           }
           head=cur;
       }
       return head;
    }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值