随笔分类 - 剑指Offer
一共就这么点题,刷透刷透刷透它!!!!
摘要:public ListNode getIntersectionNode(ListNode headA, ListNode headB) { if (headA == null || headB == null) { return null; } ListNode node1 = headA, nod
阅读全文
摘要:public int findNthDigit(int n) { int digit = 1; long start = 1; long count = 9; while (n > count) { // 1. n -= count; digit += 1; start *= 10; count =
阅读全文
摘要:public String minNumber(int[] nums) { String[] strs = new String[nums.length]; for(int i = 0; i < nums.length; i++) strs[i] = String.valueOf(nums[i]);
阅读全文
摘要:public int translateNum(int num) { //动态规划 if(num<10) return 1; String s = String.valueOf(num); int len = s.length(); int[] dp = new int[len+1]; dp[0]
阅读全文
摘要:public int maxValue(int[][] grid) { //动态规划 int m = grid.length; int n = grid[0].length; for(int i = 1;i<n;i++){ grid[0][i] = grid[0][i] + grid[0][i-1]
阅读全文
摘要:public int lengthOfLongestSubstring(String s) { //用哈希表存储 int len = s.length(); if(len<=1) return len; char[] c = s.toCharArray(); HashMap<Character,In
阅读全文
摘要:public int nthUglyNumber(int n) { int a = 0,b = 0,c = 0; int[] dp = new int[n]; dp[0] = 1; for(int i = 1;i<n;i++){ int n2 = dp[a]*2,n3 = dp[b]*3,n5 =
阅读全文
摘要:public int singleNumber(int[] nums) { int ones = 0, twos = 0; for(int num : nums){ ones = ones ^ num & ~twos; twos = twos ^ num & ~ones; } return ones
阅读全文
摘要:class Solution { public int[] singleNumbers(int[] nums) { Arrays.sort(nums); int i = 1,j = 0; int c = 0; ArrayList<Integer> list = new ArrayList<>();
阅读全文
摘要:class MaxQueue { Queue<Integer> queue; LinkedList<Integer> max; public MaxQueue() { queue = new LinkedList<>(); max = new LinkedList<>(); } public int
阅读全文
摘要:public int maxProfit(int[] prices) { int n = prices.length; if(n == 0) return 0; int[] x = new int[n]; x[0] = prices[0]; int max = -1; for(int i = 1;i
阅读全文
摘要:public int sumNums(int n) { boolean flag = n > 0 && (n+=sumNums(n-1))>0; return n; } public int sumNums(int n) { return IntStream.range(1,n+1).sum();
阅读全文
摘要:public boolean oneEditAway(String first, String second) { int m = first.length(); int n = second.length(); if(Math.abs(m-n)>1) return false; if(m == 0
阅读全文
摘要:public boolean canPermutePalindrome(String s) { char[] c = s.toCharArray(); int n = c.length; Map<Character,Integer> map = new HashMap<>(); for (char
阅读全文
摘要:public String replaceSpaces(String S, int length) { S = S.substring(0,length); char[] c = S.toCharArray(); StringBuilder sb = new StringBuilder(); for
阅读全文
摘要:public boolean CheckPermutation(String s1, String s2) { char[] c1 = s1.toCharArray(); char[] c2 = s2.toCharArray(); int n = c1.length; if(n!=c2.length
阅读全文
摘要:public boolean isUnique(String astr) { for(char x = 'a';x<='z';x++){ if(!(astr.indexOf(x)==astr.lastIndexOf(x))){ return false; } } return true; }
阅读全文
摘要:public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(root == null || root == p || root == q) return root; TreeNode left =
阅读全文
摘要:public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if((root.val-p.val)*(root.val-q.val)<=0) return root; if(p.val<root.val&
阅读全文
摘要:public int[] constructArr(int[] a) { int len = a.length; if(len == 0) return new int[0]; int[] b = new int[len]; return multiply(a,b,len); } private i
阅读全文