秋招笔试惨痛经历之——字符串

1. 回文系列

  1. 最长回文子串
class Solution {
    public String longestPalindrome(String s) {
        /*
        最长回文子串,时间复杂度O(n2)
        7.22
        */
        //双指针中心扩散,不转换为字符数组,回文长度为len*2-1

        String result = "";
        for(int i=0;i= 0 && right < s.length() && s.charAt(left) == s.charAt(right)){
                //截取子串长度
                String temp = s.substring(left,right+1);
                //判断长度
                if(temp.length() > result.length()){
                    result = temp;
                }

                left--;
                right++;
            }

            left--;
            right++;
        }

        return result;
    }
}
  1. 回文子串
class Solution {
    public int countSubstrings(String s) {
        /*
        回文子串
        7.26
        */
        //双指针中心扩散

        if(s.length() == 0){
            return 0;
        }

        int result = 0;
        for(int i=0;i= 0 && right < s.length() && s.charAt(left) == s.charAt(right)){
                result++;

                left--;
                right++;
            }

            left--;
            right++;
        }

        return result;
    }
}

2. 其他

  1. 字符串压缩
class Solution {
    public String compressString(String S) {
        /*
        字符串压缩
        7.27
        */

        if(S.length() < 2){
            return S;
        }

        StringBuilder result = new StringBuilder();
        char[] str = S.toCharArray();
        result.append(str[0]);

        int count = 1;

        for(int i=1;i= S.length()){
            return S;
        }else{
            return result.toString();
        }
    }
}
  1. 字符串相加
class Solution {
    public String addStrings(String num1, String num2) {
        /*
        字符串相加
        7.26
        */
        //十进制相加,双指针指向末尾模仿柱式相加

        int left = num1.length()-1;
        int right = num2.length()-1;
        int carry = 0;

        StringBuilder result = new StringBuilder();

        while(left >= 0 || right >= 0 || carry != 0){
            if(left >= 0){
                carry += num1.charAt(left--) - '0';
            }
            if(right >= 0){
                carry += num2.charAt(right--) - '0';
            }

            result.append(carry%10);
            carry /= 10;
        }

        return result.reverse().toString();
    }
}
  1. 字符串相乘
class Solution {
    public String multiply(String num1, String num2) {
        /*
        字符串相乘
        8.1
        */
        //参考字符串相加,两层遍历,外层为num1,内层为num2,使用数组来存相乘结果,sum = (result[i+j+1] + n1*n2),result[i+j+1] = sum%10,result[i+j] += sum/10;

        //判断0
        if(num1.equals("0") || num2.equals("0")){
            return "0";
        }

        int[] result = new int[num1.length() + num2.length()];

        //倒序遍历实现柱式相乘
        for(int i=num1.length()-1;i>=0;i--){
            //提取num1
            int n1 = num1.charAt(i) - '0';

            for(int j=num2.length()-1;j>=0;j--){
                //提取num2
                int n2 = num2.charAt(j) - '0';
                //相乘
                int sum = (result[i+j+1] + n1*n2);

                
                result[i+j+1] = sum%10;
                //进位
                result[i+j] += sum/10;
            }
        }
        
        StringBuilder str = new StringBuilder();
        for(int i=0;i

3. 笔试题

  1. 大疆笔试:
    C平时最喜欢玩数字游戏,最近他碰到一道有趣的数字题,他和他的好朋友打赌,一定能在10分钟内解出这道题,成功完成,小C就可以得到好朋友送他的Switch游戏机啦,你能帮助小C赢得奖品吗?
    题目是这样的:给定一个非负的、字符串形式的整形数字,例如“12353789”,字符串的长度也就是整形数字的位数不超过10000位,并且字符串不会以0开头,小C需要挑选出其中K个数字(K小于字符串的长度)并删掉他们,使得剩余字符组成新的整数是最小的。
样例输入
71245323308
4

样例输出
1223308

输入样例二:
1683212
3

输出样例二:
1212
import java.util.*;
 
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
 
        String str = sc.nextLine();
        int count = sc.nextInt();
 
        //还有考虑字符串后面有空字符的情况
        if(count == 0 || count > str.length() || str == "" || str.trim() == ""){
            System.out.println(str);
        }
 
        //k不能大于str的长度
        if(count >= str.length()){
            System.out.println("0");
        }
 
        //使用StringBuilder进行delete,先存入str
        StringBuilder builder = new StringBuilder(str);
 
        while(count > 0){
 
            int index = 0;
            //while循环排除不用删的字符:如果后一个字符大于等于当前字符,index++跳过当前
            while(index < builder.length()-1 && builder.charAt(index) <= builder.charAt(index+1)){
                index++;
            }
 
            //排除完之后,删除前一个
            builder.deleteCharAt(index);
            count--;
        }
 
        //结果的开头不能为0,因此builder.charAt(0)=='0'需要全部移除
        while(builder.length() != 0 && builder.charAt(0) == '0'){
            builder.deleteCharAt(0);
        }
 
        System.out.println(builder.toString());
    }
}
  1. 奇安信笔试:编辑和回退:输入一个字符串,当遇到"undo"时,删除前一个字符串,当遇到"redo"时,恢复上一个删除的字符串。没做出来,只过了20%,贴一个大佬的80%的答案。
import java.util.*;
 
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] str = sc.nextLine().split(" ");
 
        //双栈维护弹出和恢复
 
        Stack redo = new Stack<>();
        Stack undo = new Stack<>();
 
        for(int i=0;i
  1. 贝壳笔试:回文串构造:求一个字符串最少修改多少次能变成回文串
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        String len = sc.nextLine();
        String str = sc.nextLine();
 
        int result = 0;
        //双指针
        int left = 0;
        int right = str.length()-1;
 
        while(left <= right){
            if(str.charAt(left++) != str.charAt(right--)){
                result++;
            }
        }
        System.out.println(result);
    }
}

你可能感兴趣的:(秋招笔试惨痛经历之——字符串)