目錄
- 1、兩數之和
- 2、移動零
- 3、相交鏈表
- 4、有效的括號
- 5、反轉鏈表
- 6、回文鏈表
- 7、環形鏈表
- 8、環形鏈表II
- 9、合并兩個有序鏈表
- 10、二叉樹的中序遍歷
1、兩數之和
1. 兩數之和 - 力扣(LeetCode)
方法1:
class Solution {public int[] twoSum(int[] nums, int target) {int[] ret = new int[2];for(int i=0;i<nums.length;i++){for(int j=i+1;j<nums.length;j++) {if(nums[i]+nums[j]==target) {ret[0] = i;ret[1] = j;return ret;} }}return ret;}
}
方法2:
class Solution {public int[] twoSum(int[] nums, int target) {int[] ret = new int[2];HashMap<Integer,Integer> hash = new HashMap<>();// key:nums[i],value:ifor (int i = 0; i < nums.length; i++) {if (hash.containsKey(target - nums[i])) {ret[0] = i;ret[1] = hash.get(target - nums[i]);return ret;}hash.put(nums[i], i);}return ret;}
}
2、移動零
283. 移動零 - 力扣(LeetCode)
class Solution {public void moveZeroes(int[] nums) {int dest = -1;for (int cur = 0; cur < nums.length; cur++) {if (nums[cur] != 0) {dest++;int tmp = nums[cur];nums[cur] = nums[dest];nums[dest] = tmp;}}}
}
3、相交鏈表
160. 相交鏈表 - 力扣(LeetCode)
public class Solution {public ListNode getIntersectionNode(ListNode headA, ListNode headB) {ListNode cur1 = headA;ListNode cur2 = headB;int count1 = 0;//統計A結點個數int count2 = 0;//統計B結點個數while (cur1 != null) {count1++;cur1 = cur1.next;}while (cur2 != null) {count2++;cur2 = cur2.next;}if (count1 > count2) {for (int i = 0; i < count1 - count2; i++) {headA = headA.next;}}if (count1 < count2) {for (int i = 0; i < count2 - count1; i++) {headB = headB.next;}}while (headA != headB) {headA = headA.next;headB = headB.next;}return headA;}
}
4、有效的括號
20. 有效的括號 - 力扣(LeetCode)
class Solution {public boolean isValid(String s) {Stack<Character> stack = new Stack<>();for (int i = 0; i < s.length(); i++) {char ch = s.charAt(i);if (ch == '(' || ch == '[' || ch == '{') {//如果是左括號,入棧stack.push(ch);} else {//如果是右括號if (stack.empty()) {//判斷棧是否為空return false;} else {char tmp = stack.peek();if (ch == ')' && tmp == '('|| ch == ']' && tmp == '['|| ch == '}' && tmp == '{') {stack.pop();} else {return false;}}}}return stack.empty();}
}
5、反轉鏈表
206. 反轉鏈表 - 力扣(LeetCode)
方法1:
class Solution {public ListNode reverseList(ListNode head) {if(head==null || head.next==null){return head;} //1.先反轉后面的鏈表ListNode newHead = reverseList(head.next);//返回反轉后的頭head.next.next = head;head.next = null;return newHead;}
}
方法2:
class Solution {public ListNode reverseList(ListNode head) {if (head == null) {return head;}ListNode cur = head.next;head.next = null;while (cur != null) {ListNode curNext = cur.next;cur.next = head;head = cur;cur = curNext;}return head;}
}
6、回文鏈表
234. 回文鏈表 - 力扣(LeetCode)
class Solution {public boolean isPalindrome(ListNode head) {if (head == null) {return false;}ListNode fast = head;ListNode slow = head;//找到中間結點while (fast != null && fast.next != null) {fast = fast.next.next;slow = slow.next;}ListNode Newhead = reverseList(slow);ListNode tmp1 = head;ListNode tmp2 = Newhead;while (tmp2 != null) {//if (tmp2.val != tmp1.val) {return false;} else {tmp1 = tmp1.next;tmp2 = tmp2.next;}}return true;}//翻轉鏈表public ListNode reverseList(ListNode head) {if (head == null) {return head;}ListNode cur = head.next;head.next = null;while (cur != null) {ListNode curNext = cur.next;cur.next = head;head = cur;cur = curNext;}return head;}
}
7、環形鏈表
141. 環形鏈表 - 力扣(LeetCode)
public class Solution {public boolean hasCycle(ListNode head) {if (head == null) {return false;}ListNode fast = head.next;ListNode slow = head;while (fast != null && fast.next != null) {fast = fast.next.next;slow = slow.next;if (fast == slow) {return true;}}return false;}
}
8、環形鏈表II
142. 環形鏈表 II - 力扣(LeetCode)
public class Solution {public ListNode detectCycle(ListNode head) {if (head == null) {return null;}ListNode fast = head;ListNode slow = head;while (fast != null && fast.next != null) {fast = fast.next.next;slow = slow.next;if (fast == slow) {// 兩人相遇,說明鏈表成環while (head != slow) {slow = slow.next;head = head.next;}//return head;}}return null;}
}
9、合并兩個有序鏈表
21. 合并兩個有序鏈表 - 力扣(LeetCode)
方法1:
class Solution {public ListNode mergeTwoLists(ListNode list1, ListNode list2) {if (list1 == null) {return list2;}if (list2 == null) {return list1;}// 1.比大小if (list1.val < list2.val) {list1.next = mergeTwoLists(list1.next, list2);return list1;} else {list2.next = mergeTwoLists(list2.next, list1);return list2;}}
}
方法2:
class Solution {public ListNode mergeTwoLists(ListNode list1, ListNode list2) {ListNode l1 = list1;ListNode l2 = list2;if (l1 == null) {return l2;}if (l2 == null) {return l1;}ListNode newHead = null;ListNode newTail = null;while (l1 != null && l2 != null) {if (l1.val <= l2.val) {//if (newTail == null) {newHead = newTail = l1;l1 = l1.next;} else {newTail.next = l1;newTail = l1;l1 = l1.next;}} else {if (newTail == null) {newHead = newTail = l2;l2 = l2.next;} else {newTail.next = l2;newTail = l2;l2 = l2.next;}}}//if (l1 == null) {newTail.next = l2;} else {newTail.next = l1;}return newHead;}
}
10、二叉樹的中序遍歷
94. 二叉樹的中序遍歷 - 力扣(LeetCode)
class Solution {List<Integer> ret = new LinkedList<Integer>();public List<Integer> inorderTraversal(TreeNode root) { if (root == null) {return ret;}dfs(root, ret);return ret;}public void dfs(TreeNode root, List<Integer> ret) {if (root == null) {return;}dfs(root.left, ret);ret.add(root.val);dfs(root.right, ret);}
}