日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

LeetCode刷题

發布時間:2023/12/15 综合教程 34 生活家
生活随笔 收集整理的這篇文章主要介紹了 LeetCode刷题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.字符串最后一個單詞的長度

計算字符串最后一個單詞的長度,單詞以空格隔開。

輸入描述:

一行字符串,非空,長度小于5000。

輸出描述:

整數N,最后一個單詞的長度。

示例1

輸入

hello world

輸出

5
import java.util.Scanner;

/**
 * 計算字符串最后一個單詞的長度,單詞以空格隔開。
 *
 */
public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String s = scanner.nextLine();
            String[] words = s.split(" ");
            System.out.println(words[words.length-1].length());
        }
    }
}

2.計算字符個數

寫出一個程序,接受一個由字母和數字組成的字符串,和一個字符,然后輸出輸入字符串中含有該字符的個數。不區分大小寫。

輸入描述:

第一行輸入一個有字母和數字以及空格組成的字符串,第二行輸入一個字符。

輸出描述:

輸出輸入字符串中含有該字符的個數。

示例1

輸入

ABCDEF
A

輸出

1
import java.util.Scanner;

/**
 * 接受一個由字母和數字組成的字符串,和一個字符,然后輸出輸入字符串中含有該字符的個數。不區分大小寫。
 *
 */
public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String s = input.next().toUpperCase();
        char c = input.next().toUpperCase().charAt(0);
        int count = getNum(s, c);
        System.out.println(count);
    }

    /*public static int getNum(String s, char c) {
        char[] arr = s.toCharArray();
        int count = 0;
        for (char i : arr) {
            if (i == c)
                count++;
        }
        return count;
    }*/

    public static int getNum(String s, char c) {
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            if (c == s.charAt(i))
                count++;
        }
        return count;
    }
}

3.去重和排序

對一組數據完成去重與排序的工作,注意第一個數字為數據的個數。

import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

/**
 * 對一組數據完成去重與排序的工作
 * 
 */
public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (input.hasNext()) {
            int num = input.nextInt();
            Set<Integer> set = new TreeSet<>();
            for (int i = 0; i < num; i++) {
                set.add(input.nextInt());
            }

            for (Integer i : set) {
                System.out.print(i + " ");
            }
            System.out.println();
        }
    }

}

4.字符串分割與補零

?連續輸入字符串,請按長度為8拆分每個字符串后輸出到新的字符串數組;

?長度不是8整數倍的字符串請在后面補數字0,空字符串不處理。

輸入描述:

連續輸入字符串(輸入2次,每個字符串長度小于100)

輸出描述:

輸出到長度為8的新字符串數組

示例1

輸入

abc
123456789

輸出

abc00000
12345678
90000000
import java.util.Scanner;

/**
 * ?連續輸入字符串,請按長度為8拆分每個字符串后輸出到新的字符串數組;
 * ?長度不是8整數倍的字符串請在后面補數字0,空字符串不處理。
 */
public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (input.hasNextLine()) {
            String s = input.nextLine();
            splitString(s);
            System.out.println();
        }
    }

    public static void splitString(String s) {
        while (s.length() >= 8) {
            System.out.print(s.substring(0, 8)+" ");
            s = s.substring(8);
        }
        
        if (s.length() < 8 && s.length() > 0) {
            s = s + "0000000";
            System.out.print(s.substring(0, 8));
        }
    }
}

5.十六進制轉換為十進制

寫出一個程序,接受一個十六進制的數,輸出該數值的十進制表示。(多組同時輸入 )

輸入描述:

輸入一個十六進制的數值字符串。

輸出描述:

輸出該數值的十進制字符串。

示例1

輸入

0xA

輸出

10
import java.util.Scanner;


public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (input.hasNext()) {
            String s = input.nextLine();
        /*    char[] arr = s.toCharArray();
            int result = 0;
            int index = 0;
            for (int i = arr.length - 1; i > 1; i--) {
                int num = conversion(arr[i]);
                result += num * Math.pow(16, index++);
            }
            System.out.println(result);*/
            
            System.out.println(conversion(s.substring(2)));
        }
        input.close();
    }public static int conversion(String s) {
        int n = 0;
        int count = 0;
        int temp = 0;
        char ch;

        while (count < s.length()) {
            ch = s.charAt(s.length() - count - 1);
            if (ch >= '0' && ch <= '9') {
                temp = ch - '0';
            } else if (ch >= 'A' && ch <= 'Z') {
                temp = ch - 'A' + 10;
            } else if (ch >= 'a' && ch <= 'z') {
                temp = ch - 'a' + 10;
            } else {
                break;
            }
            n += temp * Math.pow(16, count);
            count++;
        }

        return n;
    }
}

6.質數因子

功能:輸入一個正整數,按照從小到大的順序輸出它的所有質因子(如180的質因子為22335)

最后一個數后面也要有空格

輸入描述:

輸入一個long型整數

輸出描述:

按照從小到大的順序輸出它的所有質數的因子,以空格隔開。最后一個數后面也要有空格。

示例1

輸入

180

輸出

2 2 3 3 5

import java.util.Scanner;

/**
 * 功能:輸入一個正整數,按照從小到大的順序輸出它的所有質數的因子(如180的質數因子為2 2 3 3 5 )最后一個數后面也要有空格
 */
public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Long number = input.nextLong();
        if (number < 2) {
            input.close();
            return;
        }
        isPrimeFactors(number);
        input.close();

    }

    public static void isPrimeFactors(Long number) {
        while (number != 1) {
            for (int i = 2; i <= number; i++) {
                if (number % i == 0) {
                    number = number / i;
                    System.out.print(i + " ");
                    break;
                }
            }
        }
    }
}

7.四舍五入

寫出一個程序,接受一個正浮點數值,輸出該數值的近似整數值。如果小數點后數值大于等于5,向上取整;小于5,則向下取整。

輸入描述:

輸入一個正浮點數值

輸出描述:

輸出該數值的近似整數值

示例1

輸入

5.5

輸出

6
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        double number = input.nextDouble();
        int res = round(number);
        System.out.println(res);
        input.close();
    }

    public static int round(double number) {
        int res = (int) number;
        return (number - res) >= 0.5 ? res + 1 : res;
    }
}

8.對表索引相同的記錄進行合并

數據表記錄包含表索引和數值(int范圍的整數),請對表索引相同的記錄進行合并,即將相同索引的數值進行求和運算,輸出按照key值升序進行輸出。

輸入描述:

先輸入鍵值對的個數
然后輸入成對的index和value值,以空格隔開

輸出描述:

輸出合并后的鍵值對(多行)

示例1

輸入

4
0 1
0 2
1 2
3 4

輸出

0 3
1 2
3 4
import java.util.Scanner;
import java.util.TreeMap;


public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int num = Integer.parseInt(input.nextLine());

        TreeMap<Integer, Integer> map = new TreeMap<>();
        for (int i = 0; i < num; i++) {
            String s = input.nextLine();
            getResult(s, map);
        }

        for (Integer key : map.keySet()) {
            System.out.println(key + " " + map.get(key));
        }

    }

    public static void getResult(String s, TreeMap<Integer, Integer> map) {
        String[] arr = s.split(" ");
        int key = Integer.parseInt(arr[0]);
        int value = Integer.parseInt(arr[1]);
        if (map.containsKey(key)) {
            value += map.get(key);
            map.put(key, value);
        } else {
            map.put(key, value);
        }
    }

}

9.提取不重復的整數

輸入一個int型整數,按照從右向左的閱讀順序,返回一個不含重復數字的新的整數。

輸入描述:

輸入一個int型整數

輸出描述:

按照從右向左的閱讀順序,返回一個不含重復數字的新的整數

示例1

輸入

9876673

輸出

37689

import java.util.LinkedHashSet;
import java.util.Scanner;

public class T09提取不重復的整數 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String s = input.nextLine();
        LinkedHashSet<Character> set = new LinkedHashSet<>();
        for (int i = s.length() - 1; i >= 0; i--) {
            char ch = s.charAt(i);
            set.add(ch);
        }
        for (Character ch : set) {
            System.out.print(ch);
        }
        input.close();
    }

}

10.統計字符個數

編寫一個函數,計算字符串中含有的不同字符的個數。字符在ACSII碼范圍內(0~127),換行表示結束符,不算在字符里。不在范圍內的不作統計。

輸入描述:

輸入N個字符,字符在ACSII碼范圍內。

輸出描述:

