测试开发备战秋招面试16-牛客刷题之字符串

努力了那么多年,回头一望,几乎全是漫长的挫折和煎熬。对于大多数人的一生来说,顺风顺水只是偶尔,挫折、不堪、焦虑和迷茫才是主旋律。我们登上并非我们所选择的舞台,演出并非我们所选择的剧本。继续加油吧!

目录

1、 字符串变形

2、最长公共前缀

3、验证IP地址

4、大数加法


1、 字符串变形

题目链接:字符串变形_牛客题霸_牛客网

思路:按空格划分字符串,然后反转,对每个字符大小写转换,最后拼接在一起。

python版:

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param s string字符串 
# @param n int整型 
# @return string字符串
#
class Solution:
    def trans(self , s: str, n: int) -> str:
        # write code here
        s1 = ""
        l = s.split(' ')
        x = l[::-1]
        for i in x:
           tmp = i.swapcase()
           s1 += tmp
           s1 += " "    
        return s1[0:len(s1)-1] 

2、最长公共前缀

题目链接:最长公共前缀_牛客题霸_牛客网
思路:有第一个字符串依次和后面的比对即可,得到新的前缀继续比对。

Java版:

import java.util.*;


public class Solution {
    /**
     * 
     * @param strs string字符串一维数组 
     * @return string字符串
     */
    public String longestCommonPrefix (String[] strs) {
        // write code here
        if(strs.length == 0){
            return "" ;
        }
        String prefix = strs[0] ;
        for(int i=1; i<strs.length; i++){
            prefix = bestLongCommon(prefix, strs[i]) ;
            if(prefix.length() == 0){
                return "" ;
            }
        }
        return prefix ;
    }
    public String bestLongCommon(String s1, String s2){
        String ans = "" ;
        for(int i=0; i<Math.min(s1.length(),s2.length()); i++){
            if(s1.charAt(i) == s2.charAt(i)){
                ans += s1.charAt(i) ;
            }else{
                return ans ;
            }
        }
        return ans ;
    }
}

3、验证IP地址

题目链接:验证IP地址_牛客题霸_牛客网

思路:写两个方法分别依次比对字符串是否满足ipv4和ipv6,满足返回true,不满足返回false。

Java版:

import java.util.*;


public class Solution {
    /**
     * 验证IP地址
     * @param IP string字符串 一个IP地址字符串
     * @return string字符串
     */
    public String solve (String IP) {
        // write code here
        if(isIp4(IP)){
            return "IPv4" ;
        }else if(isIp6(IP)){
            return "IPv6" ;
        }else{
            return "Neither" ;
        }
    }
    public boolean isIp4(String ip){
        if(ip.indexOf(".") == -1){
            return false ;
        }
        String [] s = ip.split("\\.") ;
        if(s.length != 4){
            return false ;
        }
        for(int i=0; i<s.length; i++){
            if(s[i].length() <= 0 || s[i].length()>4 || (s[i].charAt(0) == '0' && s[i].length() > 1)){
                return false ;
            }
            int num = 0 ;
            for(int j=0; j<s[i].length(); j++){
                char c = s[i].charAt(j) ;
                if(c<'0' || c>'9'){
                    return false ;
                }
                num = num * 10 + (c - '0') ;
                if(num<0 || num>255){
                    return false ;
                }
            }
        }
        return true ;
    }
    public boolean isIp6(String ip){
        if(ip.indexOf(":") == -1){
            return false ;
        }
        String [] s = ip.split(":", -1) ;
        if(s.length != 8){
            return false ;
        }
        for(int i=0; i<s.length; i++){
            if(s[i].length() <=0 || s[i].length()>4){
                return false ;
            }
            for(int j=0; j<s[i].length(); j++){
                char c = s[i].charAt(j) ;
                boolean flag = (c>='a' && c<='f') || (c>='A' && c<='F') || (c>='0' && c<='9') ;
                if(!flag){
                    return false ;
                }
            }
        }
        return true ;

    }
}

4、大数加法

题目链接:大数加法_牛客题霸_牛客网

思路:从后向前遍历,模拟两个数字的相加过程,从字符串拼接起来,最后反转即可。
Java版:

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * 计算两个数之和
     * @param s string字符串 表示第一个整数
     * @param t string字符串 表示第二个整数
     * @return string字符串
     */
    public String solve (String s, String t) {
        // write code here
        if(s.length()==0){
            return t ;
        }
        if(t.length() == 0){
            return s ;
        }
        if(s.length() > t.length()){
            String tmp = s ;
            s = t  ;
            t = tmp ;
        }
        StringBuilder sb = new StringBuilder("") ;
        int sum = 0 , temp = 0, j = t.length() - 1 ;
        for(int i=s.length()-1; i>=0; i--){
            sum = temp ;
            sum += (s.charAt(i) - '0') + (t.charAt(j--) - '0') ;
            if(sum >= 10){
                temp = sum / 10 ;
                sum = sum % 10 ;
            }else{
                temp = 0 ;
            }
            sb.append(sum) ;
        }
        int s1 = 0 ;
        if(j>=0){
            for(;j>=0 && temp>0; j--){
                s1 = s1 + (t.charAt(j) - '0')  + temp ;
                if(s1 >= 10){
                    temp = s1 / 10 ;
                    s1 = s1 % 10 ;
                }else{
                    temp = 0 ;
                }
                sb.append(s1) ;
            }
            sb.append(t.substring(0,j+1)) ;
        }
        if(temp > 0){
            sb.append(temp) ;
        }
        return sb.reverse().toString() ;

    }
}

猜你喜欢

转载自blog.csdn.net/nuist_NJUPT/article/details/131076518