一面
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;
}