[LeetCode-20220111] My problem solution record


Foreword

        Winter vacation comes quickly. It's time to train to coding.  I hope the number of my solved problem can reach 200 before Spring Festival. So i must be solve much more problem than before in one day. Just a artical written by a rookie, there may be some mistakes, It's welcome to correct. 

Notice: All the artical is write by Flex, myself. All rights reserved. Please contact me if reproduced.


Longest Substring Without Repeating Characters

Problem :Find the longest substring without repeating characters in a given string,then return the length of it.

Solution : To solve this problem,we can use a Queue store every new character which is not in queue in each loop.We can also delete character one by one until the repeat character be deleted when we add a repeat character in the queue.

Code :

class Solution {
    public int lengthOfLongestSubstring(String s) {
        StringBuffer res = new StringBuffer();
        Set<Character> set = new HashSet<>();
        int max = 0;
        for(int i = 0;i < s.length();i += 1) {
            if(set.contains(s.charAt(i))) {
                int idx = 0;
                while(res.charAt(idx) != s.charAt(i)) {
                    if(res.charAt(idx) != s.charAt(i))
                        set.remove(res.charAt(idx));
                    idx += 1;
                }
                idx += 1;
                res.delete(0,idx);
                res.append(s.charAt(i));
            } else {
                res.append(s.charAt(i));
                set.add(s.charAt(i));
            }
            if(res.length() > max) {
                max = res.length();
            }
        }
        return max;
    }
}

​​​​​​Median of Two Sorted Arrays

Problem : Return the median of two given sorted arrays

Solution : Defind a new list and push the elements of two given arrays in.Don't broke the state of sorted,and find median of the list,then return.

Code :

class Solution {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        ArrayList<Integer> arr = new ArrayList<>();
        int idx1 = 0,idx2 = 0;
        while(idx1 < nums1.length || idx2 < nums2.length) {
            if(idx1 < nums1.length && idx2 < nums2.length) { 
                if(nums1[idx1] < nums2[idx2]) {
                    arr.add(nums1[idx1]);
                    idx1 += 1;
                } else {
                    arr.add(nums2[idx2]);
                    idx2 += 1;
                }
            } else if(idx1 < nums1.length && idx2 >= nums2.length) {
                arr.add(nums1[idx1]);
                idx1 += 1;
            } else if(idx1 >= nums1.length && idx2 < nums2.length) {
                arr.add(nums2[idx2]);
                idx2 += 1;
            }
        }
        if(arr.size() % 2 == 0) {
            return (double)(((double)arr.get(arr.size() / 2) + (double)arr.get(arr.size() / 2 - 1)) / 2);
        } else {
            return (double)arr.get(arr.size() / 2);
        }
    }
}



Reverse Integer

Problem : Given a signed 32-bit integer x ,return x with its digits reversed. specially,return 0 when the value over the range [2 ^ -31, 2 ^ 31]

Solution : get all digits and store it in a list in reverse order, then get every digit from the list and build a integer. Pay attention, don't forget judge whether the value is overflow or not.

Code :

class Solution {
    public int reverse(int x) {
        if(x > -10 && x < 10) return x; // return x if x have only one digit
        boolean isNegative = (x < 0); // 1
        if(x <= -2147483648) return 0;
        if(isNegative) x = -x; // 2
        // the purpose of '1' and '2' step is convert the negative number to positive number 
        ArrayList<Integer> digits = new ArrayList<>();
        int res = 0;
        boolean isOverflow = false;
        while(x >= 10) {
            digits.add(x % 10);
            x /= 10;
        }
        digits.add(x);
        for(Integer i : digits) {
            int tmp = res;
            // res = 10;
            if(res * 10 / 10 != tmp) {
                isOverflow = true;
                break;
            } else {
                res *= 10;
                res += i;
            }
        }
        if(isOverflow) return 0;
        if(isNegative) return -res;
        return res;
    }
}

Palindrome Number

Problem : Given a integer x, judge and return the result whether x is a palindrome number or not.

Solution : First, judge whether x is a negative number. Negative must be not a palindrome. Then store the number in a list, and set two index, one points the head of list and theother points the rear of list.This two indexes will be close each other, until they point the same location.In this process,The loop will stop if two indexex point diffient numbers.

Code :

class Solution {
    public boolean isPalindrome(int x) {
        if(x < 0) return false; // Negative number must be not a palindrome number
        if(x < 10) return true;
        ArrayList<Integer> arr = new ArrayList<>();
        while(x >= 10) {
            arr.add(x % 10);
            x /= 10;
        }
        arr.add(x);
        int i = 0,j = arr.size() - 1;
        while(i < j) {
            if(arr.get(i) != arr.get(j))
                return false;
            i += 1;
            j -= 1;
        }
        return true;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值