腾讯实习生笔试题

1、构造回文

é¢ç®

package com.lyz.dataStructure.niuke;

import java.util.Scanner;

/**
 *@Author:759057893@qq.com Lyz
 *@Date: 2019/4/2 22:07
 *@Description:
 **/

/*
解题思路:
  (1)把字符串旋转形成另外一个字符串,称为旋转字符串;
  (2)求原字符串s1与旋转字符串s2中,最长公共子串的长度;
  (3)删除的字符数目 = 原字符串的长度 - 最长公共子串的长度。
需要解决的子问题:
   求两个字符串s1和s2中最长公共子串的长度。
   子问题的求解方式:动态规划
     设 MaxLen(i,j)表示s1左边i个字符与s2左边j个字符的最长公共子串长度,则子问题的解为MaxLen(strlen(s1),strlen(s2));
     MaxLen(i,j)的求解方式为:
       若s1第i个字符与s2第j个字符相匹配,则 return 1+MaxLen(i-1,j-1);
       否则:return max(MaxLen(i-1,j),MaxLen(i,j-1))
    边界条件:
    MaxLen(i,n)=0; for n in 0 to strlen(s2)
    MaxLen(n,j)=0; for n in 0 to strlen(s1)
*/
public class HuiWen {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入字符串:");
        while (input.hasNext()) {
            String s1 = input.next();
            //求反转之后的字符串
            String s2 = new StringBuilder(s1).reverse().toString();
            //定义一个二维数组,然后遍历,求出最长公共子串
            int[][] dp = new int[s1.length() + 1][s2.length() + 1];

            for (int i = 1; i < dp.length; i ++ ) {
                for (int j = 1; j < dp.length; j ++ ) {
                    //如果i行的下标和j行的下标一样
                    if(s1.charAt(i - 1) == s2.charAt(j - 1)){
                        dp[i][j]=dp[i - 1][j - 1] + 1;
                    }else{
                        dp[i][j]= Math.max(dp[i - 1][j], dp[i][j - 1]);
                    }
                }
            }
            System.out.println(s1.length() - dp[s1.length()][s2.length()]);
        }
    }
}





2、字符移位

package com.lyz.dataStructure.niuke;

import java.util.Scanner;

/**
 *@Author:759057893@qq.com Lyz
 *@Date: 2019/4/3 16:55
 *@Description:
 **/

/*

小Q最近遇到了一个难题:把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,且不能申请额外的空间。
你能帮帮小Q吗?
 */

/*    public class StringMain {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.println("请输入字符串");
            while (input.hasNext()) {
                String string = input.nextLine();
                char [] chars = string.toCharArray();
                int length = string.length();
                //冒泡排序
                for (int i = 0; i < length-1; i ++) {
                    for (int j = 0; j < length - i-1; j ++) {
                        if ((chars[j] >= 'A' && chars[j] <= 'Z') &&
                                (chars[j+1] < 'A' || chars[j+1] > 'Z')) {
                            char temp = chars[j];
                            chars[j] = chars[j+1];
                            chars[j+1] = temp;
                        }
                    }
                }
                System.out.println(String.valueOf(chars));
            }
        }
    }*/

public class StringMain{
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println("请输入字符串:");
        while(input.hasNext()){
            String str = input.nextLine();
            char[] chars = str.toCharArray();
            int length = str.length();
            //冒泡排序
            for(int i=0;i<length-1;i++){
                for(int j = 0;j<length-i-1;j++){
                    if((chars[j]>='A'&&chars[j]<='Z')&&(chars[j+1]>'Z')){
                        char tmp = chars[j+1];
                        chars[j+1] = chars[j];
                        chars[j] = tmp;
                    }
                }
            }
            System.out.println(String.valueOf(chars)); //将数组装换为字符串
        }

    }
}




3、翻转数列

public class StringDemo2 {
    public static void main(String args []) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        int m = sc.nextInt();
        System.out.println((n / (m << 1)) * (m * m));
    }
}

 

