二叉树
NC45 实现二叉树先序,中序和后序遍历
public class Solution {
/**
*
* @param root TreeNode类 the root of binary tree
* @return int整型二维数组
*/
public int[][] threeOrders (TreeNode root) {
//三个集合,分别存储三种遍历结果
List<Integer> list1 = new ArrayList<>();
List<Integer> list2 = new ArrayList<>();
List<Integer> list3 = new ArrayList<>();
//调用函数计算遍历结果
preOrder(root, list1);
inOrder(root, list2);
postOrder(root, list3);
//存放结果集
int[][] res = new int[3][list1.size()];
for(int i = 0; i < list1.size(); i++){
res[0][i] = list1.get(i);
res[1][i] = list2.get(i);
res[2][i] = list3.get(i);
}
//答案返回
return res;
}
// 先序遍历函数
public void preOrder(TreeNode root, List<Integer> list){
//特判
if(root == null){
return;
}
//对左右子树进行递归的遍历
list.add(root.val);
preOrder(root.left, list);
preOrder(root.right, list);
}
// 中序遍历函数
public void inOrder(TreeNode root, List<Integer> list){
//特判
if(root == null){
return;
}
//递归调用:对左右子树进行递归的遍历
inOrder(root.left, list);
list.add(root.val);
inOrder(root.right, list);
}
// 后序遍历函数
public void postOrder(TreeNode root, List<Integer> list){
if(root == null){
return;
}
//递归调用:对左右子树进行递归的遍历
postOrder(root.left, list);
postOrder(root.right, list);
list.add(root.val);
}
}
链表
206. 反转链表
class Solution {
public ListNode reverseList(ListNode head) {
if(head==null||head.next==null){
return head;
}
ListNode last = reverseList(head.next);
head.next.next=head;
head.next=null;
return last;
}
}
92. 反转链表 II
class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
// 定义一个dummyHead, 方便处理
ListNode zero = new ListNode(0);
zero.next = head;
// 初始化指针
ListNode g = zero;
ListNode p = zero.next;
// 将指针移到相应的位置
for (int i = 0; i < m - 1; i++) {
g = g.next;
p = p.next;
}
// 头插法插入节点
for (int i = 0; i < n - m; i++) {
ListNode mid = p.next;
p.next = p.next.next;
mid.next = g.next;
g.next = mid;
}
return zero.next;
}
}
NC15 求二叉树的层序遍历
public class Solution {
/**
*
* @param root TreeNode类
* @return int整型ArrayList<ArrayList<>>
*/
public ArrayList<ArrayList<Integer>> levelOrder (TreeNode root) {
// write code here
ArrayList<ArrayList<Integer>> res = new ArrayList<>();
if(root==null){
return res;
}
DFS(root,res,0);
return res;
}
public void DFS(TreeNode root,ArrayList<ArrayList<Integer>> list,int depth){
if(root==null){
return;
}
if(list.size()==depth){
list.add(new ArrayList<Integer>());
}
list.get(depth).add(root.val);
DFS(root.left,list,depth+1);
DFS(root.right,list,depth+1);
}
}
链表中的节点每k个一组翻转
public class Solution {
public ListNode reverseKGroup (ListNode head, int k) {
if(head==null){
return null;
}
ListNode a,b;
a=head;
b=head;
for(int i = 0;i < k;i++){
if(b==null){
return head;
}
b=b.next;
}
ListNode newHead =reverse(a,b);
a.next=reverseKGroup(b,k);
return newHead;
}
public ListNode reverse(ListNode a,ListNode b){
ListNode pre ,cur,nxt;
pre =null;
cur=a;
nxt=a;
while(cur!=b){
nxt=cur.next;
cur.next=pre;
pre=cur;
cur=nxt;
}
return pre;
}
}
判断链表中是否有环
class Solution {
public char firstUniqChar(String s) {
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1);
}
for (int i = 0; i < s.length(); i++) {
if (map.get(s.charAt(i)) == 1) {
return s.charAt(i);
}
}
return ' ';
}
}
链表中环的入口结点
import java.util.HashSet;
import java.util.Set;
public class Solution {
public ListNode EntryNodeOfLoop(ListNode pHead) {
Set<Integer> nodes = new HashSet<>();
ListNode cur = pHead;
for (int i = 1; cur != null; i++) {
nodes.add(cur.val);
if (i != nodes.size()) {
break;
}
cur = cur.next;
}
return cur;
}
}
删除链表的倒数第n个节点
很喜欢的一道题,双指针找到位置。
public class Solution {
/**
* @param head ListNode类
* @param n int整型
* @return ListNode类
*/
public ListNode removeNthFromEnd(ListNode head, int n) {
// write code here
ListNode fast = head;
ListNode slow = head;
for (int i = 0; i < n; i++) {
fast = fast.next;
}
if (fast == null) {
return head.next;
}
while (fast.next != null) {
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return head;
}
}
数组
寻找第K大
public class Solution {
public int findKth(int[] a, int n, int K) {
// write code here
Integer[] b = new Integer[a.length];
for (int i = 0; i < a.length; i++) {
b[i] = a[i];
}
Arrays.sort(b, Collections.reverseOrder());
return b[K - 1];
}
}
两数之和
public class Solution {
/**
*
* @param numbers int整型一维数组
* @param target int整型
* @return int整型一维数组
*/
public int[] twoSum (int[] numbers, int target) {
// write code here
int[] res =new int[2];
for(int i=0;i<numbers.length;i++){
for(int j=i+1;j<numbers.length;j++){
if(numbers[i]+numbers[j]==target){
res[0]=i+1;
res[1]=j+1;
break;
}
}
if(res[1]!=0){
break;
}
}
return res;
}
}
合并两个排序的链表
public class Solution {
public ListNode Merge(ListNode list1,ListNode list2) {
if(list1==null){
return list2;
}
if(list2==null){
return list1;
}
if(list1.val<list2.val){
list1.next=Merge(list1.next,list2);
return list1;
}else{
list2.next=Merge(list1,list2.next);
return list2;
}
}
}
动态规划
连续子数组的最大和
public class Solution {
public int FindGreatestSumOfSubArray(int[] array) {
int res =array[0] ;
for(int i =1;i<array.length;i++){
array[i] +=Math.max(array[i-1],0);
res=Math.max(res,array[i]);
}
return res;
}
}
动态想不到,最起码暴力求得会
public class Solution {
public int FindGreatestSumOfSubArray(int[] array) {
int max = array[0];
int sum = 0;
for(int i=0;i<array.length;i++){
sum = 0;
for(int j=i;j<array.length;j++){
sum+=array[j];
max=Math.max(sum,max);
}
}
return max;
}
}
最长无重复子数组
public class Solution {
/**
* @param arr int整型一维数组 the array
* @return int整型
*/
public int maxLength(int[] arr) {
// write code here
//安排一个map记录每个数字的坐标
Map<Integer, Integer> dic = new HashMap<>();
int tmp = 0;
int res = 0;
for (int j = 0; j < arr.length; j++) {
int i = dic.getOrDefault(arr[j], -1);
//实时更新坐标位置
dic.put(arr[j], j);
//核心:出现重复元素之间距离和临时变量tmp做对比,
tmp = tmp < j - i ? tmp + 1 : j - i;
res = Math.max(res, tmp);
}
return res;
}
}
栈
有效括号序列
import java.util.*;
public class Solution {
/**
* @param s string字符串
* @return bool布尔型
*/
public boolean isValid(String s) {
// write code here
Stack<Character> stack = new Stack<>();
if (s == null) {
return false;
}
for (char a : s.toCharArray()) {
if (a == '[') {
stack.push(']');
} else if (a == '{') {
stack.push('}');
} else if (a == '(') {
stack.push(')');
} else if (stack.isEmpty() || a != stack.pop()) {
return false;
}
}
return stack.isEmpty();
}
}
字符串
大数加法
class Solution {
public String solve(String s, String t) {
int i = s.length() - 1, j = s.length() - 1;
int temp = 0;
StringBuilder out = new StringBuilder();
while (i >= 0 || j >= 0 || temp != 0) {
temp += i >= 0 ? s.charAt(i--) - '0' : 0;
temp += j >= 0 ? s.charAt(j--) - '0' : 0;
out.append(temp % 10);
temp = temp / 10;
}
return out.reverse().toString();
}
}