My algorithm road, plan to spend a year, brush 2000 questions

The list

The tree

An array of

1 the most large

Given a set of non-negative integer nums, rearrange the order of each number (each number is not separable) to form the largest integer.

Note: The output can be very large, so you need to return a string instead of an integer.

Leetcode-cn.com/problems/la…

public String largestNumber(int[] nums) { if (nums == null || nums.length == 0) { return ""; } String[] strAtr = new String[nums.length]; for (int i = 0; i < strAtr.length; i++) { strAtr[i] = String.valueOf(nums[i]); } Arrays.sort(strAtr, new Comparator<String>() { @Override public int compare(String o1, String o2) { return (o2 + o1).compareTo(o1 + o2); }}); StringBuilder sb = new StringBuilder(); for (String aStrArr : strAtr) { sb.append(aStrArr); } String result = sb.toString(); if ('0' == result.charAt(0)) { result = "0"; } return result; }}Copy the code

How many numbers of 2 are less than the current number

Given an array nums, for each element nums[I], count all the numbers in the array smaller than it.

In other words, for each nums[I] you must calculate the number of valid js, where j satisfies j! = I and nums[j] < nums[I]

Returns the answer as an array.

Example 1:

Input: nums = [8,1,2,2,3] output: [4,0,1,1,3] explanation: there are four numbers smaller than nums[0]=8: (1,2,2, and 3). There is no number smaller than nums[1]=1. There exists a number smaller than nums[2]=2 :(1). There exists a number smaller than nums[3]=2 :(1). There exist three numbers smaller than nums[4]=3 :(1,2 and 2).

Source: LeetCode

Link: leetcode-cn.com/problems/ho…

class Solution { public int[] smallerNumbersThanCurrent(int[] nums) { int n=nums.length; int []res=new int[n]; for(int i=0; i<n; i++) { for(int j=0; j<n; j++) { if(i==j)continue; if(nums[j]<nums[i]) { res[i]++; } } } return res; }}Copy the code

3 The longest substring without repeating characters

Find the longest substring from the string that does not contain repeated characters, and calculate the length of the oldest string.

 

Example 1:

Input: “abcabcbb” Output: 3 Explanation: Since the oldest string without repeating characters is “ABC”, its length is 3.

Source: LeetCode

Link: leetcode-cn.com/problems/zu…

 public int lengthOfLongestSubstring(String s) {
        if (s.length() <= 0) {
            return 0;
        }
        int left = 0;
        int right = 0;
        int length = 0;

        HashSet<Character> hashSet = new HashSet<>();
        while (right < s.length()) {
            if (hashSet.contains(s.charAt(right))) {
                hashSet.remove(s.charAt(left));
                left++;
            } else {
                hashSet.add(s.charAt(right));
                right++;
            }
            length = hashSet.size() > length ? hashSet.size() : length;
        }
        return length;
    }
Copy the code

4 The oldest string without repeating characters

Given a string, find the length of the smallest string that does not contain repeating characters.

 

Example 1:

Input: s = “abcabcbb” Output: 3 Explanation: Since the oldest string without repeating characters is “ABC”, its length is 3. Example 2:

Input: s = “BBBBB” Output: 1 Explanation: Since the oldest string without repeating characters is “b”, its length is 1.

Source: LeetCode

Link: leetcode-cn.com/problems/lo…

 public int lengthOfLongestSubstring(String args) {
         if (args.length() <= 0) {
            return 0;
        }

        int left = 0;
        int right = 0;
        int length = 0;
        HashSet<Character> hashSet = new HashSet<>();
        while (right < args.length()) {
            if (hashSet.contains(args.charAt(right))) {
                hashSet.remove(args.charAt(left++));
            } else {
                hashSet.add(args.charAt(right++));
            }
            length = hashSet.size() > length ? hashSet.size() : length;
        }
        return length;
    }
Copy the code

5 The longest public prefix

Write a function to find the longest public prefix in an array of strings.

Returns the empty string “” if no public prefix exists.

Example 1:

STRS = [“flower”,”flow”,”flight”] Output: “fl” Example 2:

Input: STRS = [“dog”,”racecar”,”car”] Output: “” Explanation: The input does not have a common prefix.

Source: LeetCode

Link: leetcode-cn.com/problems/lo…

public String longestCommonPrefix(String[] strs) { if (strs.length == 0) { return ""; } String s = strs[0]; for (String str : strs) { while (! str.startsWith(s)) { s = s.substring(0, s.length() - 1); } } return s; }Copy the code

6 Delete all adjacent duplicates in the string

Given a string S consisting of lowercase letters, the deduplication operation selects two adjacent and identical letters and deletes them.

The deduplication operation is repeated on S until the deletion cannot continue.

Returns the final string after all deduplication operations have been completed. The answer is guaranteed to be unique.

 

Example:

Input: “abbaca” Output: “ca” Explanation: For example, in “abbaca”, we can delete “bb” because the two letters are adjacent and identical, this is the only duplicate item that can be deleted at this time. Then we get the string “aaca”, where only “aa” can be deduplicated, so the final string is “ca”.

Source: LeetCode

Link: leetcode-cn.com/problems/re…

 public String removeDuplicates(String s) {
        if (s.length() <=0){
            return s;
        }
        return removeHelper(new StringBuilder(s)).toString();
    }

    static StringBuilder removeHelper(StringBuilder sb) {
        for (int i = 0; i + 1 < sb.length(); i++) {
            if (sb.charAt(i) == sb.charAt(i + 1)) {
                sb.delete(i, i + 2);
                return removeHelper(sb);
            }
        }
        return sb;
    }