public class StringDemo3{
    public static void main(String args[]){
        Scanner in=new Scanner(System.in);
        int n =in.nextInt();
        long[] a=new long [100];
        long x=0,y=0;
        int i,j;
        for ( i=0;i<n;i++)
            a[i]=in.nextLong();

        for( i=0;i<n;i++){//外层循环控制排序趟数
            for( j=0;j<n-1-i;j++){//内层循环控制每一趟排序多少次
                if(a[j]<a[j+1]){
                    long temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                }
            }
        }
        //System.out.println("排序后如下");
        //for ( i=0;i<n;i++)
        //System.out.println(a[i]);
        for (i=1;i<=n/2;i++){
            x=x+a[i*2];
            y=y+a[i*2-1];
        }
        x=x+a[0];
        System.out.println(x-y);
    }
}

 

/*
小Q的父母要出差N天,走之前给小Q留下了M块巧克力。
小Q决定每天吃的巧克力数量不少于前一天吃的一半,
但是他又不想在父母回来之前的某一天没有巧克力吃,
请问他第一天最多能吃多少块巧克力
 */
public class StringDemo4{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int day = scan.nextInt();
        int number = scan.nextInt();
        System.out.print(fun(day,number));
    }

    //计算第一天吃s个巧克力一共需要多少个巧克力
    public static int sum(int s, int n, int m){
        int sum=0;
        for(int i=0;i<n;i++){
            sum+=s;
            s=(s + 1)/2;//向上取整
        }
        return sum;
    }
    //二分查找
    public static int fun(int n, int m){
        if(n==1) return m;
        int low=1;
        int high=m;//第一天的巧克力一定是大于等于1小于等于m的
        while(low<=high){
            int mid=(low+high + 1)/2;//向上取整
            if(sum(mid, n, m)==m) return mid;//如果第一天吃mid个巧克力,刚刚好吃完所有巧克力,那么直接返回
            else if(sum(mid, n, m)<m){
                low=mid+1;
            }else{
                high=mid-1;
            }
        }
        return high;
    }
}

public class StringDemo5{
    public static final int ASD = 1000000007;

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int k=sc.nextInt();
        int a=sc.nextInt(), x=sc.nextInt();
        int b=sc.nextInt(), y=sc.nextInt();
        int[] dp = new int[k+1];
        dp[0] = 1;
        for(int i=0; i<x ; i++){
            for(int j=k; j>=a; j--){
                dp[j] = (dp[j] + dp[j-a]) % ASD;
            }
        }

        for(int i=0; i<y ; i++){
            for(int j=k; j>=b; j--){
                dp[j] = (dp[j] + dp[j-b]) % ASD;
            }
        }

        System.out.println(dp[k]);
        sc.close();
    }
}

5

public class StringDemo6 {

    static class Pair {
        int time;
        int diff;

        public Pair(int time, int diff) {
            this.time = time;
            this.diff = diff;
        }
    }

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int machineNum = sc.nextInt();
            int taskNum = sc.nextInt();
            Pair[] machines = new Pair[machineNum];
            Pair[] tasks = new Pair[taskNum];
            for (int i = 0; i < machineNum; i++) {
                machines[i] = new Pair(sc.nextInt(), sc.nextInt());
            }
            for (int i = 0; i < taskNum; i++) {
                tasks[i] = new Pair(sc.nextInt(), sc.nextInt());
            }
            Comparator<Pair> cmp = new Comparator<Pair>() {

              @Override
                public int compare(Pair p1, Pair p2) {
                    if (p1.time == p2.time) {
                        return p2.diff - p1.diff;
                    } else {
                        return p2.time - p1.time;
                    }
                }

            };
            Arrays.sort(machines, cmp);
            Arrays.sort(tasks, cmp);
            long sum = 0;
            int count = 0;
            int j = 0;
            int[] dp = new int[101];
            for (int i = 0; i < taskNum; i++) {
                while (j < machineNum && machines[j].time >= tasks[i].time) {
                    dp[machines[j].diff]++;
                    j++;
                }
                for (int k = tasks[i].diff; k < 101; k++) {
                    if (dp[k] != 0) {
                        dp[k]--;
                        sum += 200 * tasks[i].time + 3 * tasks[i].diff;
                        count++;
                        break;
                    }
                }
            }
            System.out.println(count + " " + sum);
        }
        sc.close();

    }

}

public class main {

        private String[][] str;
        private int N;
        private int M;

