双指针
public int removeElement(int[] nums, int val) {
//双指针思想
//定义一个快指针:用来查找不是目标元素的索引
//定义一个慢指针:用来将不是目标元素的数据放入一个数组中,知道快指针遍历完目标数组
int i = 0; //定义快指针
int slow = 0; //定义慢指针
for (i = 0; i < nums.length; i++) {
if (nums[i] != val) {
nums[slow] = nums[i];
slow++;
}
}
//System.out.println(Arrays.toString(arr));
//System.out.println(Arrays.toString(nums));
//此时这个j就相当于这个新数组的大小,也就是删除目标元素后数组的大小
return slow;
}
public void reverseString(char[] s) {
//双指针
int j = s.length - 1;
for (int i = 0; i < s.length / 2; i++) {
char temp = s[i];
s[i] = s[j];
s[j] = temp;
j--;
}
}
第三题:151. 反转字符串中的单词 - 力扣(LeetCode)
public String reverseWords(String s) {
// 1.去除首尾以及中间多余空格
StringBuilder sb = removeSpace(s);
//System.out.println(sb);
// 2.反转单词.以空格为界限,进行反转
String str = sb.toString();
String[] split = str.split(" ");
//双指针进行反转单词
int j = split.length - 1;
for (int i = 0; i < split.length / 2; i++, j--) {
String temp = split[i];
split[i] = split[j];
split[j] = temp;
}
//System.out.println(Arrays.toString(split));
StringBuilder s1 = new StringBuilder();
for (int i = 0; i < split.length; i++) {
if (i == split.length - 1){
s1.append(split[i]);
}else {
s1.append(split[i]).append(" ");
}
}
//将StringBuilder转为string
String result = s1.toString();
return result;
}
private static StringBuilder removeSpace(String s) {
int start = 0;
int end = s.length() - 1;
while (s.charAt(start) == ' '){
start++;
}
while (s.charAt(end) == ' '){
end--;
}
StringBuilder sb = new StringBuilder();
while (start <= end){
char c = s.charAt(start);
if (c != ' ' || sb.charAt(sb.length() - 1) != ' '){
sb.append(c);
}
start++;
}
return sb;
}
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode cur = head;
ListNode temp = null;
while (cur != null) {
temp = cur.next;// 保存下一个节点
cur.next = prev;
prev = cur;
cur = temp;
}
return prev;
}
第五题:19. 删除链表的倒数第 N 个结点 - 力扣(LeetCode)
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummyNode = new ListNode(0);
dummyNode.next = head;
ListNode fastIndex = dummyNode;
ListNode slowIndex = dummyNode;
// 只要快慢指针相差 n 个结点即可
for (int i = 0; i < n + 1; i++){
fastIndex = fastIndex.next;
}
//同时移动,直到fast指向末尾
while (fastIndex != null){
fastIndex = fastIndex.next;
slowIndex = slowIndex.next;
}
//此时 slowIndex 的位置就是待删除元素的前一个位置。
slowIndex.next = slowIndex.next.next;
return dummyNode.next;
}
第六题:面试题 02.07. 链表相交 - 力扣(LeetCode)
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode curA = headA;
ListNode curB = headB;
int lenA = 0, lenB = 0;
while (curA != null) { // 求链表A的长度
lenA++;
curA = curA.next;
}
while (curB != null) { // 求链表B的长度
lenB++;
curB = curB.next;
}
curA = headA;
curB = headB;
// 让curA为最长链表的头,lenA为其长度
if (lenB > lenA) {
// 1. swap (lenA, lenB);
int tmpLen = lenA;
lenA = lenB;
lenB = tmpLen;
// 2. swap (curA, curB);
ListNode tmpNode = curA;
curA = curB;
curB = tmpNode;
}
// 求长度差
int gap = lenA - lenB;
// 让curA和curB在同一起点上(末尾位置对齐)
while (gap-- > 0) {
curA = curA.next;
}
// 遍历curA 和 curB,遇到相同则直接返回
while (curA != null) {
if (curA == curB) {
return curA;
}
curA = curA.next;
curB = curB.next;
}
return null;
}
第七题:142. 环形链表 II - 力扣(LeetCode)
public ListNode detectCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {// 有环
ListNode index1 = fast;
ListNode index2 = head;
// 两个指针,从头结点和相遇结点,各走一步,直到相遇,相遇点即为环入口
while (index1 != index2) {
index1 = index1.next;
index2 = index2.next;
}
return index1;
}
}
return null;
}
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
// 先对数组进行排序
Arrays.sort(nums);
// 找出a + b + c = 0
// a = nums[i], b = nums[left], c = nums[right]
for (int i = 0; i < nums.length; i++) {
// 排序之后如果第一个元素已经大于零,那么无论如何组合都不可能凑成三元组,直接返回结果就可以了
if (nums[i] > 0) {
return result;
}
if (i > 0 && nums[i] == nums[i - 1]) { // 去重a
continue;
}
int left = i + 1;
int right = nums.length - 1;
// 不能相等,如果相等这就只剩下两个数字了,不满足三元组
while (right > left) {
int sum = nums[i] + nums[left] + nums[right];
if (sum > 0) { // 和有点大,故将right往左移动
right--;
}
if (sum < 0) { // 和有点小,故将left往右移动
left++;
}
if (sum == 0) { // 满足要求
result.add(Arrays.asList(nums[i], nums[left], nums[right]));
// 去重逻辑应该放在找到一个三元组之后,对 b 和 c 去重
while (right > left && nums[right] == nums[right - 1])
right--;
while (right > left && nums[left] == nums[left + 1])
left++;
right--;
left++;
}
}
}
return result;
}
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> result = new ArrayList<>();
Arrays.sort(nums);
for (int i = 0; i < nums.length; i++) {
// nums[i] > target 直接返回, 剪枝操作
if (nums[i] > 0 && nums[i] > target) {
return result;
}
if (i > 0 && nums[i - 1] == nums[i]) { // 对nums[i]去重
continue;
}
for (int j = i + 1; j < nums.length; j++) {
if (j > i + 1 && nums[j - 1] == nums[j]) { // 对nums[j]去重
continue;
}
int left = j + 1;
int right = nums.length - 1;
while (right > left) {
// nums[k] + nums[i] + nums[left] + nums[right] > target int会溢出
long sum = (long) nums[i] + nums[j] + nums[left] + nums[right];
if (sum > target) { // 和有点大,故将right往左移动
right--;
}
if (sum < target) { // 和有点小,故将left往右移动
left++;
}
if (sum == target) { // 满足要求
result.add(Arrays.asList(nums[i], nums[j], nums[left], nums[right]));
// 对nums[left]和nums[right]去重
while (right > left && nums[right] == nums[right - 1])
right--;
while (right > left && nums[left] == nums[left + 1])
left++;
left++;
right--;
}
}
}
}
return result;
}