第二题
参考8月26日华为笔试第三题中,大佬的题解(https://www.nowcoder.com/discuss/489973?type=post&order=create&pos=&page=1&channel=666&source_id=search_post)
public class ali { static String ans; static int res = 0; public static void main(String[] args) { String[] strings = new String[] {"0000","1010","0101","1111"}; int[] nums = new int[] {2,2,2,2}; dfs(0, strings, nums,""); if(res != 1) { System.out.println(res); return; } System.out.println(ans); } private static void dfs(int i, String[] strings, int[] nums, String string2) { for(int temp : nums) { if(temp < 0 || temp > strings[0].length() - i) { return ; } } if(i == strings[0].length()) { ans = string2; res++; return; } int[] nums_0 = new int[nums.length]; int[] nums_1 = new int[nums.length]; for(int k = 0; k < strings.length; k++) { if(strings[k].charAt(i) == '0') { nums_0[k] = nums[k] - 1; nums_1[k] = nums[k]; }else { nums_0[k] = nums[k]; nums_1[k] = nums[k] - 1; } } dfs(i+1, strings, nums_0, string2+"0"); dfs(i+1, strings, nums_1, string2+"1"); } }
全部评论
(0) 回帖