随笔分类 -  剑指offer系列

摘要:/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ 阅读全文
posted @ 2020-12-18 11:57 peanut_zh 阅读(113) 评论(0) 推荐(0)
摘要:/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ 阅读全文
posted @ 2020-12-18 11:29 peanut_zh 阅读(51) 评论(0) 推荐(0)
摘要:class Solution { public int strToInt(String str) { int res =0; //用于数字溢出判断 int bndry = Integer.MAX_VALUE / 10; //sign标记 正负号 int i = 0, sign = 1,length 阅读全文
posted @ 2020-12-18 01:26 peanut_zh 阅读(73) 评论(0) 推荐(0)
摘要://求 1+2+...+n ,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。class Solution { //定义res 表示计算结果 int res = 0; public int sumNums(int n) { // 当 阅读全文
posted @ 2020-12-18 00:17 peanut_zh 阅读(53) 评论(0) 推荐(0)
摘要:class Solution { public int maxProfit(int[] prices) { //minPrice表示 股票最低价购入 int minPrice = Integer.MAX_VALUE; //最大收益 int resultMax = 0 ; for(int i =1; 阅读全文
posted @ 2020-12-18 00:05 peanut_zh 阅读(58) 评论(0) 推荐(0)
摘要:import java.util.Set; import java.util.HashSet; public class Solution { public boolean isContinuous(int [] numbers) { //边界判断 if(numbers == null || num 阅读全文
posted @ 2020-12-17 21:45 peanut_zh 阅读(76) 评论(0) 推荐(0)
摘要:import java.util.ArrayList; public class Solution { public ArrayList<Integer> maxInWindows(int [] num, int size) { //结果集 ArrayList<Integer> res = new 阅读全文
posted @ 2020-12-17 20:55 peanut_zh 阅读(41) 评论(0) 推荐(0)
摘要:class Solution { public String reverseLeftWords(String s, int n) { //返回集 StringBuilder res = new StringBuilder(); //先添加 区间【n,s.length-1】 for(int i = n 阅读全文
posted @ 2020-12-17 20:13 peanut_zh 阅读(53) 评论(0) 推荐(0)
摘要://双指针 class Solution { public String reverseWords(String s) { //处理字符串 首尾空格 String str = s.trim(); //双指针 int right = str.length() - 1; //left 指向 最右, 从右 阅读全文
posted @ 2020-12-17 19:49 peanut_zh 阅读(82) 评论(0) 推荐(0)
摘要:这里贴的牛客,LeetCode 和牛客会有一丢丢不一样,不过解题思路是一样的。 //滑动窗口法,窗口区间一般规定为 左闭右开,这里也是 import java.util.ArrayList; public class Solution { public ArrayList<ArrayList<Int 阅读全文
posted @ 2020-12-17 18:45 peanut_zh 阅读(85) 评论(0) 推荐(0)
摘要://双指针法 class Solution { public int[] twoSum(int[] nums, int target) { //结果集 int[] tmp = new int[2]; //双指针,一头一尾 int start = 0; int end = nums.length-1; 阅读全文
posted @ 2020-12-17 17:25 peanut_zh 阅读(52) 评论(0) 推荐(0)
摘要:/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ 阅读全文
posted @ 2020-12-17 15:00 peanut_zh 阅读(57) 评论(0) 推荐(0)
摘要:/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ 阅读全文
posted @ 2020-12-17 12:24 peanut_zh 阅读(52) 评论(0) 推荐(0)
摘要:这里要注意题目的说法,课本、牛客 都是第K个节点 ,LeetCode 上说的是第K大的节点。大家都知道 二叉搜索树的中序遍历 可以得到一个递增序列,那么 第K个, 和第K大 是稍稍不一样的。 LeetCode /** * Definition for a binary tree node. * pu 阅读全文
posted @ 2020-12-17 12:06 peanut_zh 阅读(60) 评论(0) 推荐(0)
摘要://二分查找法 class Solution { public int missingNumber(int[] nums) { if(nums == null || nums.length <= 0) return -1; //定义左右边界 int left = 0; int right = num 阅读全文
posted @ 2020-12-17 01:45 peanut_zh 阅读(69) 评论(0) 推荐(0)
摘要:class Solution { public int search(int[] nums, int target) { int number =0; if(nums != null && nums.length>0){ int first = getFirstK(nums,nums.length, 阅读全文
posted @ 2020-12-17 01:18 peanut_zh 阅读(85) 评论(0) 推荐(0)
摘要:/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } * 阅读全文
posted @ 2020-12-16 22:30 peanut_zh 阅读(49) 评论(0) 推荐(0)
摘要://哈希表 class Solution { public char firstUniqChar(String s) { //定义一个HashMap,来保存字符出现的次数 Map<Character,Boolean> map = new HashMap<>(); char[] chars = s.t 阅读全文
posted @ 2020-12-16 21:12 peanut_zh 阅读(56) 评论(0) 推荐(0)
摘要://动态规划 class Solution { public int nthUglyNumber(int n) { //定义一个数组dp,来按序存放丑数 int[] dp = new int[n]; //第一个丑数是1 dp[0] = 1; //分别定义由质因子 2,3,5 乘以较小丑数得到的下标索 阅读全文
posted @ 2020-12-16 21:01 peanut_zh 阅读(117) 评论(0) 推荐(0)
摘要://滑动窗口法 class Solution { public int lengthOfLongestSubstring(String s) { Map<Character,Integer> map = new HashMap<>(); //最长不重复子串的长度 int res = 0; //滑动窗 阅读全文
posted @ 2020-12-16 20:33 peanut_zh 阅读(34) 评论(0) 推荐(0)