
leetcode刷题java
java刷题记录
暴走的小小菜鸟
这个作者很懒,什么都没留下…
展开
-
LeetCode(38 外观数列)
如题看上去先用递归来写写 public static String countAndSay(int n) { if (n==1){ return "1"; } StringBuilder rel =new StringBuilder(countAndSay(n-1));//递归获取 StringBuilder re =new StringBuilder(); int count =1;原创 2021-06-17 19:51:37 · 155 阅读 · 0 评论 -
LeetCode(36. 有效的数独)
如题检测每行每列每个3*3格子内的数字情况,可以使用各种方式记录数字,这里选择位操作 public static boolean isValidSudoku(char[][] board) { for (int x=0;x<9;x++){ int xn=0;//横向缓存 987654321 9位按位记录数字情况 int yn=0;//纵向缓存 for(int y=0;y<9;y++){原创 2021-04-26 18:50:33 · 130 阅读 · 0 评论 -
LeetCode(35. 搜索插入位置)
如题又是刻在dna中的二分法 public static int searchInsert(int[] nums, int target) { int left =0; int right = nums.length-1; if (target<=nums[0]){//小于等于最小值,插入0 return 0; }else if (target==nums[right]){//=最大,插入原最后一位原创 2021-04-26 18:40:39 · 84 阅读 · 0 评论 -
LeetCode(34. 在排序数组中查找元素的第一个和最后一个位置)
如图刻在DNA里面的二分法。先上二饭查找,再往两边匹配public static int[] searchRange(int[] nums, int target) { if (nums.length == 0) { return new int[]{-1, -1}; } int left = 0; int right = nums.length - 1; int middle = -1;原创 2021-04-07 20:20:04 · 73 阅读 · 0 评论 -
LeetCode(33. 搜索旋转排序数组)
如题找数的最直接想法就是,排序二分两板斧了,直接有序,那就上二分大法,有翻转很显然会有特殊情况的处理public static int search(int[] nums, int target) { if ((nums[0] > nums[nums.length - 1] && target < nums[0] && target > nums[nums.length - 1]) || //在范围外的直接排除原创 2021-04-07 11:32:37 · 68 阅读 · 0 评论 -
LeetCode(32. 最长有效括号)
如题对于匹配有效括号的最直接思路恐怕就是栈了,但是在这题,没必要完全使用栈public static int longestValidParentheses(String s) { int max = 0; int len = 0;//当前连续有效长度 int count = 0;//计数标志 int bre = 0;//下次循环继续节点前一位 for (int i = 0; i < s.length(); i++)原创 2021-04-01 19:56:05 · 168 阅读 · 0 评论 -
LeetCode(19. 删除链表的倒数第N个节点)
如题删除倒数第n个元素即将倒数第n+1个元素的next指向倒数第n-1个即可,直观做法先遍历得到node个数,然后找倒数第n+1个元素public static ListNode removeNthFromEnd(ListNode head, int n){ int index =0; ListNode node=head; while(node!=null){//获取元素个数 node=node.next;原创 2020-06-12 23:33:01 · 278 阅读 · 0 评论 -
LeetCode(1431. 拥有最多糖果的孩子)
如题这就很简单吧,找到最大值,然后判断每一位的加上额外是否大于等于最大值,就是注意一个等于就好public static List<Boolean> kidsWithCandies(int[] candies, int extraCandies) { int max =Integer.MIN_VALUE;//现有最多糖果数 for(int i: candies){//找到现有最多的数量 if(i>max){原创 2020-06-01 20:59:17 · 202 阅读 · 0 评论 -
LeetCode(14. 最长公共前缀)
如题最直观的做法,依次用strs[0]的每个字符去匹配所有其他字符串的相同位置public static String get(String[] strs) { if(strs.length==0){ return ""; } if(strs.length==1){ return strs[0]; } String str = strs[0];//最大前缀 for (int i = 0; i < str.length(); i++) { char ch原创 2020-05-29 18:54:06 · 140 阅读 · 0 评论 -
LeetCode(5. 最长回文子串)
如题起初想偷懒用dp,index为i处的最大回文串和包含i的最大回文串推导i+1的 //暂时无法实现 public static String longestPalindrome1(String s) { if (s.length() < 1) { return ""; } char[] cs = s.toCharArray(); List<int[][]> data = new ArrayList<>(); int[][] data1 = { {原创 2020-05-29 18:13:52 · 122 阅读 · 0 评论 -
LeetCode(16. 最接近的三数之和)
如题先直接暴力安排,三轮循环冲冲冲public static int threeSumClosest(int[] nums, int target) { int min=Integer.MAX_VALUE; int minsum=0; for(int i=0;i<nums.length-2;i++) { //第一重遍历 for(int x=i+1;x<nums.length-1;x++) { //第二次遍历 for(int y=x+1;y<nums.leng原创 2020-05-29 18:00:49 · 114 阅读 · 0 评论 -
LeetCode(198. 打家劫舍)
如题审题,偷了第i家以后是两种可能的做法,偷i+2家或者偷i+3家的,首先用回溯穷举试试public static void doalone(int idex,int[] nums,int sum,int[]re){ if(idex<nums.length){ sum+=nums[idex]; doalone(idex+2,nums,sum,re);//偷隔一位 doalone(idex+3,nums,sum,原创 2020-05-29 16:41:37 · 251 阅读 · 0 评论 -
LeetCode(13. 罗马数字转整数)
如题就是一个逐字符匹配了public static int romanToInt(String s) { int num = 0; int i=0; while(i<s.length()) { char ch = s.charAt(i); switch (ch) { case ('M'): num += 1000; break; case ('D'): num += 500; break; case ('C'): i原创 2020-05-28 18:51:38 · 100 阅读 · 0 评论 -
LeetCode(12. 整数转罗马数字)
如题首先想到的是通过进制位去匹配public static String change(int num) { StringBuilder sb = new StringBuilder(""); int a = num / 1000; int b = num % 1000; int c = num % 100; int d = num % 10; for (int i = 0; i < a; i++) {// 千位处理 sb.append("M"); } for原创 2020-05-28 18:30:19 · 101 阅读 · 0 评论 -
LeetCode(394 字符串解码)
如题有两种思路,一种是从外层向内层拼接public static String decodeString(String s) { if(s.length()==0) { return ""; } return write(new StringBuilder(""),s); } public static String write(StringBuilder sb,String s) { boolean flag =true;//包含标志 int bf = 0;//原创 2020-05-28 17:10:50 · 142 阅读 · 0 评论 -
LeetCode(11盛最多水的容器)
如题最直接的做法,直接从所有两两组合中找最大public static int maxArea(int[] height) { int max = 0; for (int i = 0; i < height.length - 1; i++) {// 左边界 for (int m = i + 1; m < height.length; m++) {// 右边界 if (Math.min(height[i], height[m]) * (m - i) > max)原创 2020-05-27 00:25:06 · 108 阅读 · 0 评论 -
LeetCode(9回文数)
如题基本思路就是倒过来比较是否相等public static boolean isPalindrome(int x) { if(x<0) { return false; } int res=0; int i=x; while(i!=0) { res=res*10+i%10; i/=10; } return res==x; }原创 2020-05-27 00:02:41 · 95 阅读 · 0 评论 -
LeetCode(8 字符串转整数)
如题感觉难点还是就一个越界判断public static int myAtoi(String str) { str = str.trim();//去除头部空格 int num=0; if(str.charAt(0)=='-') {//负号打头 for(int i=1;i<str.length();i++) { char ch = str.charAt(i); if(ch>='0'&&ch<='9') { if(num<I原创 2020-05-26 23:57:58 · 403 阅读 · 0 评论 -
LeetCode(7 整数反转)
如题这就不用分析了,直接依次取每位即可,难点就是个越界判断public static int reverse(int x) { int res=0; while(x!=0) { int i = x%10; x=x/10; if(res>Integer.MAX_VALUE/10||(res==Integer.MAX_VALUE/10&&i>7) ||res<Integer.MIN_VALUE/10||(res==Integer.MIN_原创 2020-05-26 00:05:54 · 79 阅读 · 0 评论 -
LeetCode(6 Z字形变换)
如题一眼看上去两种思路,一种是直接模拟,用二维数组实现。另一种就是直接找规律了先来简单的找规律public static String convert(String s, int numRows) { if(numRows==1) { return s; } char [] chs = new char[s.length()]; int index = 0;//chs的赋值位index int t = 2*numRows-2;//相邻完整列同一行的索引之差 int i1=原创 2020-05-26 00:00:25 · 93 阅读 · 0 评论 -
LeetCode(4 两个正向数组的中位数)
如题一眼看上去,还被唬住了,直接双指针plus安排 public static double findMedianSortedArrays(int[] nums1, int[] nums2) { if(nums1.length==0) { return nums2.length%2==0?((double)(nums2[nums2.length/2-1]+nums2[nums2.length/2]))/2:nums2[nums2.length/2]; } if(nums2.length原创 2020-05-20 23:45:16 · 206 阅读 · 0 评论 -
LeetCode(面试53I 在排序数组中查找数字)
如题就没必要分析了,最直观的就是遍历计数呗public static int search(int[] nums, int target) { int count=0; for(int num :nums) { if(num==target) { count++; } } return count; }但是很显然,不是最优结果,分析题意,是有序数组,那就有更优化的找对应值的方法了public static int search1(int[] n原创 2020-05-12 10:15:27 · 114 阅读 · 0 评论 -
LeetCode(面试28 对称的二叉树)
如题一眼看到题目,感觉可以镜像遍历,然后比较结果public boolean isSymmetric(TreeNode root) { if (root == null) { return true; } List<Integer> l1 = new ArrayList<Integer>(); List<Integer> l2 = ne...原创 2020-04-30 18:38:34 · 134 阅读 · 0 评论 -
LeetCode(20有效的括号)
如题很显然的运用抵消的思想来做public boolean isValid(String s) { char [] cs=new char[s.length()];//顺序缓存未匹配字符 int i=0;//可填入的index for(int m=0;m<s.length();m++) { if(s.charAt(m)=='('||s.charAt(m)=='{'||s...原创 2020-04-20 17:11:58 · 133 阅读 · 0 评论 -
LeetCode(941 有效的山脉数组)
如题很直观的题目,就是判断数组是否是先递增后递减,先通过直接遍历来进行判断public boolean validMountainArray(int[] A) { if(A==null||A.length<3) { return false; } int front=A[0]; int flag =-1;//前序增1 减0 for(int i=1;i<A....原创 2020-04-20 16:09:30 · 179 阅读 · 0 评论 -
LeetCode(999 车的可用捕获量)
如题很直观的思路,检索先找到R然后在它的4个方向上向外检索,先检索到B退出,直接检索到p这计数+1 public static int numRookCaptures(char[][] board) { int count =0; for(int i=0;i<8;i++) { for(int j=0;j<8;j++) { ...原创 2020-01-15 14:33:04 · 230 阅读 · 0 评论 -
LeetCode(455 分发饼干)
如题思路很清晰,每一块饼干匹配尽可能大的孩子即可public static int findContentChildren(int[] g, int[] s) { int count =0; Arrays.sort(g); Arrays.sort(s); int m = s.length-1;//倒序从大到小检索 int n = g.length-1; wh...原创 2020-01-15 11:38:27 · 92 阅读 · 0 评论 -
LeetCode(1232 缀点成线)
如题 真直观的比较斜率即可public boolean checkStraightLine(int[][] coordinates) { if(coordinates==null||coordinates.length<2) { return false; } double m = (double)(coordinates[1][1]...原创 2020-01-14 18:39:21 · 223 阅读 · 0 评论 -
LeetCode(1252 奇数值单元格的数目)
如题如果直接构造二维数组进行操作后检索,肯定是不够优雅的。可以类比为几何问题,两个长方形求差集,对应的某一行或列变换两次将会还原,也就是仅仅只用关注出现次数为奇数的对应行列即可public int oddCells(int n, int m, int[][] indices) { int x = 0;//出现次数为奇数次的行数的数量 int y = 0;//出现...原创 2020-01-14 18:25:48 · 225 阅读 · 0 评论 -
LeetCode(563 二叉树坡度)
如题很显然需要用到递归,但是不适合从上至下穷举,那样对元素和的运算会有浪费。那么转换流程为从底部开始运算,计算节点坡度的同时缓存元素和 static int num=0; //坡度 public static int findTilt(TreeNode root) { Sum(root); //包括自己在内的子元素之和 return num; } static in...原创 2020-01-14 18:03:34 · 227 阅读 · 0 评论 -
LeetCode(1011 D天内运送包裹的能力)
如题感觉没有捷径可找,只能确定需要迭代查找max元素到元素和之间的每个次数,找到最小的那一个public static int shipWithinDays(int[] weights, int D) { int sum =0;//元素和 int max =0;//最大元素 for(int i:weights) { if(i>max) { max=i; ...原创 2020-01-09 18:35:10 · 322 阅读 · 0 评论 -
LeetCode(1094 拼车)
如题最直接的做法直接把每个站位的人数算出来再遍历比较即可public static boolean carPooling(int[][] trips, int capacity) { Map<Integer, Integer> peos = new HashMap<Integer, Integer>();// 每个候车点的人数 for (int i = 0; i...原创 2020-01-07 15:31:19 · 347 阅读 · 0 评论 -
LeetCode(853 车队)
如题很显然直接按照规则去检索会非常麻烦。想办法取巧实现,对于组成车队的情况,实际上就是后面的速度快赶上前面,并且赶上前面之后就可视为前面的了,那么很显然每辆车都只用与它位置上的前一位相比较,比较的基准是什么呢,可以选取到达时间,落后的到达时间更短很显然必然能汇合public static int carFleet(int target, int[] position, int[] speed)...原创 2020-01-02 17:47:19 · 150 阅读 · 0 评论 -
LeetCode(859 亲密字符串)
如题审题很重要哦,着重注意是存在差异的话是有仅有一对并且相同时存在重复字符也是可以的public static boolean buddyStrings(String A, String B) { if(A.length()!=B.length()) { return false; } char a='\u0000'; //对于差异点的备份 char b='\u0000...原创 2019-12-30 17:07:57 · 164 阅读 · 0 评论 -
LeetCode(554 砖墙)
如题很显然,就是比较每个缺口处的线穿过的砖块数 public static int leastBricks(List<List<Integer>> wall) { int count=0;//对应缺口位置相同的层数 Map<Integer,Integer> sumMap =new HashMap<Integer, ...原创 2019-12-30 11:21:56 · 137 阅读 · 0 评论 -
LeetCode(319 灯泡开关)
如题最直观的做法就是模拟每次开关操作,用一个int数组保存状态,每次去判断对应的位置更改对应index的值,但是很显然这是指数即的计算。那么换个方向,我们就以每个位置会进行开合的次数来判断,很显然开合奇数次则为开启,否则为关闭。那么开合次数是由上面决定的,间隔n不就是对应整除n么,那么对应的就是再该数的所有约数时进行开合操作,而约数都是成对存在的,其中的特殊情况即平方数,拥有一对相同的约数组。...原创 2019-12-30 10:11:38 · 173 阅读 · 0 评论 -
LeetCode(299 猜数字)
如题很直观的的想法就是通过缓存的思想来做public static String getHint(String secret, String guess) { int bulls=0; int cows=0; int [] ins = new int[secret.length()];//未匹配上的index缓存 Map<C...原创 2019-12-26 11:32:21 · 208 阅读 · 0 评论 -
LeetCode(18 四数之和)
如题相较于三数直观的做法就是再加一重循环public static List<List<Integer>> fourSum(int[] nums, int target) { List<List<Integer>> result = new ArrayList<List<Integer>>(); if(nums =...原创 2019-12-23 17:04:40 · 82 阅读 · 0 评论 -
LeetCode(923 三数之和各种可能)
如题与之前那个三数之和的可能序列有异曲同工之妙,但是那个是需要去重,这个则需要计算全部可能。那么先用相似的双指针法来实现 public static int threeSumMulti(int[] A, int target) { double count = 0; if (A == null || A.length < 3) { return (int)count;...原创 2019-12-20 17:54:53 · 199 阅读 · 0 评论 -
LeetCode(456 132模式)
如题首线肯定是可以用多重循环来做。但是想偷懒,用缓存化的思想来做。由题意是找到匹配即可,那么我们尽可能使用匹配程度更大的条件去匹配,那么有两种方向,一种是正向遍历,那么就是用大和小去匹配中,一种是倒序用大和中区匹配小。第一种对于匹配条件的更新存在麻烦 譬如 35 02 序列 这时候就需要匹配两段了,于是选择第二种。public static boolean find132pattern...原创 2019-12-19 12:07:08 · 184 阅读 · 0 评论