輸出范圍在(0~127)字符的個數。

示例1

輸入

abc

輸出

3
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String s = input.nextLine();
        int count = 0;
        int[] arr = new int[128];
        for (int i = 0; i < s.length(); i++) {
            if (arr[s.charAt(i)] == 0) {
                arr[s.charAt(i)]++;
            }
        }
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] != 0) {
                count++;
            }
        }
        System.out.println(count);
    }

}

11.按照字典的順序排列字符串

給定n個字符串,請對n個字符串按照字典序排列。

輸入描述:

輸入第一行為一個正整數n(1≤n≤1000),下面n行為n個字符串(字符串長度≤100),字符串中只含有大小寫字母。

輸出描述:

數據輸出n行,輸出結果為按照字典序排列的字符串。

示例1

輸入

9
cap
to
cat
card
two
too
up
boat
boot

輸出

boat
boot
cap
card
cat
to
too
two
up

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int num = Integer.parseInt(input.nextLine());
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            String s = input.nextLine();
            list.add(s);
        }
        Collections.sort(list);
        for (String s : list) {
            System.out.println(s);
        }
        input.close();
    }

}

12.密碼驗證合格程序

密碼要求:

1.長度超過8位

2.包括大小寫字母.數字.其它符號,以上四種至少三種

3.不能有相同長度超2的子串重復

說明:長度超過2的子串

輸入描述:

一組或多組長度超過2的子符串。每組占一行

輸出描述:

如果符合要求輸出:OK,否則輸出NG

示例1

輸入

021Abc9000
021Abc9Abc1
021ABC9000
021$bc9000

輸出

OK
NG
NG
OK
import java.util.Scanner;
import java.util.regex.Pattern;

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (input.hasNext()) {
            String str = input.nextLine();
            String res = isPassword(str);
            System.out.println(res);
        }

    }

    public static String isPassword(String str) {
        //1.判斷長度
        if (str == null || str.length() < 9) {
            return "NG";
        }

        int count = 0;
        //2.判斷是否含有三種以上字符
        /*Pattern p1 = Pattern.compile("[a-z]");
        Matcher m1 = p1.matcher(str);
        if (m1.find())
            count++;
        Pattern p2 = Pattern.compile("[A-Z]");
        Matcher m2 = p2.matcher(str);
        if (m2.find())
            count++;
        Pattern p3 = Pattern.compile("[0-9]");
        Matcher m3 = p3.matcher(str);
        if (m3.find())
            count++;
        
        Pattern p4 = Pattern.compile("[^a-zA-Z0-9]");
        Matcher m4 = p4.matcher(str);
        if (m4.find())
            count++;*/
        if (Pattern.matches(".*[a-z]+.*", str)) //+表達式至少出現1次,相當于 {1,}
            count++;
        if (Pattern.matches(".*[A-Z]+.*", str)) //*表達式至少出現0次,表達式不出現或出現任意次,相當于 {0,}
            count++;
        if (Pattern.matches(".*[0-9]+.*", str)) //.小數點可以匹配任意一個字符(除了換行符)
            count++;
        if (Pattern.matches(".*[^a-zA-Z0-9]+.*", str))
            count++;

        if (count < 3) {
            return "NG";

        } else {
            return isHasSubString(str);
        }
    }

    private static String isHasSubString(String str) {
        for (int i = 0; i < str.length() - 3; i++) {
            String str1 = str.substring(i, i + 3);
            String str2 = str.substring(i + 3);
            if (str2.contains(str1))
                return "NG";
        }
        return "OK";
    }
}

13.密碼轉換

大家都知道手機上的字母:1--1,abc--2,def--3,ghi--4,jkl--5,mno--6,pqrs--7,tuv--8wxyz--9,0--0,就這么簡單,淵子把密碼中出現的小寫字母都變成對應的數字,數字和其他的符號都不做變換,

聲明:密碼中沒有空格,而密碼中出現的大寫字母則變成小寫之后往后移一位,如:X,先變成小寫,再往后移一位,不就是y了嘛,簡單吧。記住,z往后移是a哦。

輸入描述:

輸入包括多個測試數據。輸入是一個明文,密碼長度不超過100個字符,輸入直到文件結尾

輸出描述:

輸出淵子真正的密文

示例1

輸入

YUANzhi1987

輸出

zvbo9441987
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (input.hasNext()) {
            String str = input.nextLine();
            char[] arr = str.toCharArray();
            for (int i = 0; i < arr.length; i++) {
                char num = arr[i];
                if (num >= '0' && num <= '9') {
                    continue;
                } else if ("abc".contains(num + "")) {
                    arr[i] = '2';
                } else if ("def".contains(num + "")) {
                    arr[i] = '3';
                } else if ("ghi".contains(num + "")) {
                    arr[i] = '4';
                } else if ("jkl".contains(num + "")) {
                    arr[i] = '5';
                } else if ("mno".contains(num + "")) {
                    arr[i] = '6';
                } else if ("pqrs".contains(num + "")) {
                    arr[i] = '7';
                } else if ("tuv".contains(num + "")) {
                    arr[i] = '8';
                } else if ("wxyz".contains(num + "")) {
                    arr[i] = '9';
                } else if (num>='A' && num<='Y') {
                    arr[i] = (char) (num + 33);
                } else if (num =='Z') {
                    arr[i] = 'a';
                } else{
                    System.out.println("your password is error!");
                    return;
                }
            }
            for (char c : arr) {
                System.out.print(c);
            }
        }
    }

}

14.刪除出現次數最少的字符

實現刪除字符串中出現次數最少的字符,若多個字符出現次數一樣,則都刪除。輸出刪除這些單詞后的字符串,字符串中其它字符保持原來的順序。
注意每個輸入文件有多組輸入,即多個字符串用回車隔開

輸入描述:

字符串只包含小寫英文字母,不考慮非法輸入,輸入的字符串長度小于等于20個字節。

輸出描述:

刪除字符串中出現次數最少的字符后的字符串。

示例1

輸入

abcdd

輸出

dd
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeMap;


public class Main {

    /*public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (input.hasNext()) {
            String s = input.nextLine();
            Map<Character, Integer> map = new TreeMap<>();
            for (int i = 0; i < s.length(); i++) {
                char key = s.charAt(i);
                if (map.containsKey(key)) {
                    map.put(key, map.get(key) + 1);
                } else {
                    map.put(key, 1);
                }
            }
    
            List<Map.Entry<Character, Integer>> list = new ArrayList<>(map.entrySet());
            Collections.sort(list, new Comparator<Map.Entry<Character, Integer>>() {
                @Override
                public int compare(Entry<Character, Integer> o1, Entry<Character, Integer> o2) {
                    return o1.getValue() - o2.getValue();
                }
            });
            String newS = s.replace(list.get(0).getKey() + "", "");
            for (int i = 1; i < list.size(); i++) {
                if (list.get(0).getValue() == list.get(i).getValue()) {
                    newS = newS.replace(list.get(i).getKey() + "", "");
                }else {
                    break;
                }
            }
            System.out.println(newS);
        }
        input.close();
    }*/

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (input.hasNext()) {
            String str = input.nextLine();
            int[] count = new int[26];
            char[] chs = str.toCharArray();
            int min = Integer.MAX_VALUE;
            for (int i = 0; i < chs.length; i++) {
                count[chs[i] - 'a']++;
                min = min > count[chs[i] - 'a'] ? count[chs[i] - 'a'] : min;
            }
            for (int i = 0; i < count.length; i++) {
                if (count[i] == min) {
                    str = str.replaceAll(String.valueOf((char) (i + 'a')), "");
                }
            }
            System.out.println(str);
        }
        input.close();
    }
}

15.蛇形矩陣

蛇形矩陣是由1開始的自然數依次排列成的一個矩陣上三角形。

樣例輸入

5

樣例輸出

1361015

25914

4813

712

11

import java.util.Scanner;


public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (input.hasNext()) {
            int N = Integer.parseInt(input.nextLine());
            int[][] m = new int[N][N];
            m[0][0] = 1; //核心思想,后一項與前一項的關系
            System.out.print(m[0][0] + " ");
            for (int i = 0; i < N - 1; i++) {
                m[i + 1][0] = m[i][0] + i + 1;
                for (int j = 0; j < N - 1 - i; j++) {
                    m[i][j + 1] = m[i][j] + j + i + 2;
                    System.out.print(m[i][j + 1] + " ");
                }
                System.out.print("
" + m[i + 1][0] + " ");
            }
            System.out.println(); //注意在每個測試用例后添加換行
        }
        input.close();
    }
}

