2020 autumn Ctrip recruit written in java programming problem

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/Miaoshuowen/article/details/101626623

The second question

Here Insert Picture DescriptionThinking : Whenever recording his position (variable count record, initially 0) encounters a left parenthesis, plus one count encountered left parenthesis, count minus one encounter a right parenthesis, left bracket until it encounters at this time when the matching right parenthesis (if 0, and when this time has experienced count is equal to a right parenthesis); in this case the exchange (all the elements of the first element pair in parentheses switching position and the last element, the second element and the penultimate two switching, sequentially switching all the elements in parentheses End), when faced with changes in parentheses right parenthesis left parenthesis, right parenthesis becomes left bracket; then recursively exchange the contents of the second bracket; string returned by regular expression without the brackets can be exported.

Code:

public class Main2 {

	/*
	 * 请完成下面这个函数,实现题目要求的功能 当然,你也可以不按照下面这个模板来作答,完全按照自己的想法来 ^-^ 开始写代码
	 ******************************/
	@SuppressWarnings("null")
	static String resolve(String expr) {

		if (expr == null || expr.length() == 0) {
			return expr;
		}
		char[] s = expr.toCharArray();
		resolveimpl(s, 0, s.length);
		expr = String.valueOf(s);

		String result = expr.replaceAll("[()]", "");
		return result;
	}

	public static void swap(char[] s, int start, int end) {
		char tmp = 0;
		while (start < end) {
			if (s[start] == '(') {
				s[start] = ')';
			}
			if (s[end] == ')') {
				s[end] = '(';
			}
			tmp = s[start];
			s[start] = s[end];
			s[end] = tmp;
			start++;
			end--;
		}
	}

	public static void resolveimpl(char[] s, int start, int end) {
		if (s == null || s.length == 0) {
			return;
		}
		int sta1 = 0;
		int en1 = 0;
		int count = 0;
		for (int i = start; i < end; i++) {

			int count1 = -1;
			if (s[i] == '(') {
				count++;
				if (count == 1) {
					sta1 = i;
				}
			}
			if (s[i] == ')') {
				count--;
				if (count == 0) {
					count1 = 0;
					en1 = i;
				}
			}

			if (count == 0 && count1 == 0) {

				swap(s, sta1, en1);
				resolveimpl(s, ++sta1, en1--);
			}
		} // ((ab)d(ef)) 测试用例
	}

	/****************************** 结束写代码 ******************************/

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String res;
		String _expr;
		try {
			_expr = in.nextLine();
		} catch (Exception e) {
			_expr = null;
		}

		res = resolve(_expr);
		System.out.println(res);
	}
}

The third question

Did not take pictures, it is intended to recall the title
input n represents an integer number of threads, and then enter an integer m represents the number of tasks, the next of length m input array, each element represents the time required to complete the corresponding task (in seconds), each threads run in parallel, but each thread task must be connected in order to complete the final output minimum time required for these tasks.

** thinking: ** greedy algorithm, write a method indicates whether these tasks can be completed in time t, 0 <t <= total time to complete all the tasks required. Then binary search shrinking range t until you find the minimum time.

Code:

public class Main3 {
	public int temp(int thread, int task, int[] times) {
		int start = 0;
		int end = 0;
		for (int k : times) {
			end = end + k;
		}
		return mintime(start, end, thread, task, times);
	}

	public boolean isover(int t, int thread, int task, int[] times) {
		int T = 0;
		int j = 0;
		for (int i = 0; i < thread; i++) {
			T = times[j];
			while (j < task && T <= t) {
				j++;
				if (j < task) {
					T = T + times[j];
				}
				if (j == task) {
					return true;
				}
			}
		}
		return false;
	}

	public int mintime(int start, int end, int thread, int task, int[] times) {
		
		int mid = 0;
		while (start < end) {
			System.out.println("end=" + end + "start=" + start);
			mid = (start + end) / 2;
			if (isover(mid, thread, task, times)) {
				end = mid;
			} else {
				start = mid;
				if (start + 1 == end) {
					return end;
				}
			}
		}
		return mid;
	}

	public static void main(String[] args) {

		Main3 test = new Main3();
		int[] times = { 5, 12, 17, 4 };
		int thread = 3;
		int task = times.length;
		
		System.out.println(test.temp(thread, task, times));

	}
}

Guess you like

Origin blog.csdn.net/Miaoshuowen/article/details/101626623