美团
/** * 输入数组的元素只能为 1或-1 * 求解乘积为1的子串 * 4 * 1 1 -1 -1 * 10 * // 前缀积 * public static void main(String[] args) { * Scanner sc = new Scanner(System.in); * int n = sc.nextInt(); * int[] nums = new int[n]; * for (int i = 0; i < n; i++) nums[i] = sc.nextInt(); * // 前缀积 [0, i] * for (int i = 1; i < n; i++) nums[i] *= nums[i-1]; * * int ans = 0; * for (int i = 0; i < n; i++) { * for (int j = i; j < n; j++) { * if (nums[j] * (i > 0? nums[i-1]: 1) == 1) ans ++; * } * } * System.out.println(ans); * } * */
/** * n个房间 m秒 * 接下来m个数字代表第i秒 炸弹在哪个房间爆炸 * dp[i][j]表示第i秒在第j个房间所消耗的能量 * 2 4 * 2 1 1 2 * 2 * * 3 10 * 2 3 1 3 2 1 1 2 3 1 * * static int inf = (int)(1e9+5); * public static void main(String[] args) { * Scanner sc = new Scanner(System.in); * int n = sc.nextInt(); * int m = sc.nextInt(); * int[] arr = new int[m+1]; * for (int i = 1; i <= m; i++) arr[i] = sc.nextInt(); * int[][] dp = new int[m+1][n+1]; * for (int i = 0; i <= m; i++) Arrays.fill(dp[i], inf); * dp[0][1] = 0; * for (int i = 1; i <= m; i++) { * for (int j = 1; j <= n; j++) { * if (arr[i] == j) dp[i][j] = inf; * else dp[i][j] = Math.min(dp[i][j], Math.min(dp[i-1][j], dp[i-1][arr[i]] + 1)); // 不跳 or 跳 * } * } * Arrays.sort(dp[m]); * System.out.println(dp[m][0]); * } */
/** * 节点范围n, 1 <=n<= 10000 * 邻接表建图 * 6 * 1 0 1 1 0 0 0为白色节点 1为黑色节点 * 0 1 2 1 4 4 * 3 2
* 若白色节点 没有子节点或者至少有一个黑色节点 则为好节点
* 若黑色节点 没有子节点或者所有节点全为白色节点 则为好节点 */ public class Main { static int N = 10005, M = 2 * 10005; static int[] head = new int[N]; static int[] edge = new int[M]; static int[] next = new int[M]; static int root, r1, r2; static int idx = 0; static void add(int x, int y) { // x -> y 头插法 edge[idx] = y; next[idx] = head[x]; head[x] = idx++; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] color = new int[n+1]; for (int i = 1; i <= n; i++) color[i] = sc.nextInt(); Arrays.fill(head, -1); for (int i = 1; i <= n; i++) { int from = sc.nextInt(); if (from == 0) root = i; else add(from ,i); } dfs(color, root); System.out.println(r1 + " " + r2); } public static void dfs(int[] color, int node) { // 处理当前节点 if (color[node] == 0) { // 如果是白色节点 int size = 0, black = 0; for (int i = head[node]; i != -1; i = next[i]) { size++; int to = edge[i]; if (color[to] == 1) black++; } if (size == 0 || black >= 1) r1++; } else { // 如果为黑色节点 int size = 0, white = 0; for (int i = head[node]; i != -1; i = next[i]) { size ++; int to = edge[i]; if (color[to] == 0) white++; } if (size == 0 || white == size) r2++; } // 处理子节点 for (int i = head[node]; i != -1; i = next[i]) { int to = edge[i]; dfs(color, to); } } }
// https://www.lintcode.com/problem/386/description
public class Solution {
/**
* @param s: A string
* @param k: An integer
* @return: An integer
*/
public int lengthOfLongestSubstringKDistinct(String s, int k) {
// write your code here
// 使用Map记录(字符,出现次数)
Map<Character, Integer> map = new HashMap<>();
int sx = 0, sy = 0;
int left = 0, right = 0;
int maxLen = 0;
for (left = 0; left < s.length(); left ++) {
while (right < s.length()) {
char c = s.charAt(right);
if (map.containsKey(c)) {
map.put(c, map.get(c) + 1);
} else {
if (map.size() == k) {
break;
} else {
map.put(c, 1);
}
}
right ++;
}
if (maxLen < right - left) {
maxLen = right - left;
sx = left;
sy = right;
}
// maxLen = Math.max(maxLen, right - left);
// left左移
char c = s.charAt(left);
if (map.containsKey(c)) {
if (map.get(c) == 1) {
map.remove(c);
} else {
map.put(c, map.get(c) - 1);
}
}
}
System.out.println(s.substring(sx, sy)); // 也可以把结果打印一下
return maxLen;
}
}
作者:Ryanjie
出处:http://www.cnblogs.com/ryanjan/
本文版权归作者和博客园所有,欢迎转载。转载请在留言板处留言给我,且在文章标明原文链接,谢谢!
如果您觉得本篇博文对您有所收获,觉得我还算用心,请点击右下角的 [推荐],谢谢!