Copy the code

7 Reverse string

Write a function that reverses the input string. The input string is given in the form of the character array char[].

Instead of allocating extra space to another array, you must modify the input array in place, using O(1) extra space to solve the problem.

You can assume that all the characters in the array are printable characters in the ASCII code table.

By LeetCode

Link: leetcode-cn.com/leetbook/re…

class Solution { public void reverseString(char[] s) { if(s==null || s.length <=0){ return; } int start = 0; int end = s.length-1; while (start < end) { char temp = s[start]; s[start++] = s[end]; s[end--] = temp; }}}Copy the code

8 Integer inversion

You are given a 32-bit signed integer x that returns the result of reversing the numeric portion of x.

If the inverted integer exceeds the range of 32-bit signed integers [−231, 231 − 1], 0 is returned.

Assume that the environment does not allow storage of 64-bit integers (signed or unsigned).

By LeetCode

Link: leetcode-cn.com/leetbook/re…

class Solution { public int reverse(int x) { int res = 0; while (x ! = 0) { int t = x % 10; int newRes = res * 10 + t; if ((newRes - t) / 10 ! = res) { return 0; } res = newRes; x = x / 10; } return res; }}Copy the code
class Solution {
    public int reverse(int x) {
        long result = 0;
        while (x!=0){
            result = result*10+x%10;
            x = x/10;
        }
        if(result<Integer.MIN_VALUE || result> Integer.MAX_VALUE){
            return 0;
        }
        return (int)result;
    }
}
Copy the code

9 The first unique character in a string

Given a string, find its first non-repeating character and return its index. If none exists, -1 is returned.

Example:

S = “leetcode” returns 0

S = “loveleetcode” returns 2

Tip: You can assume that the string contains only lowercase letters.

By LeetCode

Link: leetcode-cn.com/leetbook/re…

class Solution { public int firstUniqChar(String s) { if (s == null || s.length() == 0) { return -1; } HashMap<Character, Integer> map = new HashMap<>(); char[] chars = s.toCharArray(); for (Character ch : chars) { map.put(ch, map.getOrDefault(ch, 0) + 1); } for (int i = 0; i < s.length(); i++) { if (map.get(s.charAt(i)) == 1) { return i; } } return -1; } } class Solution { public int firstUniqChar(String s) { if (s == null || s.length() == 0) { return -1; } for (int i = 0; i < s.length(); i++) { if (s.indexOf(s.charAt(i)) == s.lastIndexOf(s.charAt(i))) { return i; } } return -1; }}Copy the code

10 valid letter heterotopic words

Given two strings s and t, write a function to determine whether t is an alphabetic allotopic of S.

Example 1:

Enter: s = “anagram”, t = “nagaram”

Input: s = “rat”, t = “car” Output: false Note: You can assume that the string contains only lowercase letters.

By LeetCode

Link: leetcode-cn.com/leetbook/re…

public static boolean isAnagram(String s, String t) { if (s.length() ! = t.length()) { return false; } int[] letterCount = new int[26]; For (int I = 0; i < s.length(); i++) { letterCount[s.charAt(i) - 'a']++; } // Subtract the number of each character in string t for (int I = 0; i < t.length(); i++) { letterCount[t.charAt(i) - 'a']--; } // Return true if each value of the array letterCount is 0, otherwise return false for (int count: letterCount) {if (count! = 0) { return false; } } return true; } public Boolean isAnagram(String s, String t) {if (s.length()! =t.length()) return false; Char [] array1 = s.tochararray (); char[] array2 = t.toCharArray(); Arrays.sort(array1); Arrays.sort(array2); return Arrays.equals(array1,array2); }Copy the code

11 palindrome

** gives you an integer x, and returns true if x is a palindrome integer; Otherwise, return false.

Palindromes are integers that read in positive (left to right) and backward (right to left) order. For example, 121 is palindrome and 123 is not.

 

Example 1:

Input: x = 121 Output: true

Source: LeetCode

Link: leetcode-cn.com/problems/pa…

class Solution {
    public boolean isPalindrome(int x) {
        if(x<0 || (x % 10 == 0 && x!=0)){
            return false;
        }
        int revertNumber = 0;
        while(x>revertNumber){
            revertNumber = revertNumber*10+ x%10;
            x /= 10;
        }
        return x == revertNumber || x == revertNumber/10;
    }
}
Copy the code

12 valid parentheses

Specify a string s containing only ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘, ‘]’.

A valid string must meet the following requirements:

An open parenthesis must be closed with a close parenthesis of the same type. The left parentheses must be closed in the correct order.

Example 1:

Input: s = “()” output: true

Source: LeetCode

Link: leetcode-cn.com/problems/va…

class Solution { public boolean isValid(String s) { if (s == null || s.isEmpty() || (s.length() % 2 ! = 0)) { return false; } char[] chars = s.toCharArray(); Stack<Character> stack = new Stack<>(); for (char c : chars) { if (c == '(') { stack.push(')'); } else if (c == '[') { stack.push(']'); } else if (c == '{') { stack.push('}'); } else if (stack.isEmpty() || c ! = stack.pop()) { return false; } } return stack.isEmpty(); }}Copy the code