16.順時針打印矩陣

package nowcode;


/**
 * 順時針打印矩陣
 *     輸入一個矩陣,按照從外向里以順時針的順序依次打印出每一個數字,
 *     例如,如果輸入如下4 X 4矩陣:
 *          1  2  3  4 
 *          5  6  7  8 
 *          9 10 11 12 
 *          13 14 15 16 
 *     則依次打印出數字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.
 */
public class T22printMatrixInCircle {

    public static void main(String[] args) {
        int[][] m = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } };
        int rows = m.length;
        int cols = m[0].length;
        int start = 0;
        while (cols > 2 * start && rows > 2 * start) {
            pringMatrixInCircle(m, rows, cols, start);
            start++;
        }
    }

    public static void pringMatrixInCircle(int[][] m, int rows, int cols, int start) {
        //1.從左到右打印
        for (int j = start; j < cols - start; j++) {
            System.out.print(m[start][j] + " ");
        }

        //2.從上到下
        for (int i = start + 1; i < rows - start; i++) {
            System.out.print(m[i][cols - start - 1] + " ");
        }

        //3.從右到左
        for (int j = cols - start - 2; j >= start; j--) {
            System.out.print(m[rows - start - 1][j] + " ");
        }

        //4.從下到上
        for (int i = rows - start - 2; i > start; i--) {
            System.out.print(m[i][start] + " ");
        }
    }
}

17.字符串加密

package nowcode;

import java.util.LinkedHashSet;
import java.util.Scanner;
import java.util.Set;

/**
 * 有一種技巧可以對數據進行加密,它使用一個單詞作為它的密匙。下面是它的工作原理:首先,選擇一個單詞作為密匙,如TRAILBLAZERS。如果單詞中包含有重復的字母,只保留第1個,其余幾個丟棄。現在,修改過的那個單詞屬于字母表的下面,如下所示:
    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
    T R A I L B Z E S C D F G H J K M N O P Q U V W X Y
    上面其他用字母表中剩余的字母填充完整。在對信息進行加密時,信息中的每個字母被固定于頂上那行,并用下面那行的對應字母一一取代原文的字母(字母字符的大小寫狀態應該保留)。因此,使用這個密匙,Attack AT DAWN(黎明時攻擊)就會被加密為Tpptad TP ITVH。
    通過指定的密匙和明文得到密文。
    
    輸入描述:
    先輸入key和要加密的字符串
    
    輸出描述:
    返回加密后的字符串
    
    示例1
    輸入
    nihao
    ni
    輸出
    le
 */
public class T23字符串加密 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String s1 = "abcdefghijklmnopqrstuvwxyz";
        while (input.hasNext()) {
            StringBuffer sb = new StringBuffer(s1);
            String str = input.nextLine();
            String data = input.nextLine();
            String disStr = getDistinctString(str);
            //            System.out.println(disStr);

            String key = completion(sb, disStr);
            //            System.out.println(key);
            String result = getPlainText(key, data);
            System.out.println(result);
        }
    }

    /**
     * 字符串去重
     */
    public static String getDistinctString(String s) {
        Set<Character> set = new LinkedHashSet<>();
        for (int i = 0; i < s.length(); i++) {
            set.add(s.charAt(i));
        }
        String res = "";
        for (Character ch : set) {
            res += ch;
        }
        return res;
    }

    /**
     * 字符串補齊
     * @param sb:標準字符串
     * @param str:去重之后的字符串
     * @return 
     */
    public static String completion(StringBuffer sb, String str) {
        //int index = 0;
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);

            sb.deleteCharAt(sb.indexOf(ch + ""));
            //sb.insert(index++, ch);
        }
        return str + sb.toString();
    }

    /**
     * 得到明文
     * @param key:密鑰
     * @param data:加密的數據
     * @return
     */
    public static String getPlainText(String key, String data) {
        String res = "";
        for (int i = 0; i < data.length(); i++) {
            char ch = data.charAt(i);
            if (Character.isLowerCase(ch)) {
                res += key.charAt(ch - 'a');
            } else if (Character.isUpperCase(ch)) {
                res += (char) (key.charAt(ch - 'A') - 32);
            } else {
                System.out.println("wrong");
                return null;
            }
        }
        return res;
    }
}

18.最長遞增子序列

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (input.hasNext()) {
            String str = input.nextLine();
            String[] arr = str.split(" ");
            int[] nums = new int[arr.length];
            for (int i = 0; i < nums.length; i++) {
                nums[i] = Integer.parseInt(arr[i]);
            }
            int res = getMaxLengthOfSubsequence(nums);
            System.out.println(res);
        }
        input.close();
    }

    //獲得最長子序列
    public static int getMaxLengthOfSubsequence(int[] nums) {
        int n = nums.length;
        int[] ls = new int[n];
        int res = 1;

        for (int i = 0; i < n; i++) {//填充ls
            int maxValue = 1;
            ls[i] = 1;
            for (int j = 0; j < i; j++) { ///遍歷所有的A
                if (nums[i] > nums[j]) {
                    maxValue = max(maxValue, ls[j] + 1);
                }
                ls[i] = maxValue;
            }
        }

        int index = 0;
        //最長子序列末尾出現的位置
        for (int i = 0; i < n; i++) { //查找最大的子序列
            if (ls[i] > res) {
                res = ls[i];
                index = i;
            }
        }
        
        int count = res; //從后往前找子序列
        int[] arrRes = new int[count];
        arrRes[--count] = nums[index];
        for (int i = index; i >= 0; i--) {
            if (nums[i] < nums[index] && ls[i] == ls[index] - 1) {
                arrRes[--count] = nums[i];
                index = i;
            }
        }

        System.out.println(Arrays.toString(arrRes));
        return res;
    }

    public static int max(int a, int b) {
        return a > b ? a : b;
    }

}

19.合唱隊

package nowcode2;

import java.util.Arrays;
import java.util.Scanner;

/**
 * 計算最少出列多少位同學,使得剩下的同學排成合唱隊形
    說明:
    N位同學站成一排,音樂老師要請其中的(N-K)位同學出列,使得剩下的K位同學排成合唱隊形。 
    合唱隊形是指這樣的一種隊形:設K位同學從左到右依次編號為1,2…,K,他們的身高分別為T1,T2,…,TK,   則他們的身高滿足存在i(1<=i<=K)使得T1<T2<......<Ti-1<Ti>Ti+1>......>TK。 
    你的任務是,已知所有N位同學的身高,計算最少需要幾位同學出列,可以使得剩下的同學排成合唱隊形。 
    
    輸入描述:
    整數N
    
    輸出描述:
    最少需要幾位同學出列
    
    示例1
    輸入
    8
    186 186 150 200 160 130 197 200
    輸出
    4
 */
public class T25合唱隊 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (input.hasNext()) {
            int num = Integer.parseInt(input.nextLine());
            String str = input.nextLine();
            String[] snum = str.split(" ");
            int[] nums = new int[num];
            for (int i = 0; i < num; i++) {
                nums[i] = Integer.parseInt(snum[i]);
            }

            int[] l1 = new int[num];
            int[] l2 = new int[num];

            int res = getMaxLenOfSubsequence(nums, l1, l2, num);
            System.out.println(res);
        }
        input.close();
    }

    public static int getMaxLenOfSubsequence(int[] nums, int[] l1, int[] l2, int num) {
        // 核心思想:動態規劃 li = max(lj) +1,其中  0 <= j <i。
        for (int i = 0; i < num; i++) {//由左向右遍歷
            l1[i] = 1;
            for (int j = 0; j < i; j++) { 
                if (nums[i] > nums[j] && l1[i] < l1[j] + 1) {
                    l1[i] = l1[j] + 1;
                }
            }
        }
        for (int i = num - 1; i >= 0; i--) { //從右向左遍歷
            l2[i] = 1;
            for (int j = i + 1; j < num; j++) {
                if (nums[i] > nums[j] && l2[i] < l2[j] + 1) {
                    l2[i] = l2[j] + 1;
                }
            }
        }

        int res = 0;
        for (int i = 0; i < num; i++) {
            if (res < l1[i] + l2[i] - 1) {
                res = l1[i] + l2[i] - 1;
            }
        }
        return num - res;
    }
}

20.數據分類處理

輸入描述:

