2道
19.Remove Nth Node From End of List
Given a linked list, remove the n-th node from the end of list and return its head.
Example:
Given linked list: 1->2->3->4->5, and n = 2.
After removing the second node from the end, the linked list becomes
1->2->3->5. Note:Given n will always be valid.
Follow up:
Could you do this in one pass?
题意:对于一个链表删除倒数第n个数后,输出链表
- Java 先计算长度,找到删除元素的正数位置,再进行删除操作
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode z = head;
ListNode a = new ListNode(0);
ListNode y,b = new ListNode(0);
b.next = head ;
y = b;
int m =0;
while(z != null){
z=z.next;
m++;
}
m = m -n+1;
for(int i = 0;i<m;i++){
a = y;
y = y.next;
}
a.next = y.next;
return b.next;
}
- 牛?的思路:fast点先移动到n+1位置,再和slow点一起移动,直到fast点到最后,此时slow点就是要删除的点
ListNode start = new ListNode(0);
start.next = head;
ListNode slow = start,fast = head;
for(int i =0;i<n;i++){
fast = fast.next;
}
while(fast != null){
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return start.next;
- python
def removeNthFromEnd(self, head, n):
"""
:type head: ListNode
:type n: int
:rtype: ListNode
"""
fast = head
slow = start = ListNode(0)
slow.next = head
start.next = head
for i in range(n):
fast = fast.next
while(fast):
fast = fast.next
slow = slow.next
slow.next = slow.next.next
return start.next
10. Regular Expression Matching
Given an input string (s) and a pattern§,
implement regular expression matching with support for ‘.’ and ‘’.
‘.’ Matches any single character. '’ Matches zero or more of the preceding element.
The matching should cover the entire input string(not partial).
思路:输入一个字符串s,p由正则表达式组成,. 任意字符 * 代表出现>=0
对字符串进行遍历,
MARK 改变思路专题练习
Eclpse查看源码
Ctrl+鼠标左键,这个常用语查看变量、类型及方法的定义以及查看方法的实现
Ctrl+O,这个用于查看一个类的纲要,列出其方法和成员变量,再按一次时,列出该类继承的方法及变量。
Ctrl+T,这个用于查看一个类的继承关系树,是自顶向下的结构显示,再按一次会变成自底向上,同时列出所实现的接口;当这个快捷键使用在方法上时,会列出有同名方法的父类、子类和接口。
Alt+左右方向键,阅读代码时,我们经常进入调用的方法查看后又跳转回调用的位置,Ctrl+左方向键是退回上一个阅读的位置,Ctrl+右方向键是前进到下一个阅读 的位置。
Ctrl+Alt+H,这个用于显示方法被哪些方法调用,并生成调用树。
Ctrl+L,这个用于跳转到指定的行