2019小红书笔试题

编程

  • 题目一:
  • 题目二:

题目一:

输入一串字符,请编写一个字符串压缩程序,将字符串中连续出现的重复字母进行压缩,并输出压缩后的字符串。
例如:
aac 压缩为 1ac
xxxxyyyyyyzbbb 压缩为 3x5yz2b

java实现

package xiaohongshu;

import com.sun.corba.se.impl.orbutil.concurrent.Sync;

/**
 * 输入一串字符,请编写一个字符串压缩程序,将字符串中连续出现的重复字母进行压缩,并输出压缩后的字符串。
 * 例如:
 * aac 压缩为 1ac
 * xxxxyyyyyyzbbb 压缩为 3x5yz2b
 */
public class Solodity_String {
     
    private static Solodity_String solodity_string = new Solodity_String();

    public static Solodity_String getSolodity_string() {
     
        return solodity_string;
    }

    public Solodity_String() {
     
    }

    public void get_bian(String content){
     
        StringBuilder  ds  = new StringBuilder();
        int count = 0;
        if(content.length()>0) {
     
            char one = content.charAt(0);
            for(int i=1;i<content.length();i++){
     
                if(content.charAt(i) == one){
     
                    count++;
                }else{
     
                    if(count != 0 ){
     
                        ds.append(count);
                    }
                    ds.append(one);
                    one = content.charAt(i);
                    count = 0;
                }
            }
            ds.append(count);
            ds.append(one);
            System.out.println(ds);

        }

    }
    void ceshi_sstrubg(){
     
        String ds = new String();
        for(int i = 0 ;i < 10 ;i++){
     
            ds = ds + "dds";
        }
    }
}


python实现

def get_bian(content):
    count:int = 0
    ds:string = ""
    if(len(content)>0):
        one = content[0]
        for i in range(1,len(content)):
            if(content[i] == one):
                count = count+1
            else:
                if(count != 0):
                    ds = ds + str(count)
                ds = ds + one
                one = content[i]
                count = 0
        ds += str(count)
        ds += one
        print(ds)


题目二:

计算任意一个正整数n(0

java实现:

package xiaohongshu;
/**
 * 计算任意一个正整数n(0
public class Count_One_Number {
     
    private char[] num_array;
    int count = 0;
    private static Count_One_Number ourInstance = new Count_One_Number();

    public static Count_One_Number getInstance() {
     
        return ourInstance;
    }

    private Count_One_Number() {
     
    }
    public void get_one_number(String number){
     
         num_array = number.toCharArray();
         for (int i = 0 ; i <num_array.length ; i++){
     
             if(num_array[i] == '1'){
     
                 count++;
             }
         }
         System.out.println(count);
    }
}

python实现:

def count_number(content):
    count:int = 0
    for i in range(0,len(content)):
        if ( content[i] == '1'):
            count += 1
    print(count)

你可能感兴趣的:(面试题)