一組輸入整數序列I和一組規則整數序列R,I和R序列的第一個整數為序列的個數(個數不包含第一個整數);整數范圍為0~0xFFFFFFFF,序列個數不限

輸出描述:

從R依次中取出R<i>,對I進行處理,找到滿足條件的I<j>:

I<j>整數對應的數字需要連續包含R<i>對應的數字。比如R<i>為23,I<j>為231,那么I<j>包含了R<i>,條件滿足。

按R<i>從小到大的順序:

(1)先輸出R<i>;

(2)再輸出滿足條件的I<j>的個數;

(3)然后輸出滿足條件的I<j>在I序列中的位置索引(從0開始);

(4)最后再輸出I<j>。

附加條件:

(1)R<i>需要從小到大排序。相同的R<i>只需要輸出索引小的以及滿足條件的I<j>,索引大的需要過濾掉

(2)如果沒有滿足條件的I<j>,對應的R<i>不用輸出

(3)最后需要在輸出序列的第一個整數位置記錄后續整數序列的個數(不包含“個數”本身)

序列I:15,123,456,786,453,46,7,5,3,665,453456,745,456,786,453,123(第一個15表明后續有15個整數)

序列R:5,6,3,6,3,0(第一個5表明后續有5個整數)

輸出:30,3,6,0,123,3,453,7,3,9,453456,13,453,14,123,6,7,1,456,2,786,4,46,8,665,9,453456,11,456,12,786

說明:

30----后續有30個整數

3----從小到大排序,第一個R<i>為0,但沒有滿足條件的I<j>,不輸出0,而下一個R<i>是3

6---存在6個包含3的I<j>

0---123所在的原序號為0

123---123包含3,滿足條件

示例1

輸入

15 123 456 786 453 46 7 5 3 665 453456 745 456 786 453 123
5 6 3 6 3 0

輸出

30 3 6 0 123 3 453 7 3 9 453456 13 453 14 123 6 7 1 456 2 786 4 46 8 665 9 453456 11 456 12 786
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class Main {
    /*
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            while (input.hasNext()) {
                String I = input.nextLine();
                String[] sarrI = I.substring(I.indexOf(" ") + 1).split(" ");
                String R = input.nextLine();
                String[] sarrR = R.substring(R.indexOf(" ") + 1).split(" ");
                int[] arrR = new int[sarrR.length];
                for (int i = 0; i < sarrR.length; i++) {
                    arrR[i] = Integer.parseInt(sarrR[i]);
                }
                //            System.out.println(Arrays.toString(sarrI));
                //            System.out.println(Arrays.toString(arrR));
    
                ArrayList<String> listR = sortAndDistinct(arrR);
                //            System.out.println(Arrays.toString(arrR));
                ArrayList<Integer> listRes = new ArrayList();
    //            System.out.println(listR);
                
                for (int i = 0; i < listR.size(); i++) {
                    listRes.add(Integer.parseInt(listR.get(i)));
                    ArrayList<String> listNums = new ArrayList<>();
                    ArrayList<Integer> listIndexs = new ArrayList<>();
                    for (int j = 0; j < sarrI.length; j++) {
                        if (sarrI[j].contains(listR.get(i))) {
                            listNums.add(sarrI[j]);
                            listIndexs.add(j);
                        }
                    }
                    
                    if(listNums.size()== 0) {
                        listRes.remove(listRes.size()-1); 
                        continue;
                    }
                    
                    listRes.add(listNums.size());
                    
                    for(int k = 0; k< listNums.size();k++) {
                        listRes.add(listIndexs.get(k));
                        listRes.add(Integer.parseInt(listNums.get(k)));
                    }
                }
                
                System.out.print(listRes.size()+" ");
                for (Integer val :listRes ) {
                    System.out.print(val + " ");
                }
    
            }
            input.close();
        }
    
        public static ArrayList<String> sortAndDistinct(int[] arr) {
            Arrays.sort(arr);
            LinkedHashSet<Integer> set = new LinkedHashSet<>();
            for (int i = 0; i < arr.length; i++) {
                set.add(arr[i]);
            }
    
            ArrayList<String> list = new ArrayList<>();
            for (Integer i : set) {
                list.add(String.valueOf(i));
            }
            return list;
        }*/

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (input.hasNext()) {
            String strI = input.nextLine();
            String strR = input.nextLine();
            String res = fun(strI, strR);
            System.out.println(res);
        }
        input.close();
    }

    public static String fun(String strI, String strR) {
        String[] arrI = strI.split(" ");
        String[] arrR = strR.split(" ");

        Set<Integer> setR = new TreeSet<>(); //有序的無重復數字可以用TreeSet
        for (int i = 1; i < arrR.length; i++) {
            setR.add(Integer.parseInt(arrR[i]));
        }

        StringBuffer result = new StringBuffer();

        for (int rdata : setR) {
            int num = 0;
            StringBuffer sb = new StringBuffer();

            for (int i = 1; i < arrI.length; i++) {
                if (arrI[i].contains(String.valueOf(rdata))) {
                    num++;
                    sb.append((i - 1) + " ");
                    sb.append(arrI[i] + " ");
                }
            }

            if (num != 0) {
                result.append(rdata + " ");
                result.append(num + " ");
                result.append(sb);
            }
        }
        int all_num = result.toString().split(" ").length;
        result.insert(0, (all_num) + " ");
        return result.toString();
    }
}

21.字符串排序

package nowcode2;

import java.util.Arrays;
import java.util.Scanner;

/**
 * 題目描述
    編寫一個程序,將輸入字符串中的字符按如下規則排序。
    規則 1 :英文字母從 A 到 Z 排列,不區分大小寫。
    如,輸入: Type 輸出: epTy
    規則 2 :同一個英文字母的大小寫同時存在時,按照輸入順序排列。
    如,輸入: BabA 輸出: aABb
    規則 3 :非英文字母的其它字符保持原來的位置。
    如,輸入: By?e 輸出: Be?y
    
    輸入描述:
    輸入字符串
    輸出描述:
    輸出字符串
    示例1
    輸入
    A Famous Saying: Much Ado About Nothing (2012/8).
    輸出
    A aaAAbc dFgghh: iimM nNn oooos Sttuuuy (2012/8).
 */
public class T27字符串排序 {

    /*public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (input.hasNext()) {
            String str = input.nextLine();
            char[] data = str.toCharArray();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < data.length; i++) {
                char ch = data[i];
                if (Character.isLetter(ch)) {
                    sb.append(ch);
                }
            }
            char[] sarr = sb.toString().toCharArray();
            getSortString(sarr);
    
            int index = 0;
            for (int i = 0; i < data.length; i++) {
                if (Character.isLetter(data[i])) {
                    System.out.print(sarr[index++]);
                } else {
                    System.out.print(data[i]);
                }
            }
            System.out.println();
        }
        input.close();
    }
    
    public static void getSortString(char[] sarr) {
        char temp = '*';
        for (int i = 0; i < sarr.length - 1; i++) {
            for (int j = sarr.length - 1; j > i; j--) {
                if (Character.toUpperCase(sarr[j - 1]) > Character.toUpperCase(sarr[j])) { //前面的字母比后面的字母小
                    temp = sarr[j - 1];
                    sarr[j - 1] = sarr[j];
                    sarr[j] = temp;
                } 
            }
        }
    }*/

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (input.hasNext()) {
            String str = input.nextLine();
            //            char[] arr = str.toCharArray();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < 26; i++) {
                char ch = (char) (i + 'A');
                for (int j = 0; j < str.length(); j++) {
                    if (ch == str.charAt(j) || ch == str.charAt(j) - 32) {
                        sb.append(str.charAt(j));
                    }
                }
            }

            for (int i = 0; i < str.length(); i++) {
                if (!Character.isLetter(str.charAt(i))) {
                    sb.insert(i, str.charAt(i));
                }
            }
            System.out.println(sb.toString());
        }
        input.close();
    }
}

22.查找兄弟單詞

