1.暴力就完事,瞎调,ac
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] s = in.nextLine().split(" "); int m = in.nextInt(); int count = 0; for (int i = 0;i<s.length;i++){ for (int j = 0;j<s.length;j++){ if (i!=j){ long x= Long.parseLong(s[i]); long y= Long.parseLong(s[j]); if ((x+y) <= m)count++; } } } System.out.println(count/2); } }2.invert函数花点时间,用斐波那契数列的思想,ac (核心代码模式放个Main,方便调试)
import java.util.Arrays; public class Main{ static char[] L = "abcdefghijklmnopqrstuvwxyz".toCharArray(); public static void main(String[] args) { System.out.println(findKthBit(4,11)); } private static char findKthBit (int n, int k) { String a = "a"; for (int i = 1;i < n;i++){ a = a+L[i]+invert(a); } return a.charAt(k); } private static String invert(String s) { StringBuilder temp = new StringBuilder(); for (int i = 0;i<s.length();i++){ char c = s.charAt(i); int start = 0,end = 25; while (c!=L[start++])end--; temp.append(L[end]); } return temp.reverse().toString(); } }
3.LeetCode135 分糖果 看到大佬解答才想起,忘记把第一个小朋友拿到后面去了,好气,没想到,调了调int/long还是0.8😅
正确应该是生成n+1的数组,把第一个放在最后。
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] age = in.nextLine().split(" "); int n = age.length; int[] age_n = new int[n]; long[] left = new long[n]; for (int i =0;i<n;i++)age_n[i] = Integer.parseInt(age[i]); for (int i =0;i<n;i++){ if (i>0&&age_n[i]>age_n[i-1])left[i]=left[i-1]+1; else left[i]=1; } int right = 0,ans = 0; for (int i = n-1;i>=0;i--){ if (i<n-1&&age_n[i]>age_n[i+1])right++; else right=1; ans += Math.max(left[i],right); } System.out.println(ans); } }
4.回溯+剪枝,看到有大佬说回溯0.83 加个判断input 长度(若1*1 返回 0)即可ac,也不知道正确与否,害,好气,忘记尝试了 (同样加个Main,方便调试)
public class Main{ static int[][] next = {{1,0},{0,1}}; static int min = Integer.MAX_VALUE; public static void main(String[] args) { int[][] input = {{1,1,1,1,0},{0,1,0,1,0},{1,1,2,2,2},{0,2,0,2,1}}; int r = 0,c = 0; int row = input.length,column = input[0].length; boolean[][] marked = new boolean[row][column]; int value = 0; //加个row column 1*1判断? if (input[0][0]==2) System.out.println(-1); else if (input[0][0]==1)value = -1; else value = -2; dfs(input,r,c,row,column,marked,value); System.out.println(min==Integer.MAX_VALUE ? -1 : min); } private static void dfs(int[][] input, int r, int c, int row, int column, boolean[][] marked, int value) { if (r >= row || c >= column || marked[r][c] || input[r][c]==2)return; if (input[r][c]==1)value += 1; else if (input[r][c]==0)value += 2; if (r==row-1&&c==column-1)min = Math.min(min,value); for (int[] i:next){ marked[r][c]=true; dfs(input,r+i[0],c+i[1],row,column,marked,value); marked[r][c]=false; } } }
问答题:软件工程问题,很迷。
全部评论
(0) 回帖