![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法练习
文章平均质量分 94
jinxlzc
展开
-
springboot集成spring data jpa使用排序查询(gradle)
排序JpaRepository中的api/* * (non-Javadoc) * @see org.springframework.data.repository.PagingAndSortingRepository#findAll(org.springframework.data.domain.Sort) */@OverrideList<T> findAll(Sort sort);注:若findAll不填参数,可调用List findAll(); 返回结果为数据表中的所有数据原创 2020-10-20 19:21:30 · 570 阅读 · 0 评论 -
springboot集成spring data jpa使用分页查询(gradle)
分页查询service在UserService中添加下面的方法Page<User> find(Integer page, Integer size);并在UserServiceImpl中进行实现@AutowiredUserRepository userRepository;@Overridepublic Page<User> find(Integer page, Integer size) { PageRequest pageable = PageReque原创 2020-10-20 19:20:29 · 251 阅读 · 0 评论 -
算法练习(48) leetcode 41. 缺失的第一个正数
https://leetcode-cn.com/problems/first-missing-positive/class Solution { public int firstMissingPositive(int[] nums) { if(nums.length==0){return 1;}; for (int i = 0; i < nums.length; i++) { if (nums[i] <= 0) {原创 2020-09-25 11:07:58 · 88 阅读 · 0 评论 -
算法练习(47) leetcode 125. 验证回文串
https://leetcode-cn.com/problems/valid-palindrome/class Solution { public boolean isPalindrome(String s) { for (int i=0;i<s.length();i++){ if (!iscode(s.charAt(i))){ s=s.replace(Character.toString(s.charAt(i)),原创 2020-09-15 15:09:12 · 75 阅读 · 0 评论 -
算法练习(46) leetcode 205. 同构字符串
https://leetcode-cn.com/problems/isomorphic-strings/class Solution { public boolean isIsomorphic(String s, String t) { if (test(s,t)&&test(t,s)){ return true; } else { return false; } }原创 2020-09-11 17:27:59 · 90 阅读 · 0 评论 -
算法练习(45) leetcode 203. 移除链表元素
https://leetcode-cn.com/problems/remove-linked-list-elements//** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode rem原创 2020-09-11 15:02:30 · 127 阅读 · 0 评论 -
算法练习(44) leetcode 202. 快乐数(HashMap)
https://leetcode-cn.com/problems/happy-number/class Solution { public boolean isHappy(int n) { Map<Integer,Integer> map=new HashMap<>(); while (next(n)!=1){ if (map.get(next(n))!=null){ return原创 2020-09-11 14:17:15 · 78 阅读 · 0 评论 -
算法练习(43) leetcode 204. 计数质数
https://leetcode-cn.com/problems/count-primes/class Solution { public int countPrimes(int n) { if (n<=2){ return 0; } List<Integer> list=new ArrayList<Integer>(); list.add(2); for (原创 2020-09-11 11:32:34 · 84 阅读 · 0 评论 -
算法练习(42) leetcode 216. 组合总和 III (复用算法练习(33) leetcode 77代码)
https://leetcode-cn.com/problems/combination-sum-iii/class Solution { public List<List<Integer>> combinationSum3(int k, int n) { List<List<Integer>> unans=combine(9,k); List<List<Integer>> ans=new原创 2020-09-11 10:23:44 · 86 阅读 · 0 评论 -
算法练习(41) leetcode 141. 环形链表(快慢指针)
https://leetcode-cn.com/problems/linked-list-cycle/public class Solution { public boolean hasCycle(ListNode head) { if (head == null || head.next == null) { return false; } ListNode step1=head,step2=step1.next; while原创 2020-09-10 17:23:20 · 74 阅读 · 0 评论 -
算法练习(40) leetcode 82. 删除排序链表中的重复元素 II
https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/class Solution { public ListNode deleteDuplicates(ListNode head) { boolean flag=true; ListNode ans=head,before=null; if (head==null||head.next==null){原创 2020-09-10 15:32:38 · 83 阅读 · 0 评论 -
算法练习(39) leetcode 83. 删除排序链表中的重复元素
https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list//** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListN原创 2020-09-09 17:04:22 · 75 阅读 · 0 评论 -
算法练习(38) leetcode 137. 只出现一次的数字 II(排序找规律法)
https://leetcode-cn.com/problems/single-number-ii/class Solution { public int singleNumber(int[] nums) { if(nums.length==1){ return nums[0]; } Arrays.sort(nums); if (nums[0]!=nums[1]){ return n原创 2020-09-08 16:16:53 · 96 阅读 · 0 评论 -
算法练习(37) leetcode 136. 只出现一次的数字(HashMap)
https://leetcode-cn.com/problems/single-number/class Solution { public int singleNumber(int[] nums) { Map<Integer,Integer> map=new HashMap<>(); for (int num : nums) { if (map.containsKey(num)){原创 2020-09-08 16:01:54 · 71 阅读 · 0 评论 -
算法练习(36) leetcode 461. 汉明距离
https://leetcode-cn.com/problems/hamming-distance/class Solution { public int hammingDistance(int x, int y) { String stringx=reverse(Integer.toBinaryString(x)); String stringy=reverse(Integer.toBinaryString(y)); int ans=0,min原创 2020-09-08 15:30:09 · 77 阅读 · 0 评论 -
算法练习(35) leetcode 88. 合并两个有序数组
https://leetcode-cn.com/problems/merge-sorted-array/class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int[] trannums1=new int[m]; for (int i=0;i<m;i++){ trannums1[i]=nums1[i]; }原创 2020-09-08 14:43:19 · 84 阅读 · 0 评论 -
算法练习(34) leetcode 73. 矩阵置零 (空间O(m+n))
https://leetcode-cn.com/problems/set-matrix-zeroes/class Solution { public void setZeroes(int[][] matrix) { boolean[] x=new boolean[matrix[0].length]; boolean[] y=new boolean[matrix.length]; for(int i=0;i<y.length;i++){原创 2020-09-08 14:02:38 · 90 阅读 · 0 评论 -
算法练习(33) leetcode 77. 组合 (二进制数模拟组合、替换法统计字符串某字符在字符串出现次数、字符串反转)
https://leetcode-cn.com/problems/combinations/class Solution { public static void main(String[] args) { new Solution().combine(3,1); } public List<List<Integer>> combine(int n, int k) { List<List<Integer>原创 2020-09-08 10:43:31 · 157 阅读 · 0 评论 -
算法练习(32) leetcode 53. 最大子序和 (dp动态规划)
https://leetcode-cn.com/problems/maximum-subarray/class Solution { public int maxSubArray(int[] nums) { int[] dp=new int[nums.length]; dp[0]=nums[0]; for (int i=1;i<dp.length;i++){ if (dp[i-1]+nums[i]>nums[i])原创 2020-09-07 16:03:39 · 80 阅读 · 0 评论 -
算法练习(31) leetcode 55. 跳跃游戏 (贪心算法)
https://leetcode-cn.com/problems/jump-game/class Solution { public boolean canJump(int[] nums) { if (nums.length==1){ return true; } if(nums[0]==0){ return false; } int max=nums[0];原创 2020-09-07 15:32:36 · 120 阅读 · 0 评论 -
算法练习(30) leetcode 6. Z 字形变换
https://leetcode-cn.com/problems/zigzag-conversion/class Solution { public String convert(String s, int numRows) { if (numRows<=1) { return s; } String[] strings=new String[numRows]; for (int i=0;i<n原创 2020-09-07 13:49:56 · 74 阅读 · 0 评论 -
算法练习(29) leetcode 5. 最长回文子串 (中心扩散、字符串处理)
https://leetcode-cn.com/problems/longest-palindromic-substring/class Solution {public String longestPalindrome(String s) { String string="#"; int max=1,c = 0,l=0; for(int i=0;i<s.length();i++) { string=string+Cha原创 2020-09-07 11:23:45 · 75 阅读 · 0 评论 -
算法练习(28) leetcode 347. 前 K 个高频元素 (HashMap按值排序返回key)
https://leetcode-cn.com/problems/top-k-frequent-elements/class Solution { public static void main(String[] args) { new Solution().topKFrequent(new int[]{1, 1, 1, 2, 2, 3},1); } public int[] topKFrequent(int[] nums, int k) { M原创 2020-09-07 10:16:09 · 123 阅读 · 0 评论 -
算法练习(27) leetcode 50. Pow(x, n)
https://leetcode-cn.com/problems/powx-n/class Solution { public double erfen(double x, long N) { if (N == 0) { return 1.0; } double y = erfen(x, N / 2); return N % 2 == 0 ? y * y : y * y * x; } pu原创 2020-09-04 14:16:11 · 95 阅读 · 0 评论 -
算法练习(26) leetcode 58. 最后一个单词的长度
https://leetcode-cn.com/problems/length-of-last-word/class Solution { public int lengthOfLastWord(String s) { if(s.length()==0){ return 0; } if (s.length()-1==s.indexOf(' ')){ return s.length()-1;原创 2020-09-04 14:02:47 · 62 阅读 · 0 评论 -
算法练习(25) leetcode 70. 爬楼梯
https://leetcode-cn.com/problems/climbing-stairs/class Solution {public int climbStairs(int n) { int x=1,y=2,t=0; if (n==1){ return 1; } if (n==2){ return 2; } for (int i=2;i<n;原创 2020-09-04 13:23:14 · 81 阅读 · 0 评论 -
算法练习(24) leetcode 38. 外观数列
https://leetcode-cn.com/problems/count-and-say/class Solution { public String countAndSay(int n) { if (n==1){ return "1"; } String string="1"; for(int i=1;i<n;i++){ string=say(string);原创 2020-09-04 09:54:54 · 82 阅读 · 0 评论 -
算法练习(23) leetcode 26. 删除排序数组中的重复项
https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/class Solution { public int removeDuplicates(int[] nums) { if(nums.length==0) { return 0; } int num=nums[0],location=1; for (int i=1;i&原创 2020-09-03 17:31:19 · 69 阅读 · 0 评论 -
算法练习(22) leetcode 28.实现strStr()
https://leetcode-cn.com/problems/implement-strstr/class Solution { public int strStr(String haystack, String needle) { if (needle.equals("") || needle==null||needle.equals(haystack)) { return 0 ; } boolean flag=false原创 2020-09-03 16:52:52 · 72 阅读 · 0 评论 -
算法练习(21) leetcode 27. 移除元素
https://leetcode-cn.com/problems/remove-element/class Solution { public int removeElement(int[] nums, int val) { int count = 0,length=nums.length; for(int i=0;i<length;i++){ if(nums[i]==val){ count++;原创 2020-09-01 17:16:44 · 83 阅读 · 0 评论 -
算法练习(20) leetcode 486. 预测赢家(dp动态规划)
https://leetcode-cn.com/problems/predict-the-winner/class Solution { public boolean PredictTheWinner(int[] nums) { if (nums.length%2==0){ return true; } int[][] dp=new int[nums.length][nums.length]; for (in原创 2020-09-01 15:43:38 · 138 阅读 · 0 评论 -
算法练习(19) leetcode 1528. 重新排列字符串
https://leetcode-cn.com/problems/shuffle-string/class Solution { public String restoreString(String s, int[] indices) { char[] chars=new char[indices.length]; for (int i=0;i<indices.length;i++) { chars[indices[i]]=s.ch原创 2020-09-01 13:41:42 · 83 阅读 · 0 评论 -
算法练习(18) leetcode 841. 钥匙和房间
https://leetcode-cn.com/problems/keys-and-rooms/class Solution { public boolean canVisitAllRooms(List<List<Integer>> rooms) { boolean[] flag=new boolean[rooms.size()]; flag[0]=true; Stack<Integer> stack=new原创 2020-09-01 13:39:55 · 118 阅读 · 0 评论 -
算法练习(17) leetcode 69. x 的平方根
https://leetcode-cn.com/problems/sqrtx/class Solution { public int mySqrt(int x) { long i; for ( i=0;i*i<=x;i++){ } return (int)(i-1); }}原创 2020-09-01 13:38:36 · 85 阅读 · 0 评论 -
算法练习(16) leetcode 67. 二进制求和
https://leetcode-cn.com/problems/add-binary/class Solution {public String addBinary(String a, String b) { a=new StringBuilder(a).reverse().toString(); b=new StringBuilder(b).reverse().toString(); boolean flag = false; St原创 2020-09-01 13:35:35 · 91 阅读 · 0 评论 -
算法练习(15) leetcode 66. 加一
https://leetcode-cn.com/problems/plus-one/class Solution { public int[] plusOne(int[] digits) { boolean flag=false; digits[digits.length-1]=digits[digits.length-1]+1; for (int i=digits.length-1;i>=0;i--){ if (d原创 2020-09-01 13:34:18 · 72 阅读 · 0 评论 -
算法练习(14) leetcode 657. 机器人能否返回原点
https://leetcode-cn.com/problems/robot-return-to-origin/class Solution { public boolean judgeCircle(String moves) { int x=0,y=0; for(int i=0;i<moves.length();i++) { switch (moves.charAt(i)){ case 'L' :原创 2020-09-01 10:47:04 · 74 阅读 · 0 评论 -
算法练习(13) leetcode 9. 回文数
https://leetcode-cn.com/problems/palindrome-number/class Solution { public boolean isPalindrome(int x) { int flag=x,first=0,a=0; if(x<0) { return false; }else if (x==0){ return true; }else {原创 2020-08-31 16:42:53 · 66 阅读 · 0 评论 -
算法练习(12) leetcode 8. 字符串转换整数 (atoi)
https://leetcode-cn.com/problems/string-to-integer-atoi/class Solution { public static void main(String[] args) { System.out.println(new Solution().myAtoi("- 234")); } public int myAtoi(String str) { int index; long n原创 2020-08-31 16:40:04 · 72 阅读 · 0 评论 -
算法练习(11) leetcode 7. 整数反转
https://leetcode-cn.com/problems/reverse-integer/class Solution {public static int reverse(int x) { long a = 0; int flag,first=0; if(x < 0){ x=-x; flag = -1; }else{ flag = 1;原创 2020-08-31 16:37:27 · 62 阅读 · 0 评论