acwing-93. 递归实现组合型枚举
acwing-93. 递归实现组合型枚举
12345678910111213141516171819202122232425262728293031323334import java.util.*;public class Main{ static List<List<Integer>> res = new ArrayList<>(); public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); dfs(1, n, m, new ArrayList<>()); for(List<Integer> list : res){ for(int i = 0; i < list.size( ...
acwing-92. 递归实现指数型枚举
acwing-92. 递归实现指数型枚举
123456789101112131415161718192021222324252627282930313233import java.util.*;public class Main{ static List<List<Integer>> res = new ArrayList<>(); public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); dfs(1, n, new ArrayList<>()); System.out.println(""); for(List<Integer> list : res){ for(int i = 0; i < list.size(); i+ ...
acwing-91. 最短Hamilton路径
acwing-91. 最短Hamilton路径
123456789101112131415161718192021222324252627282930313233343536373839404142434445import java.util.*;public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[][] weight = new int[n][n]; // 写入数据 for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ weight[i][j] = sc.nextInt(); } } ...
leetcode-1006. 笨阶乘
1006. 笨阶乘
12345678910111213141516171819202122232425262728293031323334353637383940class Solution { public int clumsy(int N) { int k = N % 4; int m = N / 4; int ans = 0; boolean bool = true; while (m > 0) { int a = Math.floorDiv(N * (N - 1), N - 2); int b = N - 3; ans -= a; if (bool) { ans = Math.abs(ans); bool = false; } ans += b; ...
acwing-90. 64位整数乘法
acwing-90. 64位整数乘法
12345678910111213141516171819202122import java.util.*;public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); long a = sc.nextLong(); long b = sc.nextLong(); long p = sc.nextLong(); System.out.println(multiply(a, b, p)); } public static long multiply(long a, long b, long p){ long ans = 0 % p; while(b > 0){ if((b&1) == 1){ ...
leetcode-190. 颠倒二进制位
190. 颠倒二进制位
123456789101112public class Solution { // you need treat n as an unsigned value public int reverseBits(int n) { int ans = 0; for (int i = 0; i < 32; i++) { ans = ans << 1; ans = (n&1) | ans; n = n >> 1; } return ans; }}
leetcode-90. 子集 II
90. 子集 II
12345678910111213141516171819202122class Solution { List<List<Integer>> ans = new ArrayList<>(); Set<List<Integer>> set = new HashSet<>(); public List<List<Integer>> subsetsWithDup(int[] nums) { Arrays.sort(nums); dfs(0, nums, new ArrayList<>()); return ans; } public void dfs(int cur, int[] nums, List<Integer> list){ if (!set.contains(list)){ List& ...
acwing-89. a^b
acwing-89. a^b
12345678910111213141516171819202122232425import java.util.*;public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int p = sc.nextInt(); System.out.println(power(a, b, p)); } public static long power(long a, long b, long p){ long ans = 1 % p; while(b > 0){ if((b&1) == 1){ ...
leetcode-1046. 最后一块石头的重量
1046. 最后一块石头的重量
12345678910111213141516171819202122class Solution { public int lastStoneWeight(int[] stones) { Queue<Integer> queue = new PriorityQueue<>((o1,o2)->{ return o2.compareTo(o1); }); for(int w :stones){ queue.offer(w); } while (queue.size() > 1){ int w1 = queue.poll(); int w2 = queue.poll(); if (w1 == w2){ continue; ...
leetcode-1190. 反转每对括号间的子串
1190. 反转每对括号间的子串
优化解法
123456789101112131415161718192021222324252627class Solution { public String reverseParentheses(String s) { Stack<Character> stack = new Stack<>(); for(int i = 0; i < s.length(); i++){ Character ch = s.charAt(i); if(ch == ')'){ StringBuffer sb = new StringBuffer(); // 此时将栈(括号前的取出 Character tmp; while((tmp = stack.pop()) != '( ...