剑指Offer之Java算法习题精讲数组与字符串

题目一

 解法

class Solution {
    public int findLengthOfLCIS(int[] nums) {
        if(nums.length==1) return 1;
        int fast = 1;
        int tmp = 1;
        int max = Integer.MIN_VALUE;
        while(fast<nums.length){
            if(nums[fast]>nums[fast-1]){
                tmp++;
                max = Math.max(max,tmp);
            }else{
                max = Math.max(max,tmp);
                tmp = 1;
            }
            fast++;
        }
        return max;
    }
}

题目二

 解法

class Solution {
    public boolean validPalindrome(String s) {
        int left = 0;
        int right = s.length()-1;
        while(left<right){
            if(s.charAt(left)==s.charAt(right)){
                left++;
                right--;
            }else{
                String tmp = s.substring(left, right + 1);
                return validPalindrome(tmp,1,tmp.length()-1)||validPalindrome(tmp,0,tmp.length()-2);
            }
        }
        return true;
    }
    public boolean validPalindrome(String s, int low, int high) {
        for (int i = low, j = high; i < j; ++i, --j) {
            char c1 = s.charAt(i), c2 = s.charAt(j);
            if (c1 != c2) {
                return false;
            }
        }
        return true;
    }
}

题目三

 解法

class Solution {
    public double findMaxAverage(int[] nums, int k) {
        int w = nums.length-k;
        int max = Integer.MIN_VALUE;
        for(int i = 0;i<=w;i++){
            int res = 0;
            for(int j = 0;j<k;j++){
                res = nums[i+j]+res;
            }
            max = Math.max(max,res);
        }
        double ans = (double)max/k;
        return ans;
    }
}

题目四

解法

class Solution {
    public int findShortestSubArray(int[] nums) {
        int[] n = new int[50001];
        for(int i = 0;i<nums.length;i++){
            n[nums[i]]+=1;
        }
        int max = Integer.MIN_VALUE;
        ArrayList<Integer> list = new ArrayList<Integer>();
        for(int i = 0;i<n.length;i++){
            max = Math.max(n[i],max);
        }
        for(int i = 0;i<n.length;i++){
            if(n[i]==max){
                list.add(i);
            }
        }
        int min = Integer.MAX_VALUE;
        for(int i = 0;i<list.size();i++){
            int res = list.get(i);
            int left = 0;
            int right = nums.length-1;
            while(nums[left]!=res){
                left++;
            }
            while(nums[right]!=res){
                right--;
            }
            min = Math.min(min,right-left+1);
        }
        return min;
    }
}

到此这篇关于剑指Offer之Java算法习题精讲数组与字符串的文章就介绍到这了,更多相关Java 数组内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • 剑指Offer之Java算法习题精讲排列与N叉树

    题目一  解法 class Solution { LinkedList<List<Integer>> ans = new LinkedList<List<Integer>>(); public List<List<Integer>> permute(int[] nums) { LinkedList<Integer> list = new LinkedList<Integer>(); boolean[] bo =

  • 剑指Offer之Java算法习题精讲数组与列表的查找及字符串转换

    题目一 解法 class Solution { public String toLowerCase(String s) { StringBuilder sb = new StringBuilder(); for(int i = 0;i<s.length();i++){ char ch = s.charAt(i); if('A'<=ch&&ch<='Z'){ ch = (char)(ch+32); } sb.append(ch); } return sb.toString(

  • 剑指Offer之Java算法习题精讲二叉树专项解析

    题目一 解法 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * t

  • 剑指Offer之Java算法习题精讲字符串操作与数组及二叉搜索树

    题目一  解法 class Solution { public String reverseOnlyLetters(String s) { char[] chars = s.toCharArray(); int left = 0; int right = chars.length-1; while(left<=right){ char tmp = 0; if(chars[left]>='a'&&chars[left]<='z'||(chars[left]>='A'&

  • 剑指Offer之Java算法习题精讲二叉搜索树与数组查找

    题目一  解法 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; *

  • 剑指Offer之Java算法习题精讲数组与字符和等差数列

    题目一  解法 class Solution { public int[] relativeSortArray(int[] arr1, int[] arr2) { int[] arr = new int[1001]; int[] ans = new int[arr1.length]; int index = 0; for(int i =0;i<arr1.length;i++){ arr[arr1[i]]+=1; } for(int i = 0;i<arr2.length;i++){ while

  • 剑指Offer之Java算法习题精讲求和篇

    题目一   解法 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solu

  • 剑指Offer之Java算法习题精讲二叉树与链表

    题目一  解法 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public boolean isBalanced(TreeNode root) { if(root==null){ return true; }e

  • 剑指Offer之Java算法习题精讲数组与字符串

    题目一  解法 class Solution { public int findLengthOfLCIS(int[] nums) { if(nums.length==1) return 1; int fast = 1; int tmp = 1; int max = Integer.MIN_VALUE; while(fast<nums.length){ if(nums[fast]>nums[fast-1]){ tmp++; max = Math.max(max,tmp); }else{ max

  • 剑指Offer之Java算法习题精讲数组与字符串题

    题目一 解法 class Solution { public int thirdMax(int[] nums) { Arrays.sort(nums); if(nums.length<3){ return nums[nums.length-1]; } int p = 1; for(int i =nums.length-2;i>=0;i--){ if(nums[i]==nums[i+1]){ }else{ ++p; if(p==3){ return nums[i]; } } } return n

  • 剑指Offer之Java算法习题精讲数组与二叉树

    题目一  解法 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; *

  • 剑指Offer之Java算法习题精讲链表与字符串及数组

    题目一 解法 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Soluti

  • 剑指Offer之Java算法习题精讲数组查找与字符串交集

    题目一 数组题--二分查找法 写一个函数查找给定的数组中指定的数值 具体题目如下  解法 class Solution { public int search(int[] nums, int target) { int left = 0; int right = nums.length - 1; while(left<=right){ int mid = left+(right-left)/2; if(nums[mid]==target){ return mid; }else if(nums[m

  • 剑指Offer之Java算法习题精讲链表专题篇

    题目一  解法 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solut

  • 剑指Offer之Java算法习题精讲二叉树与斐波那契函数

    题目一 解法 class Solution { public int fib(int n) { int[] arr = new int[31]; arr[0] = 0; arr[1] = 1; for(int i = 2;i<=n;i++){ arr[i] = arr[i-2]+arr[i-1]; } return arr[n]; } } 题目二  解法 /** * Definition for a binary tree node. * public class TreeNode { * in

  • 剑指Offer之Java算法习题精讲二叉树与N叉树

    题目一  解法 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; *

随机推荐