        public static void main(String[] args) {
            // TODO Auto-generated method stub
            main test = new main();
            String inputStr = null;
            Scanner in = new Scanner(System.in);
            test.N = in.nextInt();
            test.M = in.nextInt();
            int cN = test.N;
            int cM = test.M;
            test.str = new String[cN][cM];
            for(int i = 0; i < cN; i++){
                if(in.hasNext())
                    inputStr = in.next();
                for(int j = 0; j < cM; j++){
                    test.str[i][j] = String.valueOf(inputStr.charAt(j));
                }
            }


            int num = test.getLeastOperation(test.str,cN,cM);

            System.out.println(num);

        }

        private int getLeastOperation(String[][] str, int n, int m){
            int num = 0;
            for(int i = 0; i < n; i++){
                for(int j = 0; j < m; j++){
                    if("Y".equals(str[i][j])){
                        clear_Y(i,j);     //消除Y
                        num++;
                    }else if("B".equals(str[i][j])){
                        clear_B(i,j);     //消除B
                        num++;
                    }else if("G".equals(str[i][j])){
                        clear_Y(i,j);     //消除Y
                        num++;
                        clear_B(i,j);     //消除B
                        num++;
                    }
                }
            }
            return num;
        }

        private void clear_Y(int i, int j){
            for(; j < M && i < N; j++, i++){
                if("Y".equals(str[i][j]))
                    str[i][j] = "X";
                else if("G".equals(str[i][j]))
                    str[i][j] = "B";
                else
                    break;
            }
        }

        private void clear_B(int i, int j){
            for(; j >= 0 && i < N; j--,i++){
                if("B".equals(str[i][j]))
                    str[i][j] = "X";
                else if("G".equals(str[i][j]))
                    str[i][j] = "Y";
                else
                    break;
            }
        }

    }

public class TestDeadLock implements Runnable{
    public int flag =1;
    static Object o1 = new Object();
    static Object o2 = new Object();

    public void run(){
        System.out.println("flag=" +flag);
        if(flag == 1){
            synchronized(o1){
                try{
                    Thread.sleep(500);
                }catch(Exception e){
                    e.printStackTrace();
                }
                synchronized(o2){
                    System.out.println("1");
                }
            }
        }
        if(flag == 0){
            synchronized(o2){
                try{
                    Thread.sleep(500);
                }catch(Exception e){
                    e.printStackTrace();
                }
                synchronized(o1){
                    System.out.println("0");
                }
            }
        }
    }
    public static void main(String[] args){
        TestDeadLock td1 = new TestDeadLock();
        TestDeadLock td2 = new TestDeadLock();
        td1.flag = 1;
        td2.flag = 0;
        Thread t1 = new Thread(td1);
        Thread t2 = new Thread(td2);
        t1.start();
        t2.start();
    }
}

 

 

public class BlockTest  {

    public static void main(String[] args) {
        final ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<Integer>();
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=0;i<3;i++){
                    queue.add(i);
                }
            }
        });
        try {
            thread.join();
            thread.start();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for(int i=0;i<15;i++){
            queue.take();
        }
    }

}

