题目很简单,看下面代码
public List<List<Integer>> shiftGrid(int[][] grid, int k) {
List<List<Integer>> lists = new ArrayList<>();
for (int i = 0; i < grid[0].length; i++) {
List<Integer> list = new ArrayList<>();
for (int j = 0; j < grid.length; j++) {
list.add(grid[j][i]);
}
lists.add(list);
}
if (k == 0) {
return lists;
}
for (int i = 0; i < k; i++) {
List<Integer> list = lists.remove(lists.size() - 1);
Integer m = list.remove(list.size() - 1);
list.add(0, m);
lists.add(0, list);
}
List<List<Integer>> lists2 = new ArrayList<>();
for (int i = 0; i < lists.get(0).size(); i++) {
List<Integer> list = new ArrayList<>();
for (int j = 0; j < lists.size(); j++) {
list.add(lists.get(j).get(i));
}
lists2.add(list);
}
return lists2;
}
也比较容易,在构造方法里面直接就用DFS把这个树恢复好,用Set存一下遍历过程中的值。
class FindElements {
HashSet<Integer> set = new HashSet<>();
public FindElements(TreeNode root) {
if (root == null) {
return;
}
root.val = 0;
DFS(root);
}
private void DFS(TreeNode root) {
if (root == null) {
return;
}
set.add(root.val);
if (root.left != null) {
root.left.val = 2 * root.val + 1;
}
if (root.right != null) {
root.right.val = 2 * root.val + 2;
}
DFS(root.left);
DFS(root.right);
}
public boolean find(int target) {
return set.contains(target);
}
}
%3==1和%3==2
的元素存起来,然后对这两个列表排序。 public int maxSumDivThree(int[] nums) {
List<Integer> list1 = new ArrayList<>(nums.length);
List<Integer> list2 = new ArrayList<>();
int sum = 0;
for (int num : nums) {
if (num % 3 == 2) {
list2.add(num);
} else if (num % 3 == 1) {
list1.add(num);
}
sum += num;
}
Collections.sort(list1);
Collections.sort(list2);
if (sum % 3 == 0) {
return sum;
} else if (sum % 3 == 1) {
int a1 = Integer.MAX_VALUE, a2 = Integer.MAX_VALUE;
if (list1.size() > 0) {
a1 = list1.get(0);
}
if (list2.size() > 1) {
a2 = list2.get(0) + list2.get(1);
}
return sum - Math.min(a1, a2);
} else {
int a1 = Integer.MAX_VALUE, a2 = Integer.MAX_VALUE;
if (list2.size() > 0) {
a2 = list2.get(0);
}
if (list1.size() > 1) {
a1 = list1.get(0) + list1.get(1);
}
return sum - Math.min(a1, a2);
}
}
这个题我不会,我尝试用DFS 暴力暴出来,但是代码庞大且复杂。
我写吐了,下面是有问题的代码
int[] T = new int[2];
int[] B = new int[2];
int[] S = new int[2];
public int minPushBox(char[][] grid) {
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == 'T') {
T[0] = i;
T[1] = j;
} else if (grid[i][j] == 'B') {
B[0] = i;
B[1] = j;
} else if (grid[i][j] == 'S') {
S[0] = i;
S[1] = j;
}
}
}
boolean[][] isVisit = new boolean[grid.length][grid[0].length];
DFS(B[0], B[1], S[0], S[1], isVisit, grid);
return min;
}
int tem = 0;
int min = Integer.MAX_VALUE;
private void DFS(int bi, int bj, int si, int sj, boolean[][] isVisit, char[][] grid) {
if (bi < 0 || bi >= grid.length
|| bj < 0 || bj >= grid[0].length
|| grid[bi][bj] == '.'
|| isVisit[bi][bj]) {
return;
}
if (bi == T[0] && bj == T[1]) {
min = Math.min(tem, min);
return;
}
isVisit[bi][bj] = true;
if (canGo(bi, bj, bi + 1, bj, si, sj, grid, new boolean[grid.length][grid[0].length])) {
tem++;
DFS(bi - 1, bj, bi, bj, isVisit, grid);
tem--;
}
if (canGo(bi, bj, bi - 1, bj, si, sj, grid, new boolean[grid.length][grid[0].length])) {
tem++;
DFS(bi + 1, bj, bi, bj, isVisit, grid);
tem--;
}
if (canGo(bi, bj, bi, bj + 1, si, sj, grid, new boolean[grid.length][grid[0].length])) {
tem++;
DFS(bi, bj - 1, bi, bj, isVisit, grid);
tem--;
}
if (canGo(bi, bj, bi, bj - 1, si, sj, grid, new boolean[grid.length][grid[0].length])) {
tem++;
DFS(bi, bj + 1, bi, bj, isVisit, grid);
tem--;
}
isVisit[bi][bj] = false;
}
private boolean canGo(int bi, int bj, int ti, int tj, int si, int sj, char[][] grid, boolean[][] isVisit) {
if (si < 0 || si >= grid.length
|| sj < 0 || sj >= grid[0].length
|| (grid[si][sj] == '.' || (si == bi && sj == bj))
|| isVisit[si][sj]) {
return false;
}
isVisit[si][sj] = true;
if (ti == si && tj == sj) {
return true;
}
boolean end = canGo(bi, bj, ti, tj, si - 1, sj, grid, isVisit)
|| canGo(bi, bj, ti, tj, si - 1, sj, grid, isVisit)
|| canGo(bi, bj, ti, tj, si - 1, sj, grid, isVisit)
|| canGo(bi, bj, ti, tj, si - 1, sj, grid, isVisit);
isVisit[si][sj] = false;
return end;
}