Weekly Contest 139
1071. Greatest Common Divisor of Strings
For strings
S
andT
, we say "T
dividesS
" if and only ifS = T + ... + T
(T
concatenated with itself 1 or more times)Return the largest string
X
such thatX
divides str1 andX
divides str2.
Example 1:
Input: str1 = "ABCABC", str2 = "ABC"
Output: "ABC"Example 2:
Input: str1 = "ABABAB", str2 = "ABAB"
Output: "AB"Example 3:
Input: str1 = "LEET", str2 = "CODE"
Output: ""
Note:
1 <= str1.length <= 1000
1 <= str2.length <= 1000
str1[i]
andstr2[i]
are English uppercase letters.
Approach #1: Simulate. [Java]
class Solution {
public String gcdOfStrings(String str1, String str2) {
int len1 = str1.length(), len2 = str2.length();
int minLen = Math.min(str1.length(), str2.length());
while (minLen > 0) {
if (len1 % minLen == 0 && len2 % minLen == 0) {
String subStr = str2.substring(0, minLen);
if (isRepeat(str1, subStr) && isRepeat(str2, subStr)) {
return subStr;
}
}
minLen--;
}
return new String("");
} public boolean isRepeat(String target, String subStr) {
int n = subStr.length();
for (int i = 0; i < target.length(); ++i) {
if (target.charAt(i) != subStr.charAt(i%n))
return false;
}
return true;
}
}
Analysis:
The greatest common divisor of string's length must is the divisor of str1.length() and str2.length(). So we can find the min length of str1.length() and str2.length() as the common divisor of string's length at the first. If divisor string's length is the divisor of str1.length and str2.length, and str1, str2 are consituted by repeating divisor string, we return the longest divisor string.
1072. Flip Columns For Maximum Number of Equal Rows
Given a
matrix
consisting of 0s and 1s, we may choose any number of columns in the matrix and flip every cell in that column. Flipping a cell changes the value of that cell from 0 to 1 or from 1 to 0.Return the maximum number of rows that have all values equal after some number of flips.
Example 1:
Input: [[0,1],[1,1]]
Output: 1
Explanation: After flipping no values, 1 row has all values equal.Example 2:
Input: [[0,1],[1,0]]
Output: 2
Explanation: After flipping values in the first column, both rows have equal values.Example 3:
Input: [[0,0,0],[0,0,1],[1,1,0]]
Output: 2
Explanation: After flipping values in the first two columns, the last two rows have equal values.
Note:
1 <= matrix.length <= 300
1 <= matrix[i].length <= 300
- All
matrix[i].length
's are equalmatrix[i][j]
is0
or1
Approach #1:
class Solution {
public int maxEqualRowsAfterFlips(int[][] matrix) {
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < matrix.length; ++i) {
String key = Arrays.toString(matrix[i]);
for (int j = 0; j < matrix[i].length; ++j) matrix[i][j] ^= 1;
String rev = Arrays.toString(matrix[i]);
map.put(key, map.getOrDefault(key, 0) + 1);
map.put(rev, map.getOrDefault(rev, 0) + 1);
}
int ret = -1;
for (String key : map.keySet()) {
ret = Math.max(ret, map.get(key));
} return ret;
}
}
Analysis:
Intuitively, if two rows have the same numbers or have reverse numbers(0->1 or 1->0), we can flip some column to make them only contains 0 or 1. So we can use a map, the row number to a string as the key and the count as the value, otherwise, we should reverse the row's numbers as the key, too.
Finally, find the max value in the map.
1073. Adding Two Negabinary Numbers
Given two numbers
arr1
andarr2
in base -2, return the result of adding them together.Each number is given in array format: as an array of 0s and 1s, from most significant bit to least significant bit. For example,
arr = [1,1,0,1]
represents the number(-2)^3 + (-2)^2 + (-2)^0 = -3
. A numberarr
in array format is also guaranteed to have no leading zeros: eitherarr == [0]
orarr[0] == 1
.Return the result of adding
arr1
andarr2
in the same format: as an array of 0s and 1s with no leading zeros.
Example 1:
Input: arr1 = [1,1,1,1,1], arr2 = [1,0,1]
Output: [1,0,0,0,0]
Explanation: arr1 represents 11, arr2 represents 5, the output represents 16.
Note:
1 <= arr1.length <= 1000
1 <= arr2.length <= 1000
arr1
andarr2
have no leading zerosarr1[i]
is0
or1
arr2[i]
is0
or1
Approach #1:
class Solution {
public int[] addNegabinary(int[] arr1, int[] arr2) {
int i = arr1.length - 1, j = arr2.length - 1, carry = 0;
Stack<Integer> stack = new Stack<>();
while (i >= 0 || j >= 0 || carry != 0) {
int n1 = i >= 0 ? arr1[i--] : 0;
int n2 = j >= 0 ? arr2[j--] : 0;
carry = n1 + n2 + carry;
stack.push(carry & 1);
carry = -(carry >> 1);
}
while (!stack.empty() && stack.peek() == 0) stack.pop();
int[] ret = new int[stack.size()];
int index = 0;
while (!stack.empty()) {
ret[index++] = stack.pop();
}
return ret.length == 0 ? new int[1] : ret;
}
}
Approach #2: [WA]
class Solution {
public int[] addNegabinary(int[] arr1, int[] arr2) {
int num1 = 0, num2 = 0;
int len1 = arr1.length - 1, len2 = arr2.length - 1;
for (int i = 0; i < arr1.length; ++i) {
if (arr1[i] == 1) {
num1 += Math.pow(-2, len1);
len1--;
} else {
len1--;
}
}
for (int i = 0; i < arr2.length; ++i) {
if (arr2[i] == 1) {
num2 += Math.pow(-2, len2);
len2--;
} else {
len2--;
}
}
int sum = num1 + num2;
List<Integer> list = new ArrayList<Integer>();
if (sum == 0) list.add(0);
while (sum != 0) {
int remainder = sum % (-2);
sum = sum / (-2);
// System.out.println(remainder + " " + sum);
if (remainder < 0) {
remainder += 2;
sum += 1;
}
list.add(remainder);
}
Collections.reverse(list); int[] ret = new int[list.size()];
for (int i = 0; i < list.size(); ++i)
ret[i] = list.get(i); return ret;
}
}
1074. Number of Submatrices That Sum to Target
Given a
matrix
, and atarget
, return the number of non-empty submatrices that sum to target.A submatrix
x1, y1, x2, y2
is the set of all cellsmatrix[x][y]
withx1 <= x <= x2
andy1 <= y <= y2
.Two submatrices
(x1, y1, x2, y2)
and(x1', y1', x2', y2')
are different if they have some coordinate that is different: for example, ifx1 != x1'
.
Example 1:
Input: matrix = [[0,1,0],[1,1,1],[0,1,0]], target = 0
Output: 4
Explanation: The four 1x1 submatrices that only contain 0.Example 2:
Input: matrix = [[1,-1],[-1,1]], target = 0
Output: 5
Explanation: The two 1x2 submatrices, plus the two 2x1 submatrices, plus the 2x2 submatrix.
Note:
1 <= matrix.length <= 300
1 <= matrix[0].length <= 300
-1000 <= matrix[i] <= 1000
-10^8 <= target <= 10^8
Approach #1:
class Solution {
public int numSubmatrixSumTarget(int[][] matrix, int target) {
int row = matrix.length, col = matrix[0].length;
int[][] sumMatrix = new int[row+1][col+1];
sumMatrix[1][1] = matrix[0][0];
for (int i = 2; i <= row; ++i)
sumMatrix[i][1] = matrix[i-1][0] + sumMatrix[i-1][1];
for (int j = 2; j <= col; ++j)
sumMatrix[1][j] = matrix[0][j-1] + sumMatrix[1][j-1];
for (int i = 2; i <= row; ++i) {
for (int j = 2; j <= col; ++j) {
sumMatrix[i][j] = sumMatrix[i][j-1] + sumMatrix[i-1][j] - sumMatrix[i-1][j-1] + matrix[i-1][j-1]; }
} int count = 0;
for (int i = 1; i <= row; i++) {
for (int j = 1; j <= col; ++j) {
count += countTarget(i, j, target, sumMatrix);
}
} return count;
} public int countTarget(int x, int y, int target, int[][] sumMatrix) {
int subCount = 0, sum = 0;
for (int i = 0; i < x; ++i) {
for (int j = 0; j < y; ++j) {
sum = sumMatrix[x][y] - sumMatrix[i][y] - sumMatrix[x][j] + sumMatrix[i][j];
if (sum == target) subCount++;
}
}
return subCount;
}
}
Analysis:
Firstly, we calculate the sum of a sub-matrix from [0, 0] to [i, j].
Secondly, traveling all the points in the sub-matrix as the start point and claculate the sum, if the sum equal to the target, the count number increase one.
Weekly Contest 139的更多相关文章
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- LeetCode之Weekly Contest 91
第一题:柠檬水找零 问题: 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10 ...
- LeetCode Weekly Contest
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
- LeetCode Weekly Contest 47
闲着无聊参加了这个比赛,我刚加入战场的时候时间已经过了三分多钟,这个时候已经有20多个大佬做出了4分题,我一脸懵逼地打开第一道题 665. Non-decreasing Array My Submis ...
- 75th LeetCode Weekly Contest Champagne Tower
We stack glasses in a pyramid, where the first row has 1 glass, the second row has 2 glasses, and so ...
- LeetCode之Weekly Contest 102
第一题:905. 按奇偶校验排序数组 问题: 给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入: ...
随机推荐
- 使用 xunit 编写测试代码
使用 xunit 编写测试代码 Intro xunit 是 .NET 里使用非常广泛的一个测试框架,有很多测试项目都是在使用 xunit 作为测试框架,不仅仅有很多开源项目在使用,很多微软的项目也在使 ...
- Linux流量查看工具
目录 监控总体带宽使用 nload.bmon.slurm.bwm-ng.cbm.speedometer和netload 监控总体带宽使用(批量式输出) vnstat.ifstat.dstat和coll ...
- Java数组之二分查找
简单的二分查找 package com.kangkang.array; public class demo03 { public static void main(String[] args) { / ...
- mac 下如何轻松安装神器 Anaconda
本文推荐使用homebrew 安装 1.打开终端执行 brew cask install anaconda3 然后就可以喝一杯咖啡了,终端会自动执行安装好 如果终端卡在update homebrew ...
- PAT-1064(Complete Binary Search Tree)JAVA实现
Complete Binary Search Tree PAT-1064 本次因为涉及到完全二叉排序树,所以可以使用数组的形式来存储二叉排序树 对输入序列排序后,得到的是中序遍历二叉排序树的序列.对这 ...
- PHP题库1
选择题11. php中,不等运算符是( B.C ) A ≠ B != C <> D >< 2. 函数的参数传递包括:( A.B ) A 按值传递 B ...
- Typescript开发学习总结(附大量代码)
如果评定前端在最近五年的重大突破,Typescript肯定能名列其中,重大到各大技术论坛.大厂面试都认为Typescript应当是前端的一项必会技能.作为一名消息闭塞到被同事调侃成"新石器时 ...
- linux下redis安装运行教程——redis系列
天没降大任于我,照样苦我心智,劳我筋骨. 安装运行的过程 由于官网太慢,csdn里的资源又要钱,所以呢,只能使用我自己本地以前下载的陈年..哦不,3.xredis安装包 资源已经放到百度云,需要的可以 ...
- (3)MySQL进阶篇SQL优化(索引)
1.索引问题 索引是数据库优化中最常用也是最重要的手段之一,通过索引通常可以帮助用户解决大多数 的SQL性能问题.本章节将对MySQL中的索引的分类.存储.使用方法做详细的介绍. 2.索引的存储分类 ...
- GRU算法原理
一.GRU算法 GRU(Gate Recurrent Unit,循环门单元)是循环神经网络(Recurrent Neural Network, RNN)的一种.和LSTM(Long-Short Ter ...