package nowcode2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class T28FindBrotherWords {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s = null;
        while ((s = br.readLine()) != null) {
            String[] vals = s.split(" ");
            if (vals.length < 4)
                continue;
            int num = Integer.parseInt(vals[0]);
            if (num > 1000)
                continue;
            String key = vals[num + 1];
            int index = Integer.parseInt(vals[num + 2]);
            List<String> list = new ArrayList<String>();
            for (int i = 1; i <= num; i++) {
                if (isBrothStr(vals[i], key)) {
                    list.add(vals[i]);
                }
            }
            Collections.sort(list);
            System.out.println(list.size());
            if (list.size() >= index)
                System.out.println(list.get(index - 1));
        }
    }

    public static boolean isBrothStr(String source, String dest) {
        if (source.equals(dest) || source.length() != dest.length())
            return false;

        for (int i = 'a'; i <= 'z'; i++) {
            char ch = (char) i;
            if (getCharSize(source, ch) != getCharSize(dest, ch))
                return false;
        }
        return true;
        /*List<Character> list1 = new ArrayList<>();
        List<Character> list2 = new ArrayList<>();
        for (int i = 0; i < source.length(); i++) {
            list1.add(source.charAt(i));
            list2.add(dest.charAt(i));
        }
        System.out.println("list1:" + list1);
        System.out.println("list2:" + list2);
        return list1.containsAll(list2); // containsAll方法不能比較兩個list是否相等,
                                            //反例:list1:[a, b, c, b]和list2:[a, c, a, b]也是true
        */

    }

    public static int getCharSize(String source, char ch) {
        int count = 0;
        for (int i = 0; i < source.length(); i++)
            if (source.charAt(i) == ch)
                count++;
        return count;
    }

}

23.字符串加密

package nowcode2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
/**
 * 題目描述
    1、對輸入的字符串進行加解密,并輸出。
    2加密方法為:
    當內容是英文字母時則用該英文字母的后一個字母替換,同時字母變換大小寫,如字母a時則替換為B;字母Z時則替換為a;
    當內容是數字時則把該數字加1,如0替換1,1替換2,9替換0;
    其他字符不做變化。
    3、解密方法為加密的逆過程。
    
    說明:
    1、字符串以結尾。
    2、字符串最長100個字符。
    說明:
    
    1、字符串以結尾。
    2、字符串最長100個字符。
        
    輸入描述:
    輸入說明
    輸入一串要加密的密碼
    輸入一串加過密的密碼
    
    輸出描述:
    輸出說明
    輸出加密后的字符
    輸出解密后的字符
    
    示例1
    輸入
    abcdefg
    BCDEFGH
    輸出
    BCDEFGH
    abcdefg
 */
public class T30StringEncrption {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = "";
        while ((str = br.readLine()) != null) {
            String s1 = str;
            String s2 = br.readLine();

            String res1 = encrption(s1);
            System.out.println(res1);
            String res2 = decrption(s2);
            System.out.println(res2);
        }
    }

    public static String encrption(String s) {
        char[] arr = s.toCharArray();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] >= 'a' && arr[i] < 'z') {
                sb.append((char) (arr[i] - 31));
            } else if (arr[i] == 'z') {
                sb.append('A');
            } else if (arr[i] >= 'A' && arr[i] < 'Z') {
                sb.append((char) (arr[i] + 33));
            } else if (arr[i] == 'Z') {
                sb.append('a');
            } else if (arr[i] >= '0' && arr[i] < '9') {
                sb.append((char) (arr[i] + 1));
            } else if (arr[i] == '9') {
                sb.append('0');
            }
        }
        return sb.toString();
    }

    public static String decrption(String s) {
        char[] arr = s.toCharArray();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < arr.length; i++) {

            if (arr[i] > 'a' && arr[i] <= 'z') {
                sb.append((char) (arr[i] - 33));
            } else if (arr[i] == 'a') {
                sb.append('Z');
            } else if (arr[i] > 'A' && arr[i] <= 'Z') {
                sb.append((char) (arr[i] + 31));
            } else if (arr[i] == 'A') {
                sb.append('z');
            } else if (arr[i] > '0' && arr[i] <= '9') {
                sb.append((char) (arr[i] - 1));
            } else if (arr[i] == '0') {
                sb.append('9');
            }
        }
        return sb.toString();
    }
}

24.字符串加密

package nowcode2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
/**
 * 題目描述
    1、對輸入的字符串進行加解密,并輸出。
    2加密方法為:
    當內容是英文字母時則用該英文字母的后一個字母替換,同時字母變換大小寫,如字母a時則替換為B;字母Z時則替換為a;
    當內容是數字時則把該數字加1,如0替換1,1替換2,9替換0;
    其他字符不做變化。
    3、解密方法為加密的逆過程。
    
    說明:
    1、字符串以結尾。
    2、字符串最長100個字符。
    說明:
    
    1、字符串以結尾。
    2、字符串最長100個字符。
        
    輸入描述:
    輸入說明
    輸入一串要加密的密碼
    輸入一串加過密的密碼
    
    輸出描述:
    輸出說明
    輸出加密后的字符
    輸出解密后的字符
    
    示例1
    輸入
    abcdefg
    BCDEFGH
    輸出
    BCDEFGH
    abcdefg
 */
public class T30StringEncrption {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = "";
        while ((str = br.readLine()) != null) {
            String s1 = str;
            String s2 = br.readLine();

            String res1 = encrption(s1);
            System.out.println(res1);
            String res2 = decrption(s2);
            System.out.println(res2);
        }
    }

    public static String encrption(String s) {
        char[] arr = s.toCharArray();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] >= 'a' && arr[i] < 'z') {
                sb.append((char) (arr[i] - 31));
            } else if (arr[i] == 'z') {
                sb.append('A');
            } else if (arr[i] >= 'A' && arr[i] < 'Z') {
                sb.append((char) (arr[i] + 33));
            } else if (arr[i] == 'Z') {
                sb.append('a');
            } else if (arr[i] >= '0' && arr[i] < '9') {
                sb.append((char) (arr[i] + 1));
            } else if (arr[i] == '9') {
                sb.append('0');
            }
        }
        return sb.toString();
    }

    public static String decrption(String s) {
        char[] arr = s.toCharArray();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < arr.length; i++) {

            if (arr[i] > 'a' && arr[i] <= 'z') {
                sb.append((char) (arr[i] - 33));
            } else if (arr[i] == 'a') {
                sb.append('Z');
            } else if (arr[i] > 'A' && arr[i] <= 'Z') {
                sb.append((char) (arr[i] + 31));
            } else if (arr[i] == 'A') {
                sb.append('z');
            } else if (arr[i] > '0' && arr[i] <= '9') {
                sb.append((char) (arr[i] - 1));
            } else if (arr[i] == '0') {
                sb.append('9');
            }
        }
        return sb.toString();
    }
}

25.小球彈落

package nowcode2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 假設一個球從任意高度自由落下,每次落地后反跳回原高度的一半; 再落下, 求它在第5次落地時,共經歷多少米?第5次反彈多高? 
    輸入描述:
    輸入起始高度,int型
    輸出描述:
    分別輸出第5次落地時,共經過多少米第5次反彈多高
    
    示例1
    輸入
    1
    輸出
    2.875
    0.03125
*/
public class T39BallRebounce {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String str = null;
        while ((str = bf.readLine()) != null) {
            double h = Double.parseDouble(str);
            double sum = h;
            h = h / 2;
            int times = 5;
            for (int i = 1; i < times; i++) {
                sum += h * 2;
                h = h / 2;
            }
            System.out.println(String.format("%.0f", sum));
            System.out.println(String.format("%.2f", h));
        }
    }
}

26.砝碼稱重

package nowcode2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
 * 題目描述
    現有一組砝碼,重量互不相等,分別為m1,m2,m3…mn;
    每種砝碼對應的數量為x1,x2,x3...xn。現在要用這些砝碼去稱物體的重量(放在同一側),問能稱出多少種不同的重量。
    
    注:
    稱重重量包括0
    方法原型:public static int fama(int n, int[] weight, int[] nums)
    
    輸入描述:
    輸入包含多組測試數據。
    對于每組測試數據:
    第一行:n --- 砝碼數(范圍[1,10])
    第二行:m1 m2 m3 ... mn --- 每個砝碼的重量(范圍[1,2000])
    第三行:x1 x2 x3 .... xn --- 每個砝碼的數量(范圍[1,6])
    輸出描述:
    利用給定的砝碼可以稱出的不同的重量數
    
    示例1
    輸入
    2
    1 2
    2 1
    輸出
    5
