d202548
今天是牛客!!!
删除链表的重复元素
前两天好像在力扣刚写过,但是忘记思路了,今天又想了一种方式
用map统一下数字是否是重复的
然后第二遍,再遍历的时候保留不重复的
public ListNode deleteDuplicates (ListNode head) {
Map<Integer, Boolean> map = new HashMap<>();
ListNode cur = head;
while (cur != null) {
if (map.containsKey(cur.val)) {
map.put(cur.val, false);
} else {
map.put(cur.val, true);
}
cur = cur.next;
}
ListNode ret = new ListNode(1);
cur = ret;
ListNode curT = head;
while (curT != null) {
if (map.get(curT.val)) {
ListNode temp = new ListNode(curT.val);
cur.next = temp;
cur = cur.next;
}
curT = curT.next;
}
return ret.next;
}
找出字符串中第一个匹配项的下标
使用库函数
public int strStr(String haystack, String needle) {
return haystack.indexOf(needle);
}