牛客杂记——6.1

基础知识

1、在基本JAVA类型中,如果不明确指定,整数型默认为int类型,带小数的默认为是double类型
2、在不考虑反射的情况下,私有访问控制限制符private修饰成员变量只能被该类自身所访问和修改
3、Math.round(11.5)=12 Math.round(-11.5)=-11
Math.round()方法,括号里面的数+0.5后向下取值。例如Math.round(3.4)=3,3.4+0.5=3.9,但是没有到4所以向下取值为3
4、方法通常存储在进程中的方法区
5、要使对象具有序列化能力其类要实现java.io.Serializable接口

编程

统计回文

链接:https://www.nowcoder.com/questionTerminal/9d1559511b3849deaa71b576fa7009dc
来源:牛客网

“回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。花花非常喜欢这种拥有对称美的回文串,生日的时候她得到两个礼物分别是字符串A和字符串B。现在她非常好奇有没有办法将字符串B插入字符串A使产生的字符串是一个回文串。你接受花花的请求,帮助她寻找有多少种插入办法可以使新串是一个回文串。如果字符串B插入的位置不同就考虑为不一样的办法。

例如:
A = “aba”,B = “b”。这里有4种把B插入A的办法:

  • 在A的第一个字母之前: “baba” 不是回文
  • 在第一个字母‘a’之后: “abba” 是回文
  • 在字母‘b’之后: “abba” 是回文
  • 在第二个字母’a’之后 “abab” 不是回文
    所以满足条件的答案为2
    在这里插入图片描述
    提供两种方法,第二种可能要麻烦些。
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String str1 = sc.nextLine();
            String str2 = sc.nextLine();
            int len = str1.length();
            int n = 0;
            for (int i = 0; i <= len; i++) {
                String str4 = str1.substring(0, i) + str2 + str1.substring(i);
                String str5 = new StringBuffer(str4).reverse().toString();
                if (str4.equals(str5)) {
                    n++;
                }
            }
            System.out.println(n);
        }
    }
}

import java.util.Scanner;
public class Main{
        public static void main (String[] args) {
        Scanner in = new Scanner(System. in) ;
        String A = in.nextLine ();
        Scanner in1 = new Scanner (System. in);
        String B = in.nextLine();
        int count = 0;
        if(A == null && B == null) {
            System.out.println(count);
            return;
        } else if (A != null && B != null) {
        count = add(A, B) ;
        }else if (A == null)  {
              A=B;
     StringBuffer C = new StringBuffer (A) ;
   int k = C.length();
   if (justHuiwen(C,k)) {
       count++;
   }
        }else if (B == null) {
     StringBuffer C = new StringBuffer (A) ;
        int k = C.length() ;
    if (justHuiwen(C,k)) {
        count++;

    }
        }
            System.out.println(count);
        }
   public static  int add(String A, String B) {

       int i = A.length() ;
        int count = 0;
        StringBuffer C = null;
        for(int j = 0;j< i+1;j++) {
            StringBuffer stringBuffer = new StringBuffer(A);
             C = stringBuffer.insert(j, B);
            int k = C.length();
            if (justHuiwen(C, k)) {
                count++;
            }
        }
            return count;
        }
        public static boolean justHuiwen (StringBuffer C, int length) {
        for (int i = 0; i < length/2 ; i++) {
        if (C.charAt(i)  != C.charAt (length -1-i)){
            return false;
        }
        }
            return true;
        }
}

第K大

有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数。

给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在。

测试样例:

在这里插入图片描述
两种方法,第一种分治,第二种直接排完

import java.util.*;

public class Finder {
    public int findKth(int[] a, int n, int K) {
        return findKth(a, 0, n-1, K);
    }
    public int findKth(int[] a, int low, int high, int k) {
        int part = partation(a, low, high);
        if(k == part - low + 1) return a[part];
        else if(k > part - low + 1) return findKth(a, part + 1, high, k - part + low -1);
        else return findKth(a, low, part -1, k);
    }
    public int partation(int[] a, int low, int high) {
        int key = a[low];
        while(low < high) {
            while(low < high && a[high] <= key) high--;
            a[low] = a[high];
            while(low < high && a[low] >= key) low++;
            a[high] = a[low];
        }
        a[low] = key;
        return low;
    }
    
}
import java.util.*;
public class Finder {
public int findKth(int[] a, int n, int K) {
// write code here
int i = 0;
i = n-K;
quickSort(a);
int b = 0;
b = a[i];
return b;
}
public void quickSort(int[] a){
quickHelper(a,0,a.length - 1);
}
public void quickHelper(int[] a, int left, int right) {
if(left >= right){
return;
}
int index = patition(a, left , right);
quickHelper(a, left , index - 1);
quickHelper(a, index + 1,right);
}
public int patition(int[] a, int left, int right){
int base = a[right];
int i = left;
int j = right;
while(i < j ){
while(i < j && a[i] <= base){
i++;
}
while (i < j && a[j] >= base){
j--;
}
if(i < j){
swap(a,i,j);
}
}
swap(a,i,right);
return i;
}
public void swap(int[] a, int i, int j){
int tmp = a[i];
    a[i] = a[j];
a[j] = tmp;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值