*/
public class T41砝碼 {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String str = null;
        while ((str = bf.readLine()) != null) {
            int n = Integer.parseInt(str);
            int[] weight = new int[n];
            int[] nums = new int[n];

            String[] sarrw = bf.readLine().split(" ");
            String[] sarrn = bf.readLine().split(" ");
            for (int i = 0; i < n; i++) {
                weight[i] = Integer.parseInt(sarrw[i]);
                nums[i] = Integer.parseInt(sarrn[i]);
            }
            System.out.println(fama(n, weight, nums));
        }
    }

    private static int fama(int n, int[] weight, int[] nums) {
        // TODO Auto-generated method stub
        int maxWeight = 0;
        //所有砝碼能夠表示的最大重量
        for (int i = 0; i < n; i++) {//砝碼的種類數
            maxWeight = maxWeight + nums[i] * weight[i];
        }
        boolean[] wflag = new boolean[maxWeight + 1];
        wflag[0] = true;//默認重量為0也是可以表示的
        wflag[maxWeight] = true;
        for (int i = 0; i < n; i++) {//砝碼的種類數
            for (int j = 0; j < nums[i]; j++) {//每種砝碼的個數
                for (int k = maxWeight; k >= weight[i]; k--) {//每種砝碼的單個重量
                    if (wflag[k - weight[i]]) {
                        wflag[k] = true;
                    }
                }
            }
        }
        int count = 0;
        for (boolean b : wflag) {
            if (b)
                count++;
        }
        return count;
    }
}

27.走迷宮

package nowcode2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import tool.*;
/**
 * 題目描述
    定義一個二維數組N*M(其中2<=N<=10;2<=M<=10),如5 × 5數組下所示: 
    int maze[5][5] = {
            0, 1, 0, 0, 0,
            0, 1, 0, 1, 0,
            0, 0, 0, 0, 0,
            0, 1, 1, 1, 0,
            0, 0, 0, 1, 0,
    };
    它表示一個迷宮,其中的1表示墻壁,0表示可以走的路,只能橫著走或豎著走,不能斜著走,要求編程序找出從左上角到右下角的最短路線。入口點為[0,0],既第一空格是可以走的路。
    Input
    一個N × M的二維數組,表示一個迷宮。數據保證有唯一解,不考慮有多解的情況,即迷宮只有一條通道。
    Output
    左上角到右下角的最短路徑,格式如樣例所示。
    
    輸入描述:
    輸入兩個整數,分別表示二位數組的行數,列數。再輸入相應的數組,其中的1表示墻壁,0表示可以走的路。數據保證有唯一解,不考慮有多解的情況,即迷宮只有一條通道。
    
    輸出描述:
    左上角到右下角的最短路徑,格式如樣例所示。
    
    示例1
    輸入
    5 5
    0 1 0 0 0
    0 1 0 1 0
    0 0 0 0 0
    0 1 1 1 0
    0 0 0 1 0
    輸出
    (0,0)
    (1,0)
    (2,0)
    (2,1)
    (2,2)
    (2,3)
    (2,4)
    (3,4)
    (4,4)
 */
public class T42走迷宮 {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String str = null;
        while ((str = bf.readLine()) != null) {
            String[] sarr = str.split(" ");
            int N = Integer.parseInt(sarr[0]);
            int M = Integer.parseInt(sarr[1]);
            //System.out.println(N + " " + M);
            int[][] matrix = new int[N][M];
            for (int i = 0; i < N; i++) {
                String[] arr1 = bf.readLine().split(" ");
                for (int j = 0; j < M; j++) {
                    matrix[i][j] = Integer.parseInt(arr1[j]);
                }
            }
            //Tookit.printArray(matrix);
            findShortestPath(matrix);
        }
    }

    public static void findShortestPath(int[][] maza) {
        //不考慮多解情況,迷宮只有一條通道
        //可以橫著走或者豎著走
        int row = 0;
        int col = 0;
        while (row < maza.length) {
            while (col < maza[0].length) {
                if (maza[row][col] == 0) { 
                    printPath(row, col);
                    col++;//右
                } else {//下
                    col--;
                    row++;
                }
            }
            row++;
            if (col == maza[0].length)
                col--;//左
        }
    }

    public static void printPath(int i, int j) {
        System.out.println("(" + i + "," + j + ")");
    }
}

28.數獨

問題描述:數獨(Sudoku)是一款大眾喜愛的數字邏輯游戲。玩家需要根據9X9盤面上的已知數字,推算出所有剩余空格的數字,并且滿足每一行、每一列、每一個粗線宮內的數字均含1-9,并且不重復。

輸入:
包含已知數字的9X9盤面數組[空缺位以數字0表示]
輸出:
完整的9X9盤面數組

輸入描述:

包含已知數字的9X9盤面數組[空缺位以數字0表示]

輸出描述:

完整的9X9盤面數組

示例1

輸入

0 9 2 4 8 1 7 6 3
4 1 3 7 6 2 9 8 5
8 6 7 3 5 9 4 1 2
6 2 4 1 9 5 3 7 8
7 5 9 8 4 3 1 2 6
1 3 8 6 2 7 5 9 4
2 7 1 5 3 8 6 4 9
3 8 6 9 1 4 2 5 7
0 4 5 2 7 6 8 3 1

輸出

5 9 2 4 8 1 7 6 3
4 1 3 7 6 2 9 8 5
8 6 7 3 5 9 4 1 2
6 2 4 1 9 5 3 7 8
7 5 9 8 4 3 1 2 6
1 3 8 6 2 7 5 9 4
2 7 1 5 3 8 6 4 9
3 8 6 9 1 4 2 5 7
9 4 5 2 7 6 8 3 1
package nowcode2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;

import tool.*;

public class T43數獨 {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String str = null;
        while ((str = bf.readLine()) != null) {
            int[][] matrix = new int[9][9];
            String[] sarr = str.split(" ");
            for (int j = 0; j < 9; j++) {
                matrix[0][j] = Integer.parseInt(sarr[j]);
            }
            for (int i = 1; i < 9; i++) {
                String[] temp = bf.readLine().split(" ");
                for (int j = 0; j < 9; j++) {
                    matrix[i][j] = Integer.parseInt(temp[j]);

                }
            }
            solveSudoku(matrix);
            Tookit.printArray(matrix);

        }
        bf.close();
    }

    static int solveSudoku(int[][] board) {
        int depth = 0;
        for (int i[] : board)
            for (int j : i)
                if (j == 0)
                    depth++;
        return dfs(board, depth);

    }

    static int dfs(int[][] board, int depth) {
        if (depth == 0)
            return 0;
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if (board[i][j] == 0) {
                    if (board[6][0] == 2 && board[6][1] == 1 && board[6][2] == 3)
                        board[6][2] = 5;
                    for (int k = 1; k <= 10; k++) {
                        if (k == 10)
                            return depth;
                        board[i][j] = k;
                        if (!isValid(board, i, j))
                            board[i][j] = 0;
                        else {
                            depth--;
                            depth = dfs(board, depth);
                            if (depth == 0)
                                return depth;
                            depth++;
                            board[i][j] = 0;
                        }
                    }
                }
            }
        }
        return depth;
    }

    static boolean isValid(int[][] board, int row, int col) {
        boolean[] check = new boolean[10];
        for (int i = 0; i < check.length; i++)
            check[i] = true;
        for (int i = 0; i < board[0].length; i++) {
            if (check[board[row][i]])
                check[board[row][i]] = false;
            else if (board[row][i] != 0)
                return false;
        }

        for (int i = 0; i < check.length; i++)
            check[i] = true;
        for (int i = 0; i < board.length; i++) {
            if (check[board[i][col]])
                check[board[i][col]] = false;
            else if (board[i][col] != 0)
                return false;
        }

        for (int i = 0; i < check.length; i++)
            check[i] = true;
        int rowTemp = (row / 3) * 3;
        int colTemp = (col / 3) * 3;
        for (int k = 0; k < 9; k++) {
            row = rowTemp + k / 3;
            col = colTemp + k % 3;
            if (check[board[row][col]])
                check[board[row][col]] = false;
            else if (board[row][col] != 0)
                return false;
        }
        return true;
    }
}

29.括號匹配

package nowcode2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
 * 給定一個字符串,其中的字符只包含三種括號:花括號{ }、中括號[ ]、圓括號( ),即它僅由 “( ) [ ] { }” 這六個字符組成。
 * 設計算法,判斷該字符串是否有效,即字符串中括號是否匹配。括號匹配要求括號必須以正確的順序配對,如“{ [ ] ( ) }”
 * 或 “[ ( { } [ ] ) ]”或 “{brace*&^[square(round)]end}”等為正確的格式,而“[ ( ] )”或“{ [ ( ) }”或“( { } ] )” 
 * 或“{brace*&^[square(round])end}”均為不正確的格式。
 */
