每日练习8.15

1.下面程序的输出为:

class Test {
	private int data;
	int result = 0;
	public void m() {
		result += 2;
		data += 2;
	System.out.print(result + " " + data);
	}
}
class ThreadExample extends Thread {
	private Test mv;
	public ThreadExample(Test mv)  {
	this.mv = mv;
	}
	public void run() {
		synchronized(mv){
			mv.m();
		}
	}
}
class ThreadTest {
	public static void main(String args[]) {
		Test mv = new Test();	// 声明并初始化对data赋默认值 
		Thread t1 = new ThreadExample(mv);
		Thread t2 = new ThreadExample(mv);
		Thread t3 = new ThreadExample(mv);
		t1.start();
		t2.start();
		t3.start();
	}
}

2 24 46 6
[原因]:使用synchronized关键字加同步锁,三个线程依次执行m()

编程题1
链接: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 {
    //判断字符串是否为回文
    //一个指针从前向后扫描,一个从后往前扫描,如果同一时刻两个指针指向的字符不相等,返回false
    //直到i==j,证明字符串是回文串
    public static boolean isHuiWen(String str){
        int i = 0;
        int j = str.length()-1;
        while (i<j){
            if (str.charAt(i)!=str.charAt(j)){
                return false;
            }
            i++;
            j--;
        }
        return true;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str1 = scanner.nextLine();
        String str2 = scanner.nextLine();
        int count = 0;
       
        for (int i=0;i<=str1.length();i++){
            StringBuilder sb = new StringBuilder(str1);
            sb.insert(i,str2);//在指定位置插入字符串
            if (isHuiWen(sb.toString())){
                count++;
            }
        }
        System.out.println(count);
    }
}

编程题2
快排思想找第k大的数

思路:
左边的数均比基准值大,右边的均比基准值小
若基准值刚好为第k大的数,那么他前面有k-1个比它大的数(p-low+1 == k),返回a[p]
(p-low+1 > k),从左边找
(p-low+1 < k),从右边找

public class test2 {
     public static int partition(int[] a,int left,int right){
         int key = a[right];
         int key_index = right;
         while (left<right){
             while (left<right && a[left]>=key){
                 left++;
             }
             while (left<right && a[right]<=key){
                 right--;
             }
             if (left<right){
                 int temp = a[left];
                 a[left] = a[right];
                 a[right] = temp;
             }
         }
         if (left!=key_index) {
             int temp = a[left];
             a[left] = a[key_index];
             a[key_index] = temp;
         }
         return left;
     }

    public static int findkth(int[] a, int low, int high, int k) {
        int p = partition(a, low, high);
        if (p - low + 1 == k) {
            return a[p];
        } else if (p - low + 1 > k) {
            return findkth(a, low, p - 1, k);
        } else
            return findkth(a, p + 1, high, k - p + low - 1);
    }

    public static void main(String[] args) {
        int a[] = {45, 76, 23, 90, 65, 100, 45, 99};
        System.out.println(findkth(a, 0, a.length - 1, 2));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值