1. 移除链表元素
题目描述:
删除链表中等于给定值 val 的所有节点。
代码:
package leetcode.week7;
import leetcode.week5.ListNode;
/**
* @author chengzhengda
* @version 1.0
* @date 2020-01-21 10:45
* @desc 移除链表元素
*/
public class t203 {
public static ListNode removeElements(ListNode head, int val) {
ListNode temp = new ListNode(0);
temp.next = head;
ListNode res = temp;
while (temp != null) {
while (temp.next != null && val == temp.next.val) {
temp.next = temp.next.next;
}
temp = temp.next;
}
return res.next;
}
public static void main(String[] args) {
ListNode listNode1 = new ListNode(1);
ListNode listNode2 = new ListNode(2);
ListNode listNode3 = new ListNode(6);
ListNode listNode4 = new ListNode(3);
ListNode listNode5 = new ListNode(4);
ListNode listNode6 = new ListNode(5);
ListNode listNode7 = new ListNode(6);
listNode1.next = listNode2;
listNode2.next = listNode3;
listNode3.next = listNode4;
listNode4.next = listNode5;
listNode5.next = listNode6;
listNode6.next = listNode7;
ListNode res = removeElements(listNode1, 6);
while (res != null) {
System.out.println(res.val);
res = res.next;
}
}
}
2. 回文链表
题目描述:
请判断一个链表是否为回文链表。
代码:
package leetcode.week7;
import leetcode.week5.ListNode;
/**
* @author chengzhengda
* @version 1.0
* @date 2020-01-21 14:54
* @desc 回文链表
*/
public class t234 {
public static boolean isPalindrome(ListNode head) {
ListNode temp = new ListNode(-1);
temp.next = head;
// 快慢指针找出链表中间点
ListNode fast = temp;
ListNode slow = temp;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
}
// 分离出链表的前后半部
fast = slow.next;
slow.next = null;
slow = temp.next;
// 反转后半段链表
ListNode pre = null;
while (fast != null) {
ListNode nextTemp = fast.next;
fast.next = pre;
pre = fast;
fast = nextTemp;
}
// 逐一比较前后半段链表
while (pre != null) {
if (pre.val != slow.val) {
return false;
}
pre = pre.next;
slow = slow.next;
}
return true;
}
public static void main(String[] args) {
ListNode listNode1 = new ListNode(1);
ListNode listNode2 = new ListNode(2);
ListNode listNode3 = new ListNode(6);
ListNode listNode4 = new ListNode(3);
ListNode listNode5 = new ListNode(6);
ListNode listNode6 = new ListNode(2);
ListNode listNode7 = new ListNode(1);
listNode1.next = listNode2;
listNode2.next = listNode3;
listNode3.next = listNode4;
listNode4.next = listNode5;
listNode5.next = listNode6;
listNode6.next = listNode7;
System.out.println(isPalindrome(listNode1));
}
}
3. 数组中重复的数据
题目描述:
给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次。
找到所有出现两次的元素。
代码:
package leetcode.week7;
import java.util.ArrayList;
import java.util.List;
/**
* @author chengzhengda
* @version 1.0
* @date 2020-02-22 17:37
* @desc 数组中重复的元素
*/
public class t442 {
public static List<Integer> findDuplicates(int[] nums) {
int i = 0;
while (i < nums.length) {
while (i < nums.length && nums[i] != i + 1) {
if (nums[i] == nums[nums[i] - 1]) {
++i;
continue;
}
int temp = nums[i];
nums[i] = nums[temp - 1];
nums[temp - 1] = temp;
}
++i;
}
List<Integer> result = new ArrayList<>();
for (
int j = 0;
j < nums.length; j++) {
if (nums[j] != j + 1) {
result.add(nums[j]);
}
}
return result;
}
public static void main(String[] args) {
int[] nums = {4, 3, 2, 7, 8, 2, 3, 1};
List<Integer> res = findDuplicates(nums);
for (Integer integer : res) {
System.out.println(integer);
}
}
}
4. 删除字符串中所有相邻重复项
题目描述:
给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。
在 S 上反复执行重复项删除操作,直到无法继续删除。
在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。
代码:
package leetcode.week7;
import java.util.Stack;
/**
* @author chengzhengda
* @version 1.0
* @date 2020-02-22 19:51
* @desc
*/
public class t1047 {
public static String removeDuplicates(String S) {
Stack<Character> stack = new Stack<>();
stack.push(S.charAt(0));
for (int i = 1; i < S.length(); i++) {
if (!stack.isEmpty() && S.charAt(i) == stack.peek()) {
stack.pop();
continue;
}
stack.push(S.charAt(i));
}
StringBuilder stringBuilder = new StringBuilder();
while (!stack.isEmpty()) {
stringBuilder.append(stack.pop());
}
return stringBuilder.reverse().toString();
}
public static void main(String[] args) {
System.out.println(removeDuplicates("abbaca"));
}
}
5. 猜数字
题目描述:
小A 和 小B 在玩猜数字。小B 每次从 1, 2, 3 中随机选择一个,小A 每次也从 1, 2, 3 中选择一个猜。他们一共进行三次这个游戏,请返回 小A 猜对了几次?
输入的guess数组为 小A 每次的猜测,answer数组为 小B 每次的选择。guess和answer的长度都等于3。
代码:
package leetcode.week7;
/**
* @author chengzhengda
* @version 1.0
* @date 2020-02-22 19:48
* @desc
*/
public class t1355 {
public int game(int[] guess, int[] answer) {
int num = 0;
for (int i = 0; i < guess.length; i++) {
if (guess[i] == answer[i]) {
num++;
}
}
return num;
}
public static void main(String[] args) {
}
}