Day1
- 有效的括号:给定一个只包括 '(',')','{','}','[',']' 的括号,观察括号是否闭合。如'([])'返回 true
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for(Character c : s.toCharArray()){
if('(' == c || '[' == c || '{' == c)
stack.push(c);
//假如栈内元素为空,避免空指针异常,不取出元素,直接返回 false
else if(!stack.isEmpty()){
Character t = stack.pop();
if(!isContain(c, t))
return false;
}
else
return false;
}
if(!stack.isEmpty())
return false;
return true;
}
public boolean isContain(Character c,Character s){
if('(' == s && ')' == c)
return true;
else if('[' == s && ']' == c)
return true;
else if('{' == s && '}' == c)
return true;
return false;
}
}
- 对称二叉树:给定一个二叉树,检查它是否是镜像对称的。
![image-20001]()
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
// 输入根节点,首先检查是否次树根结点是否为空,再检查树的左右子树,可用队列修改
public boolean isSymmetric(TreeNode root) {
if(root == null || (root.left == null && root.right == null))
return true;
return check(root.left, root.right);
}
public boolean check(TreeNode left, TreeNode right){
if(left == null && right == null)
return true;
else if(left == null || right == null)
return false;
return left.val == right.val && check(left.left, right.right) &&
check(left.right, right.left);
}
}
- 只出现一次的数字,数组中除一个元素出现一次外,其他元素均出现多次,查找出现一次的元素为
- 使用异或的思路,两个相同的数字异或后为 0,0 和一个数进行异或即得到它本身。
class Solution {
public int singleNumber(int[] nums) {
int a = 0;
// 异或运算
// 当有两个相同元素,进行异或值为 0
// 当仅有一个元素,进行异或值为那个元素
for(int i : nums)
a = a ^ i;
return a;
}
}
- 反转链表。
- 有两种思路,一种假如有头节点使用头插法,进行反转,另一种则是使用三个指针记录位置进行反转,力扣的题目适合后一种做法。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pre = null;
ListNode tmp = null;
ListNode cur = head;
while(cur != null){
//这里指针的顺序不能换
tmp = cur.next;
cur.next = pre;
pre = cur;
cur = tmp;
}
return pre;
}
}
- 回文链表
解题思路较多:
- 在遍历链表的时候,开辟一块数组空间记录链表元素的值,在双指针遍历检测是否是回文链表。
- 采用递归的方式,一个公共元素作为头指针,另一尾指针使用递归的方式检测,效率较低。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
ListNode tmp;
public boolean isPalindrome(ListNode head) {
tmp = head;
return check(head);
}
public boolean check(ListNode head){
if(head == null)
return true;
boolean result = check(head.next) && (head.val == tmp.val);
tmp = tmp.next;
return result;
}
}
- 采用快慢双指针的方法,快指针每次跳两格,指向尾元素的时候,慢指针恰好指向最后中间元素,分成左右两部分,将右部分进行逆序排列。
class Solution {
public boolean isPalindrome(ListNode head) {
if(head == null)
return true;
ListNode slow = head;
ListNode fast = head;
while(fast.next != null && fast.next.next != null){
slow = slow.next;
fast = fast.next.next;
}
ListNode front = head;
ListNode back = reverse(slow.next);
while(back != null){
if(front.val != back.val)
return false;
front = front.next;
back = back.next;
}
return true;
}
public ListNode reverse(ListNode head){
ListNode cur = head;
ListNode tmp = null;
ListNode pre = null;
while(cur != null){
tmp = cur.next;
cur.next = pre;
pre = cur;
cur = tmp;
}
return pre;
}
}