public class T44括號匹配 {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String str = null;
        while ((str = bf.readLine()) != null) {
            boolean res = bracketCheck(str);
            System.out.println(res);
        }
    }

    private static boolean bracketCheck(String s) {
        Map<Character, Character> bracketMap = new HashMap<>();
        bracketMap.put('}', '{');
        bracketMap.put(')', '(');
        bracketMap.put(']', '[');

        String bracketLeft = "{([";
        String bracketRight = "}])";

        Stack<Character> stack = new Stack<>();

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            //如果是左括號
            if (bracketLeft.contains(c + "")) {
                stack.add(c);//入棧
                //如果是右括號
            } else if (bracketRight.contains(c + "")) {
                //stack不為空,并且括號匹配成功
                if (stack.size() > 0 && (stack.peek() == bracketMap.get(c))) {
                    stack.pop();//出棧
                } else {
                    return false;
                }
            }
        }
        return stack.size() == 0;//如果棧為空,則匹配上了
    }
}

30.計算器之加減乘除運算

輸入描述:

輸入一個算術表達式

輸出描述:

得到計算結果

示例1

輸入

3+2*{1+2*[-4/(8-6)+7]}

輸出

25
package nowcode3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Stack;

public class T45計算器之加減乘除運算 {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String str = null;
        while ((str = bf.readLine()) != null) {
            str = format(str);
            System.out.println(getResult(str));
        }

    }

    /**
     * 進行四則運行
     *
     * @param s 輸入一個算術表達式
     * @return 表達式結果
     */
    public static int getResult(String s) {
        // 操作符棧
        Stack<Character> optchars = new Stack<>();
        // 操作數棧
        Stack<Integer> optdata = new Stack<>();
        int index = 0;
        while (index < s.length()) {
            char c = s.charAt(index);
            // 如果是數字
            if (c >= '0' && c <= '9') {
                // 計算數字的值,可能會存在兩位及以上的數字
                int opd = 0;
                while (index < s.length() && s.charAt(index) >= '0' && s.charAt(index) <= '9') {
                    opd = opd * 10 + (s.charAt(index) - '0'); //將字符轉化成數字
                    index++;
                }
                optdata.add(opd); //將數字加入到操作數棧中
            }
            // 如果是操作符
            else {
                // 如果是左括號
                if (c == '(' || c == '[' || c == '{') {
                    optchars.add(c); //將左括號字符加入到操作符棧中
                }
                // 如果是右括號
                else if (c == ')' || c == ']' || c == '}') {
                    //要同時保證optchars.peek()操作符頂端的符號不能是'('或'['或'{',防止在有些數字的輸入形式是(3)+2,此處的3不能計算
                    while (!optchars.isEmpty() && optchars.peek() != '(' && optchars.peek() != '[' && optchars.peek() != '{') {
                        calculate(optchars, optdata);//計算左右括號內的兩個數據的運算
                    }
                    optchars.pop(); //
                }
                // 如果是乘或者除
                else if (c == '*' || c == '/') {
                    //雖然操作符是乘除,但是還要保證前面的操作符棧中不能含有括號,如果含有括號,則不能進行運算
                    while (!optchars.isEmpty() && (optchars.peek() == '*' || optchars.peek() == '/')) {
                        calculate(optchars, optdata);
                    }
                    // 操作符入棧
                    optchars.add(c);
                } else if (c == '+' || c == '-') {
                    while (!optchars.isEmpty() && (optchars.peek() == '*' || optchars.peek() == '/' || optchars.peek() == '+'
                            || optchars.peek() == '-')) {
                        calculate(optchars, optdata);
                    }
                    // 操作符入棧
                    optchars.add(c);
                }
                // 處理下一個字符
                index++;
            }
        }
        while (!optchars.isEmpty()) {
            calculate(optchars, optdata);
        }
        return optdata.pop();
    }

    /**
     * 求值操作,取optchars的最后一個操作符,optdata中的最后兩個操作數
     * @param optchars 操作符棧
     * @param optdata 操作數棧
     */
    public static void calculate(Stack<Character> optchars, Stack<Integer> optdata) {
        // 取操作數棧中的最后一個操作符
        char opt = optchars.pop();
        // 取操作數
        int v2 = optdata.pop();
        int v1 = optdata.pop();
        // 計算
        int v = calculateTowData(v1, v2, opt);
        optdata.add(v);
    }

    /**
     * 將算術表達式歸整,-5*3整理成0-5*3
     * @param s 算術表達式
     * @return 歸整后的表達式
     */
    public static String format(String s) {
        // 去掉空格
        String t = s.replaceAll("(\s)+", "");

        int index = 0;
        // 對所有的減號進行處理
        while ((index = t.indexOf('-', index)) >= 0) {//String.indexOf(String str,int index)
                                                        //從index的地方開始找,返回第一次出現的索引
            if (index == 0) {// 第一個字符是負號,要規格形式要加上0
                t = '0' + t;
            }
            // 如果不是第一個字符
            else {
                char c = t.charAt(index - 1);//負號前面的一個字符
                // 負號前面有括號,需要在前面加0
                if (c == '(' || c == '[' || c == '{') {
                    t = t.substring(0, index) + '0' + t.substring(index);
                }
            }
            index++;
        }
        return t;
    }

    /**
     * 計算 d1 operator d2,operator是加減乘除
     *
     * @param d1       操作數1
     * @param d2       操作數2
     * @param operator 操作符
     * @return 結果
     */
    public static int calculateTowData(int d1, int d2, char operator) {
        switch (operator) {
        case '+':
            return d1 + d2;
        case '-':
            return d1 - d2;
        case '*':
            return d1 * d2;
        case '/':
            return d1 / d2;
        default:
            // do nothing
        }
        return 0;
    }
}

31.名字的漂亮度

給出一個名字,該名字有26個字符串組成,定義這個字符串的“漂亮度”是其所有字母“漂亮度”的總和。
每個字母都有一個“漂亮度”,范圍在1到26之間。沒有任何兩個字母擁有相同的“漂亮度”。字母忽略大小寫。

給出多個名字,計算每個名字最大可能的“漂亮度”。

輸入描述:

整數N,后續N個名字

輸出描述:

每個名稱可能的最大漂亮程度

示例1

輸入

2
zhangsan
lisi

輸出

192
101
package nowcode3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import tool.*;

public class T46計算名字的漂亮度 {

    /*public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String str = null;
        while ((str = bf.readLine()) != null) {
            int N = Integer.parseInt(str);
            for (int i = 0; i < N; i++) {
                String name = bf.readLine();
                int res = getNameBeautyDegree(name);
                System.out.println(res);
            }
        }
    }
    
    public static int getNameBeautyDegree(String name) {
        HashMap<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < name.length(); i++) {
            char c = name.charAt(i);
            if (!map.containsKey(c)) {
                map.put(c, 1);
            } else {
                map.put(c, map.get(c) + 1);
            }
        }
        List<Map.Entry<Character, Integer>> list = new ArrayList<Map.Entry<Character, Integer>>(map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<Character, Integer>>() {
    
            @Override
            public int compare(Entry<Character, Integer> o1, Entry<Character, Integer> o2) {
                return o2.getValue().compareTo(o1.getValue());
            }
        });
        int res = 0;
        int num = 26;
        for (Entry<Character, Integer> entry : list) {
            res += entry.getValue() * num--;
        }
        return res;
    }*/

    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String str = null;
        while ((str = bf.readLine()) != null) {
            int N = Integer.parseInt(str);
            for (int i = 0; i < N; i++) {
                String name = bf.readLine();
                int res = getNameBeautyDegree(name);
                System.out.println(res);
            }
        }
    }

    public static int getNameBeautyDegree(String s) {
        int res = 0;
        int n = 26;
        int[] arr = new int[n];
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (arr[c - 'a'] != 0) {
                arr[c - 'a']++;
            } else {
                arr[c - 'a'] = 1;
            }
        }
        Arrays.sort(arr);
        for (int i = arr.length - 1; i >= 0; i--) {
            if (arr[i] > 0) {
                res += arr[i] * n--;
            }
        }
        return res;
    }
}

32.計算字符串的距離

