自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(129)
  • 收藏
  • 关注

原创 文章格式模板

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录一、pandas是什么?二、使用步骤1.引入库2.读入数据一、pandas是什么?二、使用步骤1.引入库import numpy as npimport pandas as pdimport matplotlib.pyplot as pltimport seaborn as snsimport warningswarnings.filterwarnings('ignore')import sslssl._c

2021-10-11 19:59:03 262

原创 剑指 Offer 54. 二叉搜索树的第k大节点

java:二叉搜索树是按照中序遍历排序,要找第k大的数,可以用反中序遍历,发现res的长度==k就中止遍历。class Solution { List<Integer> res = new ArrayList<>(); public int kthLargest(TreeNode root, int k) { kthLargestCore(root, k); return res.get(res.size() - 1); .

2020-12-13 19:02:39 160

原创 剑指 Offer 59 - I. 滑动窗口的最大值

java:维持一个队列que,遍历数组,每次把que后边小于nums[i]的数字出队再加入nums[i]当遍历的下标i达到窗口大小的时候把结果(队首加入res),接着判断目前窗口的第一个数字和队首值一不一样,一样需要出队。class Solution { public int[] maxSlidingWindow(int[] nums, int k) { if (nums.length < 0 || k == 0) return new int[.

2020-12-13 18:43:37 146

原创 剑指 Offer 56 - II. 数组中数字出现的次数 II

java:把数组中每个位置的1出现次数相加这个位置1出现的次数%3为1说明出现一次的这个数字这个位置是1。class Solution { public int singleNumber(int[] nums) { int res = 0; for (int i = 0; i < 32; i++) { int cmp = 1 << i; int count = 0; fo.

2020-12-13 17:47:16 138

原创 剑指 Offer 56 - I. 数组中数字出现的次数

java:先把数组的数字异或在一起tem<相异为1>找到tem第一个为1的数字<两个出现一次的数字异或后说明两个数在这个位置上数字不一样>把数组的数字以这个位置上的数字分类分别异或,最后得到两个数字class Solution { public int[] singleNumbers(int[] nums) { int tem = 0; for (int i = 0; i < nums.length; i++) { .

2020-12-13 17:41:33 138

原创 剑指 Offer 59 - II. 队列的最大值

java:用一个队列deq保存最大值,一个队列que保存所有值push_back val时,把deq尾小于val的值都删除再入val最大值一直在deq的首部pop的时候如果pop的值和deq队首的值一样就把deq也popclass MaxQueue { LinkedList<Integer> que, dque; public MaxQueue() { que = new LinkedList<>(); dque = new.

2020-12-10 21:08:57 101

原创 110. 平衡二叉树

java:class Solution { public boolean isBalanced(TreeNode root) { if (isBalancedCore(root) == -1) return false; return true; } public int isBalancedCore(TreeNode root) { if (root == null) return.

2020-12-04 15:29:51 110

原创 剑指 Offer 28. 对称的二叉树

java:class Solution { public boolean isSymmetric(TreeNode root) { if (root == null) return true; return isSymmetricCore(root.left, root.right); } public boolean isSymmetricCore(TreeNode left, TreeNode right) { .

2020-12-04 14:57:56 86

原创 82. 删除排序链表中的重复元素 II(重复节点不加入)

java:pre指向结果的尾巴,node和next用于过滤重复的节点class Solution { public ListNode deleteDuplicates(ListNode head) { if (head == null) return head; ListNode pre = new ListNode(-1); ListNode res = pre; ListNode node = head.

2020-12-04 14:44:41 164

原创 107. 二叉树的层次遍历 II(从上往下打印)

java:class Solution { List<List<Integer>> res = new ArrayList<>(); public List<List<Integer>> levelOrderBottom(TreeNode root) { if (root == null) return res; LinkedList<TreeNode> que.

2020-12-04 11:29:17 174 1

原创 排序基础

1. 冒泡排序(1) 代码//第一种最简单的冒泡排序,下面代码将数组数字递增排列;class Solution {public: vector<int> bubbleSort(vector<int> vec) { int size = vec.size(); for (int i = size - 1; i > 0; i--) {//每一次遍历是把遍历到的数字放在下面的循环向上冒泡; for (int j = size - 1; j > 0; j-

2020-12-04 09:48:52 135

原创 112. 路径总和(返回boolean)

java:class Solution { public boolean hasPathSum(TreeNode root, int sum) { return hasPathSumCore(root, sum) == 1 ? true : false; } public int hasPathSumCore(TreeNode root, int sum) {//注!!返回有三种状态可以用int返回判断 if (root == null) .

2020-12-03 21:02:10 102

原创 113. 路径总和 II(返回数组结果)

java:class Solution { public List<List<Integer>> res = new ArrayList<>(); public List<Integer> path = new ArrayList<>(); public List<List<Integer>> pathSum(TreeNode root, int sum) { pathSumCor.

2020-12-03 20:34:53 97

原创 19. 删除链表的倒数第N个节点

java:class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { ListNode l1 = head, l2 = head; ListNode l3 = new ListNode(-1); ListNode res = l3;//注!!需要考虑删除的是head节点 l3.next = head; for (int i = 0.

2020-12-02 21:24:24 85

原创 剑指 Offer 52. 两个链表的第一个公共节点

java:/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public ListNode getIntersec.

2020-12-02 21:14:48 99

原创 组合之22. 括号生成

java:回溯:记录左右括号的数量,左括号小于n就继续加左括号,右括号比左括号少就加右括号,最后在path的结果等于2*n就收集结果。class Solution { List<String> res; StringBuilder path; public List<String> generateParenthesis(int n) { res = new ArrayList<>(); path = new .

2020-12-01 11:28:23 152

原创 剑指 Offer 30. 包含min函数的栈

java:两个栈,一个保存最小值,一个正常保存class MinStack { public LinkedList<Integer> st; public LinkedList<Integer> minSt; /** initialize your data structure here. */ public MinStack() { st = new LinkedList<>(); minSt = n.

2020-12-01 10:25:42 67

原创 剑指 Offer 10- I. 斐波那契数列

java:class Solution { public int fib(int n) { if (n == 0) return 0;//注!! if (n == 1) return 1; int tem1 = 0, tem2 = 1, sum = 0; while (n >= 2) { sum = (tem1 + tem2) % 1000000007;//注!!取模 tem1 .

2020-11-30 21:02:50 80

原创 剑指 Offer 32 - III. 从上到下打印二叉树 III(之字形)

java:class Solution { public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> res = new ArrayList<>(); if (root == null) return res; LinkedList<TreeNode> que =.

2020-11-30 20:43:27 91

原创 剑指 Offer 32 - II. 从上到下打印二叉树 II(分行)

java:class Solution { public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> res = new ArrayList<>(); if (root == null)//注!!root是空返回 return res; LinkedList<TreeN.

2020-11-30 20:17:24 223 2

原创 剑指 Offer 32 - I. 从上到下打印二叉树(不分行)

java:class Solution { public int[] levelOrder(TreeNode root) { if (root == null) return new int[0];//注!!root是空的直接返回 List<Integer> res = new ArrayList<>(); LinkedList<TreeNode> que = new LinkedList.

2020-11-30 19:40:18 63

原创 剑指 Offer 09. 用两个栈实现队列

java:push就push进st1,pop时st2是空的就把st1的值倒灌进st2,返回时st2是空的就返回-1,否则就返回st2 head。class CQueue { Stack<Integer> st1; Stack<Integer> st2; public CQueue() { st1 = new Stack<>(); st2 = new Stack<>(); } .

2020-11-30 19:09:20 59

原创 子集之90. 子集 II

java:class Solution { List<List<Integer>> res = new ArrayList<>(); List<Integer> path = new ArrayList<>(); public List<List<Integer>> subsetsWithDup(int[] nums) { boolean[] used = new boo.

2020-11-26 15:44:21 50

原创 子集之78. 子集

java:class Solution { List<List<Integer>> res = new ArrayList<>(); List<Integer> path = new ArrayList<>(); public List<List<Integer>> subsets(int[] nums) { backTracking(nums, 0); retur.

2020-11-26 15:03:32 87

原创 切割之131. 分割回文串

java:class Solution { List<List<String>> res = new ArrayList<>(); List<String> path = new ArrayList<>(); public List<List<String>> partition(String s) { backTracking(s, 0); return res;.

2020-11-26 12:51:38 75

原创 排列之剑指 Offer 38. 字符串的排列+回溯总结

java:用char数组class Solution { public List<String> res = new ArrayList<>(); public String[] permutation(String s) { char[] strs = s.toCharArray(); dfsCore(strs, 0); return res.toArray(new String[0]); } //注.

2020-11-24 15:56:34 56

原创 排列之47. 全排列 II

java:class Solution { List<List<Integer>> res = new ArrayList<>(); List<Integer> path = new ArrayList<>(); public List<List<Integer>> permuteUnique(int[] nums) { boolean[] used = new boolean[.

2020-11-24 14:42:22 215

原创 排列之46. 全排列(数组无重复)

java:class Solution { List<List<Integer>> res = new ArrayList<>(); List<Integer> path = new ArrayList<>(); public List<List<Integer>> permute(int[] nums) { boolean[] used = new boolean[nums.le.

2020-11-24 14:39:42 148

原创 144. 二叉树的前序遍历 94中序 145 后序

前序遍历中左右递归:/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNod

2020-11-23 18:37:45 965

原创 557. 反转字符串中的单词 III

class Solution { public String reverseWords(String s) { char[] chars = s.toCharArray(); int i = 0, j = 0; while (i < chars.length) { if ( chars[i] != ' ') { i++; } else { .

2020-11-23 14:39:02 78

原创 组合之__17. 电话号码的字母组合

import java.util.*;class Solution { public Map<Integer, String> map = new HashMap<Integer, String>(); public List<String> res = new ArrayList<>(); public StringBuilder path = new StringBuilder(); public List<St.

2020-11-23 14:19:29 63

原创 组合之216. 组合总和 III(数字无重复)

树枝无重复,树层也无重复。class Solution { public List<List<Integer>> res = new ArrayList<>(); public List<Integer> path = new ArrayList<>(); public int sum = 0; public List<List<Integer>> combinationSum3(int.

2020-11-23 14:05:53 123

原创 组合之__40. 组合总和 II(数组有重复)

两个不同下标的元素可以放在一个结果组合中,所以树枝可重复;给的数组有重复数字,但是结果的不同组合不能有重复,所以树层不能有重复;树层不能重复可以将数组排序建立used 的boolean数组,for遍历层的时候在递归函数之前把使用的元素置true,在递归函数之后把使用的元素置false。所以在层遍历的时候used[i - 1] true说明树枝用过这个相同的元素(本题不用),used[i - 1] false说明树层用过这个相同的元素(本题用,因为对used数组的修改是先改true再改false的),再.

2020-11-23 14:01:37 134

原创 组合之39. 组合总和(数组无重复)

java:class Solution { public List<List<Integer>> res = new ArrayList<>(); public List<Integer> path = new ArrayList<>(); public int temSum = 0; public List<List<Integer>> combinationSum(int[] cand.

2020-11-22 21:49:52 144

原创 旋转数组之__剑指 Offer 11. 旋转数组的最小数字

java:class Solution { public int minArray(int[] numbers) { int left = 0, right = numbers.length - 1; while (left < right) { int middle = left + (right - left) / 2; if (numbers[middle] < numbers[right]) {/.

2020-11-22 20:34:57 74

原创 旋转数组之__33. 搜索旋转排序数组(没有重复数字)

java:class Solution { public int search(int[] nums, int target) { int left = 0, right = nums.length - 1; while (left < right) { int middle = left + (right - left) / 2; if (nums[middle] < nums[right]) { .

2020-11-22 20:34:42 68

原创 组合之77. 组合

java:class Solution { List<List<Integer>> res = new ArrayList<>(); List<Integer> path = new ArrayList<>(); public List<List<Integer>> combine(int n, int k) { backTracking(n, k, 1); ret.

2020-11-22 20:34:16 68

原创 剑指 Offer 58 - II. 左旋转字符串

java://用java apiclass Solution { public String reverseLeftWords(String s, int n) { String str1 = s.substring(0, n); String str2 = s.substring(n, s.length()); return str2 + str1; }}cpp:```cppclass Solution {public: .

2020-11-21 16:18:38 71

原创 剑指 Offer 58 - I. 翻转单词顺序

java:class Solution { public String reverseWords(String s) { String tem = s.trim();// 删除首尾空格 StringBuilder result = new StringBuilder(); int left = tem.length() - 1, right = tem.length() - 1; while (left >= 0) {//从后.

2020-11-21 16:10:20 93

原创 189. 旋转数组

java:class Solution { public void rotate(int[] nums, int k) { k = k % nums.length;//注!!移动k个位置当k超过数组长度时候,需要取余 swapNums(nums, 0, nums.length - 1); swapNums(nums, 0, k - 1); swapNums(nums, k, nums.length - 1); } p.

2020-11-20 16:10:00 102

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除