import java.util.HashMap;
import java.util.HashSet;

/**
 * @author zhangjunwei
 * @version 0.0.1
 * @ClassName ZjwTets
 * @createDate 2021/12/1 10:36
 * @updateDate 2021/12/1 10:36
 */
public class ZjwTest {

    public static void main(String[] args){
        try{
            System.out.println(Integer.parseInt("123.0"));
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public int[] twoSum(int[] nums, int target) {
        for(int i = 0;i<nums.length-1;i++){
            for(int k = i+1;k<nums.length;k++){
                if(((nums[i]+nums[k])==target)){
                    return new int[]{i,k};
                }
            }
        }
        return new int[0];
    }

    public int[] twoSum1(int[] nums, int target) {
        HashMap<Integer,Integer> hashMap = new HashMap<>(nums.length);
        for(int i = 0;i<nums.length;i++){
            hashMap.put(nums[i],i);
        }
        for(int i = 0;i<nums.length-1;i++){
            if(hashMap.containsKey(target-nums[i]) && hashMap.get(target-nums[i])!=i){
                return new int[]{i,hashMap.get(target-nums[i])};
            }
        }
        return new int[0];
    }

    
    //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; }
    }
    
    
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode result = new ListNode();
        addTwoNumbers1(0,l1,l2,result);
        return result;
    }

    private ListNode addTwoNumbers1(int needAdd, ListNode l1, ListNode l2, ListNode result) {
        if(null != l1 || null != l2 || needAdd ==1){
            int sum = (null!= l1?l1.val:0) + (null!= l2?l2.val:0) + needAdd;
            if(sum>=10){
                result.val = sum-10;
                needAdd = 1;
            }else {
                result.val = sum;
                needAdd = 0;
            }
            result.next = addTwoNumbers1(needAdd, (null!= l1?l1.next:null), (null!= l2?l2.next:null), new ListNode());
            return result;
        }
        return null;
    }

    /**
     * 给定一个字符串 s ，请你找出其中不含有重复字符的 最长子串 的长度。
     * @param s
     * @return 长度
     */
    public static int lengthOfLongestSubstring1(String s) {
        if(s.length()==0){
            return 0;
        }
        char[] chars = s.toCharArray();
        HashSet<Character> characterHashSet = new HashSet<>();
        HashMap<Integer,Character> hashMap = new HashMap<>(s.length());
        int i = 0;
        for (char aChar : chars) {
            characterHashSet.add(aChar);
            hashMap.put(i,aChar);
            i++;
        }
        int maxLength = characterHashSet.size();
        if(maxLength == s.length()){

            return maxLength;
        }


        for (int length = maxLength; length > 1; length--) {
            for (i = 0; i <= s.length()-length; i++) {
                String s1 = s.substring(i,i+length);
                HashSet<Character> characterHashSet1 = new HashSet<>();
                for (char aChar : s1.toCharArray()) {
                    characterHashSet1.add(aChar);
                }
                if (characterHashSet1.size() == length){
                    return length;
                }
            }
        }
        return 1;
    }

    /**
     * 滑动窗口算法
     * @param s
     * @return
     */
    public static int lengthOfLongestSubstring(String s) {
        if(s.length()==0){
            return 0;
        }
        HashMap<Character,Integer> hashMap = new HashMap<>();
        int max = 0;
        int left = 0;
        for (int i = 0; i < s.length(); i++) {
            if(hashMap.containsKey(s.charAt(i))){
                left = Math.max(left,hashMap.get(s.charAt(i))+1);
            }
            hashMap.put(s.charAt(i),i);
            max = Math.max(max,i-left+1);
        }
        return max;
    }

    /**
     * 4. 寻找两个正序数组的中位数
     * 给定两个大小分别为 m 和 n 的正序（从小到大）数组 nums1 和 nums2。请你找出并返回这两个正序数组的 中位数 。
     * 算法的时间复杂度应该为 O(log (m+n)) 。
     */
    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int length1 = nums1.length;
        int length2 = nums2.length;
        int getOne = 0;
        int getTwo = 0;
        if((length1+length2)%2 == 0){
            getOne = (length1+length2)/2-1;
            getTwo = getOne+1;
        }else {
            getOne = (length1+length2)/2;
            getTwo =(length1+length2)/2;
        }
        if(length1== 0 && length2 == 0){
            return 0D;
        }
        if(length1 == 0){
            return (double) (nums2[getOne]+nums2[getTwo])/2;
        }
        if(length2 == 0){
            return (double) (nums1[getOne]+nums1[getTwo])/2;
        }


        int[] result = new int[length1+length2];

        int i = 0;
        int j = 0;
        for (int i1 = 0; i1 < (length1 + length2); i1++) {
            if (i< nums1.length && j< nums2.length){
                int a = nums1[i];
                int b = nums2[j];
                if(a>=b){
                    result[i1] = b;
                    j++;
                }else {
                    result[i1] = a;
                    i++;
                }
            }else if(i< nums1.length && j== nums2.length){
                result[i1] = nums1[i];
                i++;
            }else if(i == nums1.length && j< nums2.length){
                result[i1] = nums2[j];
                j++;
            }
            if(i1>=(getTwo+1)){
                break;
            }
        }

        return (double)(result[getOne]+result[getTwo])/2;
    }


    /**
     * 5. 最长回文子串
     * 给你一个字符串 s，找到 s 中最长的回文子串。
     * @param s
     * @return
     */
    public String longestPalindrome(String s) {
        if (s.length()<=1){
            return s;
        }
        String result = s.substring(0,1);
        HashMap<Character,Integer> map = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            if(map.containsKey(s.charAt(i))){
                //if((i+1- map.get(s.charAt(i)) >=result.length()))

            }else {
                map.put(s.charAt(i),i);
            }
        }
        return result;
    }




}