package nowcode3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class T48計算字符串的距離 {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String str1 = null;
        while ((str1 = bf.readLine()) != null) {
            String str2 = bf.readLine();
            int res = getEditDistance(str1, str2);
            System.out.println(res);
        }
    }

    public static int getEditDistance(String s1, String s2) {
        int len1 = s1.length();
        int len2 = s2.length();

        if (len1 == 0 || len2 == 0)
            return Math.max(len1, len2);
        int[][] dp = new int[len1 + 1][len2 + 1];
        for (int i = 0; i <= len1; i++) {
            dp[i][0] = i;
        }

        for (int j = 0; j <= len2; j++) {
            dp[0][j] = j;
        }

        for (int i = 1; i <= len1; i++) {
            for (int j = 1; j <= len2; j++) {
                if (s1.charAt(i-1) == s2.charAt(j-1)) {
                    dp[i][j] = dp[i - 1][j - 1];
                } else {
                    dp[i][j] = min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1;
                }

            }
        }
        return dp[len1][len2];
    }

    //三個數中的最小值
    public static int min(int a, int b, int c) {
        int temp = a < b ? a : b;
        return temp < c ? temp : c;
    }
}

33.對應編碼

package nowcode3;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class T50對應編碼 {

    public static void main(String[] args) throws Exception {

        String[] S = { "Faint signals, barely perceptible", "Very weak signals", "Weak signals", "Fair signals",
                "Fairly good signals", "Good signals", "Moderately strong signals", "Strong signals",
                "Extremely strong signals" };
        String[] R = { "unreadable", "barely readable, occasional words distinguishable",
                "readable with considerable difficulty", "readable with practically no difficulty",
                "perfectly readable" };
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String str = null;
        while ((str = bf.readLine()) != null) {
            int r = Integer.parseInt(str.charAt(0) + "");
            int s = Integer.parseInt(str.charAt(1) + "");
            System.out.println(S[s - 1] +", "+ R[r - 1]+".");
        }
    }

}

34.兩數之和

給定一個整數數組 nums和一個目標值 target,請你在該數組中找出和為目標值的那兩個整數,并返回他們的數組下標。

你可以假設每種輸入只會對應一個答案。但是,你不能重復利用這個數組中同樣的元素。

示例:

給定 nums = [2, 7, 11, 15], target = 9

因為 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
package leecode;

import java.util.HashMap;

import com.sun.org.apache.regexp.internal.recompile;

import tool.Tookit;

public class TowSum {
    // 方法1:暴力法,兩層for循環
    /*public static int[] getTowSum(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] + nums[j] == target) {
                    return new int[] { i, j };
                }
            }
        }
        throw new IllegalArgumentException("No two sum solution");
    }*/

    // 方法2:使用HashMap快速查找
    public static int[] getTowSum(int[] nums, int target) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int competent = target - nums[i];
            if (map.containsKey(competent)) {
                return new int[] { map.get(competent), i };
            }
            map.put(nums[i], i);
        }
        throw new IllegalArgumentException("No two sum solution");
    }

    public static void main(String[] args) {
        int[] nums = { 2, 7, 11, 15 };
        int target = 9;
        int[] res = getTowSum(nums, target);
        Tookit.printArray(res);
    }

}

35.兩數字相加

給出兩個非空 的鏈表用來表示兩個非負的整數。其中,它們各自的位數是按照逆序的方式存儲的,并且它們的每個節點只能存儲一位數字。

如果,我們將這兩個數相加起來,則會返回一個新的鏈表來表示它們的和。

您可以假設除了數字 0 之外,這兩個數都不會以 0開頭。

示例:

輸入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
輸出:7 -> 0 -> 8
原因:342 + 465 = 807

官方題解

package leecode;

class ListNode {
    int val;
    ListNode next;

    public ListNode(int x) {
        this.val = x;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(this.val).append("->");

        ListNode node = this;
        while (node.next != null) {
            node = node.next;
            sb.append(node.val);

            if (node.next != null) {
                sb.append("->");
            }
        }
        return sb.toString();
    }
}

public class AddTowNumbers {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummyHead = new ListNode(0);// 頭節點
        ListNode p1 = l1, p2 = l2;
        ListNode current = dummyHead;
        int carry = 0;
        while (p1 != null || p2 != null) {
            int x1 = (p1 != null) ? p1.val : 0;
            int x2 = (p2 != null) ? p2.val : 0;
            int sum = carry + x1 + x2;
            carry = sum / 10;
            current.next = new ListNode(sum % 10);
            current = current.next;
            if (p1 != null)
                p1 = p1.next;
            if (p2 != null)
                p2 = p2.next;
        }
        if (carry > 0) {
            current.next = new ListNode(carry);
        }
        return dummyHead.next;
    }

    public static void main(String[] args) {
        /*        int[] num1 = new int[] { 2, 4, 3 };
        int[] num2 = new int[] { 5, 6, 4 };
        
        ListNode l1 = new ListNode(0);
        ListNode node = l1;
        for (int i = 0; i < num1.length; i++) {
            node.val = num1[i];
            if (i < num1.length - 1) {
                node.next = new ListNode(0);
                node = node.next;
            }
        }
        System.out.println(l1);
        
        ListNode n2 = new ListNode(0);
        node = n2;
        for (int i = 0; i < num2.length; i++) {
            node.val = num2[i];
            if (i < num2.length - 1) {
                node.next = new ListNode(0);
                node = node.next;
            }
        }
        System.out.println(n2);*/
        
        ListNode l1 = new ListNode(2);
        ListNode node2 = new ListNode(4);
        ListNode node3 = new ListNode(3);
        l1.next = node2;
        node2.next = node3;
        System.out.println(l1);

        ListNode l2 = new ListNode(5);
        ListNode node5 = new ListNode(6);
        ListNode node6 = new ListNode(4);
        l2.next = node5;
        node5.next = node6;
        System.out.println(l2);
        
        ListNode res = new AddTowNumbers().addTwoNumbers(l1, l2);
        System.out.println(res);
        
    }

}

36.無重復字符的最長子串

給定一個字符串,請你找出其中不含有重復字符的最長子串的長度。

示例1:

輸入: "abcabcbb"
輸出: 3 
解釋: 因為無重復字符的最長子串是 "abc",所以其長度為 3。

示例 2:

輸入: "bbbbb"
輸出: 1
解釋: 因為無重復字符的最長子串是 "b",所以其長度為 1。

示例 3:

輸入: "pwwkew"
輸出: 3
解釋: 因為無重復字符的最長子串是"wke",所以其長度為 3。
    請注意,你的答案必須是 子串 的長度,"pwke"是一個子序列,不是子串。

官方題解

package leecode;

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

public class T03_LengthOfLongestSubstring {

    /**
     * 方法1:暴力法
     *//*
        public int lengthOfLongestSubstring(String s) {
        int n = s.length();
        int ans = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {// s的所有子串
                if (allUnique(s, i, j)) {
                    ans = Math.max(ans, j - i);
                }
            }
        }
        return ans;
        }
        
        // 檢查子串中是否包含相同的元素
        public boolean allUnique(String s, int start, int end) {
        Set<Character> set = new HashSet<>();
        for (int i = start; i < end; i++) {
            Character ch = s.charAt(i);
            if (set.contains(ch))
                return false;
            set.add(ch);
        }
        return true;
        }
        */
    /**
     * 方法2
     *//*
        public int lengthOfLongestSubstring(String s) {
        int n = s.length();
        Set<Character> set = new HashSet<>();
        int ans = 0, i = 0, j = 0;
        
        while (i < n && j < n) {
            // try to extend the range [i, j]
            if (!set.contains(s.charAt(j))) {
                set.add(s.charAt(j++));
                ans = Math.max(ans, j - i);
            } else {
                set.remove(s.charAt(i++));
            }
        }
        return ans;
        }*/
    /**
     * 方法3
     */
    public int lengthOfLongestSubstring(String s) {
        int n = s.length(), ans = 0, i = 0;
        HashMap<Character, Integer> map = new HashMap<>();
        for (int j = 0; j < n; j++) {
            if (map.containsKey(s.charAt(j))) {
                i = Math.max(map.get(s.charAt(j)), i);
            }
            ans = Math.max(ans, j - i + 1);
            map.put(s.charAt(j), j + 1);
        }
        return ans;
    }

    public static void main(String[] args) {
        T03_LengthOfLongestSubstring s = new T03_LengthOfLongestSubstring();
        System.out.println(s.lengthOfLongestSubstring("bbbbb"));
        System.out.println(s.lengthOfLongestSubstring("abcabcbb"));
        System.out.println(s.lengthOfLongestSubstring("pwwkew"));
    }
}

總結

以上是生活随笔為你收集整理的LeetCode刷题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。