自定义博客皮肤VIP专享

*博客头图:

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

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

博客底图:

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

栏目图:

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

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(59)
  • 资源 (1)
  • 收藏
  • 关注

原创 一大堆字符串/公共子串/前缀/回文串

2、公共子串2.1字符串A是否含有字符串B int m = haystack.length(); int n=needle.length(); if(n==0) return 0; for(int i=0;i<m-n+1;i++){ int k = i; boolean flag = true; for(int j=0;j<n;j++){ if(haystack.charAt(k++)!

2021-06-02 19:18:21 223

原创 二分查找普通/缺失数字/三数值和

找出缺失的数字public class Solution { public int solve (int[] a) { int left = 0,right = a.length-1; while(left<right){ int mid = (left+right)/2; if(a[mid]==mid) left=mid+1; else right = mid-1; }

2021-06-02 19:11:23 164

原创 链表倒数第k个/找出和删除

找出链表倒数第k个节点 if(pHead==null) return null; ListNode slow = pHead; ListNode fast = pHead; while(fast!=null&&k>0){ fast = fast.next; k--; } if(k!=0) return null; while(f

2021-06-02 18:48:25 118

原创 力扣0/1背包问题:416/494

背包问题;给你一个整数数组 nums 和一个整数 target 。向数组中的每个整数前添加 ‘+’ 或 ‘-’ ,然后串联起所有整数,可以构造一个 表达式 :例如,nums = [2, 1] ,可以在 2 之前添加 ‘+’ ,在 1 之前添加 ‘-’ ,然后串联起来得到表达式 “+2-1” 。返回可以通过上述方法构造的、运算结果等于 target 的不同 表达式 的数目。public int findTargetSumWays(int[] nums, int S) { int su

2021-05-24 20:46:45 389

原创 字符串的压缩和解压缩-力扣443/394

字符串解码力扣-394一个栈存储数字,一个栈存储“】”之前遍历过的ans访问到数字: num=number;访问到字符串:ans.append()访问到“【”: 数字入栈,ans入栈,内容清空;访问到“】”:数字出栈,temp.append(pop)遍历; temp.append(多少次) ans = temp;代码如下:public String decodeString(String s) { Stack<Int

2021-05-24 15:10:52 223

原创 123的全排列力扣dfs

public List<List<Integer>> permute(int[] nums) { List<List<Integer>> list = new ArrayList<>(); List<Integer> list1=new ArrayList<>(); dfs(nums,list,list1); return list; } pub

2021-05-19 21:55:38 160

原创 力扣19--DFS-电话号码的组合

public List<String> letterCombinations(String digits) { if(digits.length()==0) return ans; dfs(digits,new StringBuilder(),0); return ans; } public void dfs(String s, StringBuilder res, int index){ //递归终止条件

2021-05-19 21:54:35 75

原创 最长连续递增序列---动态规划

返回递增长度 dp[0] = 1; int ans = 1; for(int i=1;i<len;i++){ dp[i] = 1; for(int j=0;j<i;j++){ if(nums[j]<nums[i]&&j<i){ dp[i] = Math.max(dp[j]+1,dp[i]);}} ans = Math.max(ans,dp[i]);

2021-04-26 17:01:41 76

原创 下一个排列---力扣31

下一个排列:1从后向前查找第一个相邻升序的元素对 (i,j),满足 A[i] < A[j]。此时 [j,end) 必然是降序2.在 [j,end) 从后向前查找第一个满足 A[i] < A[k] 的 k。3.将 A[i] 与 A[k] 交换,可以断定这时 [j,end) 必然是降序,逆置 [j,end),使其升序128 5764128 6754128 6457class Solution { public void nextPermutation(int[]

2021-04-25 21:49:33 51

原创 删除链表中重复元素

删除链表中重复元素ListNode cur = head; while(cur.next!=null){ if(cur.val!=cur.next.val){ cur = cur.next; } else{ cur.next=cur.next.next; } } return

2021-04-20 22:34:58 53

原创 2-两数之和链表进位

两数之和力扣第二题 终于做完了主要是进位;ListNode dummy = new ListNode(0); ListNode cur = dummy; int carry = 0; while(l1!=null||l2!=null){ int x = l1==null?0:l1.val; int y = l2==null?0:l2.val; int sum = x+y+carry;

2021-04-16 10:33:48 91

原创 452--射气球--贪心

if(points==null||points.length==0) return 0; //右边界排序; Arrays.sort(points,(a,b)-> a[1]>b[1]?1:-1); int count = 1; int pos = points[0][1]; for(int i=1;i<points.length;i++){ if(points[i][0]>pos).

2021-03-30 20:46:43 79

原创 力扣198-打家劫舍

动态规划 //只有一间房,两间房; if(nums.length<=1) return nums[0]; int[] dp = new int[nums.length]; dp[0]=nums[0]; dp[1] = Math.max(nums[0],nums[1]); for(int i=2;i<nums.length;i++){ dp[i] = Math.max(dp[i-2]+nums[i],dp[i-1]); }

2021-03-30 16:26:43 43

原创 排序算法整理

快排public void quickSort(int[] arr, int left, int right) { if(left>=right) return; int cur = arr[left]; int i=left,j=right; while(i<j){ while(i<j&&arr[j]>=cur) j--; while(i<j&am

2021-03-26 21:20:03 59

原创 剑指54-二叉树第k大的节点(中序遍历)

右根左class Solution { int ans = 0,count=0; public int kthLargest(TreeNode root, int k) { inorder(root,k); return ans; } public void inorder(TreeNode root,int k){ if(root==null) return; if(root.right!=null) ino

2021-03-24 16:20:09 53

原创 剑指offer-53-找出有序数组的重复数字/缺失数字

二分法解决有序数组问题找出缺失的数字public int missingNumber(int[] nums) { int left=0,right = nums.length-1; while(left<right){ int mid = (left+right)/2; if(nums[mid]!=mid) right=mid; else left=mid+1; }

2021-03-23 20:42:18 297

原创 剑指offer38-字符串排列

class Solution { List<String> res = new LinkedList<>(); char[] c; public String[] permutation(String s) { c = s.toCharArray(); dfs(0); return res.toArray(new String[res.size()]); } void dfs(int x) {

2021-03-23 16:17:43 63

原创 剑指-12:矩阵中的路径-太难了

回溯递归的办法解决 public boolean exist(char[][] board, String word) { //字符串转化为字符数组 char[] words = word.toCharArray(); for(int i = 0; i < board.length; i++) { for(int j = 0; j < board[0].length; j++) { if(dfs(board, words, i,

2021-03-22 21:55:32 65

原创 剑指14-割绳子

动态规划 int[] dp = new int[n+1]; dp[2] = 1; for(int i=3;i<n+1;i++){ //第一次割j=2; for(int j=2;j<i;j++){ dp[i] = Math.max(dp[i],Math.max(j*(i-j),j*dp[i-j])); } } ret

2021-03-22 19:55:44 159

原创 剑指-42-连续最大和

1.正常传统方法 int sum = 0; int ans = 0; for(int i = 0;i<nums.length;i++){ if(sum>0){ sum+=nums[i]; } else sum = nums[i]; ans = Math.max(ans,sum); } ret

2021-03-20 15:21:36 84

原创 剑指-58-反转字符串

双指针还可以分割后for循环拼接class Solution { public String reverseWords(String s) { s=s.trim(); int i=s.length()-1; int j=i; StringBuilder ans = new StringBuilder(); while(i>=0){ while(i>=0&&s.ch

2021-03-18 22:13:47 148

原创 剑指07-重建二叉树

这题是真难啊,死记硬背 呜呜呜太难了class Solution { int preorder[]; HashMap<Integer, Integer> hs = new HashMap<>(); public TreeNode buildTree(int[] preorder, int[] inorder) { this.preorder=preorder; for(int i=0;i<inorder.length;i

2021-03-18 20:01:01 57

原创 剑指offer-35复杂链表的复制

class Solution { public Node copyRandomList(Node head) { if(head == null) return null; Node cur = head; Map<Node, Node> map = new HashMap<>(); // 3. 复制各节点,并建立 “原节点 -> 新节点” 的 Map 映射 while(cur != nul

2021-03-18 16:26:48 47

原创 二叉树层次遍历

class Solution { public int[] levelOrder(TreeNode root) { if(root==null) return new int[0]; Queue<TreeNode> que = new LinkedList<>(); ArrayList<Integer> ans= new ArrayList<>(); que.add(root);

2021-03-16 21:30:36 55

原创 946-判断b数组是否为a入栈后的弹出顺序

构造辅助栈将a数组的元素入栈。如果遇到b数组的数。就把他pop,判断最后a数组全入栈了,栈是否为空Stack<Integer> st = new Stack<>(); int j=0; //把pushed元素入栈,如果与出栈元素相同,就pop出去 for(int i:pushed){ st.push(i); while(!st.isEmpty()&&st.pe

2021-03-16 20:52:38 82

原创 155-最小元素的栈

最小栈public class MinStack { Stack <Integer> st; Stack <Integer> minStack; /** initialize your data structure here. */ public MinStack() { st = new Stack<>(); minStack = new Stack<>(); } public

2021-03-16 20:49:44 77

原创 剑指offer-29-顺时针打印矩阵

顺时针打印矩阵if(matrix.length == 0) return new int[0]; int r1=0,l1=0,r2=matrix.length-1,l2=matrix[0].length-1; int k=0; int[] res = new int[(r2+1)*(l2+1)]; while(true){ // 左→右 r1++ for(int i=l1;i<=l2;i++)

2021-03-16 20:02:20 47

原创 剑指0ffer-28-对称二叉树

与判断两个二叉树是否相同基本一致就是先写一个判断root的left和right是否相等,然后再调用该函数判断是否镜像class Solution { //判断树的左右节点是否相同 public boolean isMirror(TreeNode L,TreeNode R){ if(L==null&&R==null) return true; if(L==null||R==null) return false; if(L.va

2021-03-15 17:31:01 73

原创 剑指offer-26-判断树是否包含

先判断是否相等,再遍历,看是否包含class Solution { //判断A是否与B相等 public boolean issame(TreeNode A, TreeNode B){ if(B==null){ return true; } if(A==null||A.val!=B.val){ return false; } return issame(A.le

2021-03-15 17:07:35 58

原创 剑指offer-27二叉树的镜像

二叉树的镜像,递归即可class Solution { public TreeNode mirrorTree(TreeNode root) { if(root==null) return null; //交换左右子树 TreeNode temp = root.left; root.left=root.right; root.right=temp; //递归 mirrorTree(root

2021-03-15 17:06:03 60

原创 剑指offer-16-计算整数次方

计算整数的次方-还是与运算移位问题 public double myPow(double x, int n) { if(n==0) return 1; if(x==0) return 0; long n1=n; double ans = 1.0; if(n1<0){ x=1/x; n1=-n1; } while(n1!=0){

2021-03-14 21:37:35 57

原创 剑指offer-15-二进制数字中1的个数

方法1:将n和1作与运算,n不断右移(容易造成死循环,当为负数时)public int hammingWeight(int n) { int count=0; while(n!=0){ if((n&1)==1){ count++; } n=n>>>1; } return count; }方法2:

2021-03-13 21:19:47 82

原创 剑指03----删除重复元素(无序)

1.置换法n,1-最优class Solution { public int findRepeatNumber(int[] nums) { for(int i=0;i<nums.length;i++){ while(nums[i]!=i){ //重复返回 if(nums[i]==nums[nums[i]]){ return nums[i]; }

2021-03-12 22:03:19 73

原创 动态规划509

class Solution { public int fib(int n) { //如果不写这一句,数组越界; if(n==0) return 0; int[] dp = new int[n+1]; dp[0]=0; dp[1]=1; for(int i = 2;i<n+1;i++){ dp[i]=(dp[i-1]+dp[i-2])%1000000007;

2021-03-10 21:12:42 57

原创 814-二叉树剪枝-递归

递归好难啊 aaapublic TreeNode pruneTree(TreeNode root) { if(root==null){ return null; } root.left = pruneTree(root.left); root.right = pruneTree(root.right); if(root.left==null&&root.right==null&&

2021-02-23 22:16:49 57

原创 141-环形链表;876-链表中间结点234回文链表

快慢指针的思想public class Solution { public boolean hasCycle(ListNode head) { ListNode fast = head; ListNode slow = head; while(fast!=null&&fast.next!=null){ fast=fast.next.next; slow=slow.next;

2021-02-23 22:06:09 59

原创 104-二叉树最大深度/111-最小深度

非递归方法:Queue<TreeNode> que = new LinkedList<TreeNode>(); que.add(root); int max_high = 0; while(!que.isEmpty()){ int m = que.size(); for(int i= 0;i<m;i++){ TreeNode node=que.poll

2021-02-23 21:39:33 60

原创 1528-重新排列字符串

class Solution { public String restoreString(String s, int[] indices) { char[] ans = new char[s.length()]; for(int i=0;i<indices.length;i++){ ans[indices[i]]=s.charAt(i); } return new String(ans); }}

2021-02-22 22:34:28 101

原创 799-倒香槟杯

**倒香槟杯,**中等难度,第一道数学有关的题仔细分析也不是很难class Solution { public double champagneTower(int poured, int query_row, int query_glass) { //数组的声明 double[][] m = new double[101][101]; m[0][0] = (double) poured; for(int i=0;i<que

2021-02-20 22:53:35 91

原创 2021-02-08

数组排序Arrays.sort(nums1);栈的操作:push,pop,peek,size队列操作add,poll,peek,size字符串操作转StringBuilder 优化HashMap集合put,get,keySet遍历,containsKey()HashSet集合add,get

2021-02-20 22:40:04 59

二手货交易系统

实验报告实验报告,,,,,,报告报告,报告,需求分析,需求分析

2017-12-24

空空如也

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

TA关注的人

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