第一题 两数之和,第二题 两数相加,第三题最长子串,第四题找中位数,第五题最长回文子串,第六题 Z字形变换,第七题整数反转 第八题 字符串转换整数

本文介绍了六个Java编程问题,涉及数组操作(两数之和、链表加法)、字符串处理(最长回文、字符串转行)、数组和链表搜索(最长无重复子串、有序数组合并找中位数)以及数值处理(整数反转和字符串转整数)。
摘要由CSDN通过智能技术生成

1

class Solution {
    public int[] twoSum(int[] nums, int target) {
        HashMap<Integer,Integer> map = new HashMap <>();
        for(int i = 0;i < nums.length;i++){
            int complement = target - nums[i];
            if(map.containsKey(complement)){
                return new int[]{map.get(complement),i};
            }
            map.put(nums[i],i);
        }
        return new int[0];
    }
}

 2

/**
 * 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 Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(0);
        ListNode curr = dummy;
        int carry = 0;
        while(l1!=null || l2!=null){
            int x = (l1!=null)?l1.val:0;
            int y = (l2!=null)?l2.val:0;
            int sum = x+y+carry;
            carry = sum/10;
            curr.next = new ListNode(sum % 10);
            curr = curr.next;
            if(l1!= null){
                l1 = l1.next;
            }
            if(l2 !=null){
                l2 = l2.next;
            }
        }
        if(carry > 0){
            curr.next = new ListNode(carry);
        }
        return dummy.next;
    }
}

3

public class Solution{
    public int lengthOfLongestSubstring(String s){
        HashSet<Character> set = new HashSet<>();
        int maxLength = 0;
        int left = 0;
        int right = 0;
        while(right < s.length()){
            char c = s.charAt(right);
            if(!set.contains(c)){
                set.add(c);
                maxLength = Math.max(maxLength,right - left + 1);
                right++;
            }else{
                set.remove(s.charAt(left));
                left++;
            }
        }
        return maxLength;
    }
}

4

class Solution {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        if (nums1.length > nums2.length) {
            return findMedianSortedArrays(nums2, nums1);
        }
        int m = nums1.length;
        int n = nums2.length;
        int left = 0;
        int right = m;
        while (left <= right) {
            int i = left + (right - left) / 2;  //开始二分查找,i是nums1的分割点 ,j是num2的分割点
            int j = (m + n + 1) / 2 - i;
            int maxLeft1 = (i == 0) ? Integer.MIN_VALUE : nums1[i - 1];
            int minRight1 = (i == m) ? Integer.MAX_VALUE : nums1[i];
            int maxLeft2 = (j == 0) ? Integer.MIN_VALUE : nums2[j - 1];
            int minRight2 = (j == n) ? Integer.MAX_VALUE : nums2[j];
            if (maxLeft1 <= minRight2 && maxLeft2 <= minRight1) {
                if ((m + n) % 2 == 0) {
                    return (Math.max(maxLeft1, maxLeft2) + Math.min(minRight1, minRight2)) / 2.0;
                } else {
                    return (double)Math.max(maxLeft1, maxLeft2);
                }
            } else if (maxLeft1 > minRight2) {
                right = i - 1;
            } else {
                left = i + 1;
            }
        }
        throw new IllegalArgumentException("Input arrays are not sorted.");
    }
}

5

class Solution {
    public String longestPalindrome(String s) {
        //中心扩展法
        if (s == null || s.length() < 1)
            return "0";
        int start = 0,end = 0;
        for (int i = 0;i < s.length();i++){
            int len1 = expandAroundCenter(s,i,i);
            int len2 = expandAroundCenter(s,i,i + 1);
            int len = Math.max(len1,len2);
            if (len > end - start){
                start = i - (len - 1) / 2;
                end = i + len / 2;
            }
        }
        return s.substring(start,end + 1);
    }
    private int expandAroundCenter(String s,int left,int right){
        while (left >=0 && right < s.length() && s.charAt(left) == s.charAt(right)){
            left--;
            right++;
        }
        return right - left -1;
    }
}

6

class Solution {
    public String convert(String s, int numRows) {
        if(numRows ==1){
            return s;
        }
        List<StringBuilder> rows = new ArrayList<>();
        for (int i = 0;i < Math.min(numRows,s.length());i++){
            rows.add(new StringBuilder());
        }
        int curRow = 0;
        boolean goingDown = false;
        for (char c : s.toCharArray()){
            rows.get(curRow).append(c);
            if (curRow == 0 || curRow == numRows -1){
                goingDown = !goingDown;
            }
            curRow += goingDown ? 1 :-1;
        }
        StringBuilder result = new StringBuilder();
        for (StringBuilder row : rows){
            result.append(row);
        }
        return result.toString();
    }
}

7

//整数反转
class Solution {
    public int reverse(int x) {
        int reversed = 0;
        while (x != 0){
            int digit = x % 10;
            x = x / 10;
            if (reversed > Integer.MAX_VALUE / 10 || (reversed == Integer.MAX_VALUE / 10 && digit > 7 )){
                return 0;
            }else if(reversed < Integer.MIN_VALUE / 10 ||(reversed == Integer.MIN_VALUE / 10 && digit < -8)){
                return 0;
            }
            reversed = reversed * 10 + digit;
        }
        return reversed;

    }
}
/**
条件 (reversed == Integer.MAX_VALUE / 10 && digit > 7) 中的 digit > 7 是针对反转后的数字的个位数进行判断的部分。
具体来说,由于 Integer.MAX_VALUE 是 32 位有符号整数的最大值,即 2^31 - 1,因此个位数的最大值应为 7。在反转一个整数的过程中,如果当前需要添加的个位数 digit 大于 7,那么在将其添加到反转的结果中时,如果 reversed 的值已经是 Integer.MAX_VALUE / 10,再加上大于 7 的个位数将会导致溢出,超出了 32 位整数的范围。
因此,条件(reversed == Integer.MAX_VALUE / 10 && digit > 7) 的含义是:如果当前 reversed 的值已经是 Integer.MAX_VALUE / 10,并且当前需要添加的个位数 digit 大于 7,那么就会发生溢出,函数应该返回 0。
 */

8

class Solution {
   public int myAtoi(String s) {
    int index = 0;
    int sign = 1;
    int result = 0;
    // 去除前导空格
    while (index < s.length() && s.charAt(index) == ' ') {
        index++;
    }
    // 确定正负号
    if (index < s.length() && (s.charAt(index) == '+' || s.charAt(index) == '-')) {
        sign = (s.charAt(index) == '-') ? -1 : 1;
        index++;
    }
    // 转换数字
    while (index < s.length() && Character.isDigit(s.charAt(index))) {
        int digit = s.charAt(index) - '0';
        // 检查是否会溢出
        if (result > Integer.MAX_VALUE / 10 || (result == Integer.MAX_VALUE / 10 && digit > 7)) {
            return (sign == 1) ? Integer.MAX_VALUE : Integer.MIN_VALUE;
        }
        result = result * 10 + digit;
        index++;
    }
    return result * sign;
    }
}

  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值