LeetCode algorithm, one question a day, impact Alibaba, day6

1. LeetCode 228. Summary interval

topic

Given an ordered integer array nums with no repeating elements.
Returns a list of the smallest ordered interval ranges that exactly cover all the numbers in the array. That is, every element of nums is exactly covered by some interval range, and there is no number x that belongs to some range but not to nums.
Input: nums = [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]

Xiaobian cooking solution

public static List<String> summaryRanges(int[] nums) {
    
    
    int step = -1;
    int next = 0;
    int over = 0;
    int curren = 0;
    List<String> list = new ArrayList<>();
    for(int i = 0;i<nums.length;i++){
    
    
        int c = nums[i];
        if (i<=curren + step){
    
    
            continue;
        }
        curren = i;
        String temp = "";
        step = 0;
        if(i+1+step == nums.length){
    
    
            list.add(String.valueOf(c));
            break;
        }
        next = nums[i+1+step];
        while (next - c == 1+step){
    
    
            over = next;
            step++;
            if(i+1+step < nums.length){
    
    
                next = nums[i+1+step];
            }
        }
        if (step == 0){
    
    
            temp = String.valueOf(c);
        }else{
    
    
            temp += c + "->"+over;
        }
        list.add(temp);
    }
    return list;
}

Enough trouble, dishes.

Big boss pointing the country

public static List<String> summaryRanges2(int[] nums) {
    
    
    List<String> ret = new ArrayList<String>();
    int i = 0;
    int n = nums.length;
    while (i < n) {
    
    
        int low = i;
        i++;
        while (i < n && nums[i] == nums[i - 1] + 1) {
    
    
            i++;
        }
        int high = i - 1;
        StringBuffer temp = new StringBuffer(Integer.toString(nums[low]));
        if (low < high) {
    
    
            temp.append("->");
            temp.append(Integer.toString(nums[high]));
        }
        ret.add(temp.toString());
    }
    return ret;
}

2. Power of LeetCode 231.2

topic

Given an integer n, please judge whether the integer is a power of 2. If so, return true; otherwise, return false.
If there is an integer x such that n == 2x, n is considered to be a power of 2.

Xiaobian cooking solution

public static boolean isPowerOfTwo(int n) {
    
    
    if (n == 1){
    
    
        return true;
    }
    while (true){
    
    
        if (n/2 >1 && n%2==0){
    
    
            n = n/2;
        }else{
    
    
            if (n == 2){
    
    
                return true;
            }else{
    
    
                return false;
            }
        }
    }
}

Thought analysis

A number nn is a power of 22 if and only if nn is a positive integer and the binary representation of nn contains only 11 11s.
Therefore, we can consider using bit operation to extract the lowest 11 in the binary representation of nn, and then judge whether the remaining value is 00. n & (n - 1)
where \texttt{&}& means bitwise AND operation. This bit operation trick can directly remove the lowest bit 11 of the binary representation of nn.

Big boss pointing the country

public boolean isPowerOfTwo(int n) {
    
    
    return n > 0 && (n & (n - 1)) == 0;
}

Binary is not very good at playing, ashamed ah.

3. LeetCode 205. Valid letter anagrams

topic

Given two strings s and t, write a function to determine whether t is an anagram of s.
Note: If each character in s and t appears the same number of times, then s and t are called anagrams of each other.

Xiaobian problem solving ideas

Take out the number of all letters and put them into the map. If the two strings are anagrams, the maps must be equal.

Xiaobian cooking solution

public static boolean isAnagram(String s, String t) {
    
    
    Map<Character,Integer> map = new HashMap<>();
    for (int i = 0; i < s.length(); i++) {
    
    
        char c = s.charAt(i);
        if(map.containsKey(c)){
    
    
            map.put(c,map.get(c)+1);
        }else{
    
    
            map.put(c,1);
        }
    }
 
    Map<Character,Integer> map2 = new HashMap<>();
    for (int i = 0; i < t.length(); i++) {
    
    
        char c = t.charAt(i);
        if(map2.containsKey(c)){
    
    
            map2.put(c,map2.get(c)+1);
        }else{
    
    
            map2.put(c,1);
        }
    }
    return map.equals(map2);
}

Thought analysis

t is an anagram of ss equivalent to "two strings sorted equal". Therefore, we can sort the strings ss and tt respectively, and judge whether the sorted strings are equal. Furthermore, if ss and tt are of different lengths, tt must not be an anagram of ss.

Big boss pointing the country

public static boolean isAnagram(String s, String t) {
    
    
    char[] c1 = s.toCharArray();
    char[] c2 = t.toCharArray();
    Arrays.sort(c1);
    Arrays.sort(c2);
    return Arrays.equals(c1,c2);
}

为什么80%的码农做不了架构师?>>>

Java Column Directory | Click Here

insert image description here

4. Follow the public account Nezha programming, reply 1024, get mind maps, and irregular book delivery activities

  1. Java from entry to project combat (full video version)
  2. Introduction and practice of NoSQL database (based on MongoDB, Redis)
  3. Node+MongoDB+React project development
  4. Easy to learn Vue.js 3.0 from entry to actual combat (case, video, color version)
    insert image description here

Pay attention to the public number: Nezha programming

Nezha programming updates high-quality articles every week. After paying attention, reply to [CSDN] to receive Java mind maps, Java learning materials, and massive interview materials.

 

Add me WeChat: 18525351592

Pull you into the technical exchange group, there are many technical bigwigs in the group, exchange technology together, advance together, enter the big factory together, and you can buy technical books for free~~

Guess you like

Origin blog.csdn.net/guorui_java/article/details/124186857