class ArrayBlockingQueue<T> {
    private Lock lock = new ReentrantLock();
    private Object[] item = new Object[4];
    private int startIndex,endIndex,count;
    private Condition notfull = lock.newCondition();
    private Condition notempty = lock.newCondition();
    public void add(T t){
        lock.lock();
        try{
            System.out.println("存放值"+t);
            while(count==item.length){
                try {
                    System.out.println("队列已满,阻塞put线程");
                    notfull.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            item[startIndex++] = t;
            count++;
            if(startIndex==item.length){
                startIndex=0;
            }
            notempty.signal();
        }finally{
            lock.unlock();
        }
    }

    public T take(){
        lock.lock();
        try{
            while(count==0){
                try {
                    System.out.println("队列空了,阻塞take线程");
                    notempty.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            T t =(T)item[endIndex++];
            System.out.println("取值"+t);
            count--;
            if(endIndex==item.length){
                endIndex = 0;
            }
            notfull.signal();
            return t;
        }finally{
            lock.unlock();
        }
    }
}

XSS就是通过在css或者html里嵌入一些恶意的Javascript代码,从而实现恶意攻击的目的。
在编写前端页面时,注意不要把任何来自用户的内容放到下面这些地方
script标签、html注释、标签名、属性、css值、
防范方法:
1、转义用户的内容 如对
a、HTML:对以下这些字符进行转义:
&:&amp; <:&alt;  >:&gt;  ':&#x27; ":&quot;  /:&#x2F;
b、Javascript:把所有非字母、数字的字符都转义成小于256的ASCII字符;
c、URL:使用Javascript的encodeURIComponent()方法对用户的输入进行编码,该方法会编码如下字符:,      /      ?     :     @     &     =     +     $     #
2、添加用户生成的内容
a、Javascript:使用textContent或者innerText,而不能使用innerHTML;
b、jquery:使用text(),而不能使用html();

 

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个IP地址(例如168.0.0.1)");
        String ss = sc.next();
        String[] split = ss.split("\\.");
        for (String s1 : split) {
            int i = Integer.parseInt(s1);/
            System.out.print(i);
        }
    }
}

public class CardsTest {

    public static void main(String[] args) {

        Cards cards = new Cards();

        Thread t1 = new Thread(cards);

        t1.start();
    }

}

class Cards implements Runnable {

    private Map<Integer, String> map = new HashMap<Integer, String>();
    private List<Integer> list = new ArrayList<Integer>();
    private TreeSet<Integer> mike = new TreeSet<Integer>();
    private TreeSet<Integer> qq = new TreeSet<Integer>();
    private TreeSet<Integer> john = new TreeSet<Integer>();
    private TreeSet<Integer> dipai = new TreeSet<Integer>();

    public void shuffle() {// 发牌
        String[] color = {"红桃", "黑桃", "梅花", "方块"};
        String[] numbers = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J",
                "Q", "K", "A"};
        int index = 0;
        for (String num : numbers) {
            for (String cc : color) {
                String concat = cc.concat(num);
                map.put(index, concat);
                list.add(index);
                index++;
            }
        }
        map.put(index, "小王");
        list.add(index);
        index++;
        map.put(index, "大王");
        list.add(index);
        Collections.shuffle(list);
        for (int i = 0; i < list.size(); i++) {
            if (i >= list.size() - 3) {
                dipai.add(list.get(i));
            } else if (i % 3 == 1) {
                qq.add(list.get(i));
            } else if (i % 3 == 2) {
                john.add(list.get(i));
            } else if (i % 3 == 0) {
                mike.add(list.get(i));
            }
        }
    }

    public void lookPuker(String name, TreeSet<Integer> ts,
                          Map<Integer, String> hm) {
        System.out.println(name + "共有 " + ts.size() + " 你的牌是:");
        for (Integer it : ts) {
            String value = hm.get(it);
            System.out.print(value + " ");
        }
        System.out.println();
    }

    @Override
    public void run() {
        shuffle();
        int a = (int) (Math.random() * 3 + 1);
        if (a == 1) {
            System.out.println("地主 张三");
            lookPuker("张三", mike, map);
            lookPuker("李四", qq, map);
            lookPuker("王五", john, map);
            lookPuker("底牌", dipai, map);
        } else if (a == 2) {
            System.out.println("地主 李四");
            lookPuker("李四", qq, map);
            lookPuker("张三", mike, map);
            lookPuker("王五", john, map);
            lookPuker("底牌", dipai, map);
        } else if (a == 3) {
            System.out.println("地主 王五");
            lookPuker("王五", john, map);
            lookPuker("李四", qq, map);
            lookPuker("张三", mike, map);
            lookPuker("底牌", dipai, map);
        }
    }
}

发生原因有如下情况:
1.可能是mysql.exe 的cpu占用率过高,
2.可能是其中一个进程特别耗CPU内存

定位:
首先输入top,查看占用资源较多的线程,接着使用jstack工具查看堆栈信息,根据堆栈信息来做判断
如果是mysql.exe情况  使用show processlist命令查找最频繁的sql语句 ,修改数据的参数设置,where条件后添加索引进行sql优化
如果在遇到上述的问题 就扩大内存,然后在解决相应的问题。

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

3分钟秒懂大数据

你的打赏就是对我最大的鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值