[java][LeetCode]最长回文子串,最大面积,在旋转排序数组中找目标值

102 二叉树层序遍历 迭代

队列的dfs经典问题

class Solution(object):
    def levelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        if not root:
            return []
        res, cur_level = [], [root]
        while cur_level:
            next_level, tmp_res = [], []
            for node in cur_level:
                tmp_res.append(node.val)
                if node.left:
                    next_level.append(node.left)
                if node.right:
                    next_level.append(node.right)
            res.append(tmp_res)
            cur_level = next_level
        return res

34,在排序数组中找到第一个和最后一个位置

Example 1:

Input: nums = [5,7,7,8,8,10], target = 8

Output: [3,4]

Example 2:

Input: nums = [5,7,7,8,8,10], target = 6

Output: [-1,-1]

双指针发(可行):双指针left和right分别指向数组的起始,即left = 0,right = nums.length - 1,之后,先从左向右遍历,找到第一个nums[i] = target,left = i, 此时left为左边界;接着再从右向左遍历,找到第一个nums[i] = target,right = i,此时right为右边界。

class Solution(object):
    def searchRange(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        
        """
        arr=[-1,-1]
        if not nums:
            return arr
        left=0
        right=len(nums)-1
        while left<=right:
            if nums[left]==target:
                arr[0]=left
                break
            else:
                left+=1
        while left<=right:
            if nums[right]==target:
                arr[1]=right
                break
            else:
                right-=1
        return arr

33,在旋转排序数组中找目标值

Example 1:

Input: nums = [4,5,6,7,0,1,2], target = 0

Output: 4

Example 2:

Input: nums = [4,5,6,7,0,1,2], target = 3

Output: -1

二分法

#coding:utf8
class Solution(object):
    def search(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        if not nums:
            return -1
        l=len(nums)
        left=0
        right=l-1
        while left<=right:
            m=(left+right)/2
            if nums[m]==target:
                return m
            if nums[m]nums[m] and target<=nums[right]:
                    left=m+1
                else:
                    right=m-1
            else:  #此时left到m升序
                if target=nums[left]:
                    right=m-1
                else:
                    left=m+1
        return -1

最长回文子串

Example 1:

Input: "babad"

Output: "bab"

Note: "aba" is also a valid answer.

Example 2:

Input: "cbbd"

Output: "bb"

class Solution {
    public String longestPalindrome(String s) {

        if(s.length()==1)return s;
        int max=0;
        String res=new String();
        for (int i=0;imax){
                max=temp.length();
                res=temp;
            }
            if(i !=s.length()-1){
                L=i;R=i+1;//abba
                temp=getPlength(s,L,R);
                if (temp.length()>max){
                    max=temp.length();
                    res=temp;
                }
            }
        }
        return res;
    }
    private String getPlength(String s,int L,int R){
        while (L>=0 && R

11,容器的最大面积

/*用两个指针从两端开始向中间靠拢,如果左端线段短于右端,那么左端右移,反之右端左移,知道左右两端移到中间重合,记录这个过程中每一次组成木桶的容积,返回其中最大的。当左端线段L小于右端线段R时,我们把L右移,这时舍弃的是L与右端其他线段(R-1, R-2, ...)组成的木桶,这些木桶是没必要判断的,因为这些木桶的容积肯定都没有L和R组成的木桶容积大。*/

import java.util.*;
class Solution {
    public int maxArea(int[] height) {
        int max=0;
        int len=height.length;
        int left=0;
        int right=len-1;
        while (left

 

 

你可能感兴趣的:(Java)