- 第一个只出现一次的字符位置
这题也简单,使用整数数组的值是否等于1判断是否有重复值。
有个有意思的地方是初始化数组英国选取多少?这个时候就了解基本数据类型占有大小
Byte 1 个字节
short 2个字节
int 4个字节
long 8个字节
float 4个字节
double 8个字节
Boolean 1个字节 (前7位是0)
char 2个字节 采用unicode编码,它的前128字节编码与ASCII兼容
所以初始化时候用的是int[] cnt = new int[256]
public static int findonce(String str){
if(str==null) return -1;
int[] cnt = new int[256];
Arrays.fill(cnt, 0);
for(int i=0;i<str.length();i++) {
cnt[str.charAt(i)]++;
}
for (int i = 0; i < str.length(); i++)
if (cnt[str.charAt(i)] == 1){
System.out.println(i);
return i;
}
return -1;
}
-
数组中的逆序对
留白 -
两个链表的第一个公共结点
看题目理解错意思;
当访问 A 链表的指针访问到链表尾部时,令它从链表 B 的头部重新开始访问链表 B;同样地,当访问 B 链表的指针访问到链表尾部时,令它从链表 A 的头部重新开始访问链表 A。这样就能控制访问 A 和 B 两个链表的指针能同时访问到交点。
public static ListNode fristnode(ListNode anode,ListNode bnode){
if(anode==null || bnode ==null) return null;
ListNode h1= anode;
ListNode h2=bnode;
while(h1!=h2){
if(h1==null){
h1=bnode;
}else {
h1=h1.next;
}
if(h2==null){
h2=anode;
}else {
h2=h2.next;
}
}
return h1;
}
53 数字在排序数组中出现的次数
二分查找数字的start end,然后相减得到结果
网友评论