1071. Greatest Common Divisor of Strings

For strings S and T, we say "T divides S" if and only if S = T + ... + T  (T concatenated with itself 1 or more times)

Return the largest string X such that X divides str1 and X 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. 1 <= str1.length <= 1000
  2. 1 <= str2.length <= 1000
  3. str1[i] and str2[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. 1 <= matrix.length <= 300
  2. 1 <= matrix[i].length <= 300
  3. All matrix[i].length's are equal
  4. matrix[i][j] is 0 or 1

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 and arr2 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 number arr in array format is also guaranteed to have no leading zeros: either arr == [0] or arr[0] == 1.

Return the result of adding arr1 and arr2 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. 1 <= arr1.length <= 1000
  2. 1 <= arr2.length <= 1000
  3. arr1 and arr2 have no leading zeros
  4. arr1[i] is 0 or 1
  5. arr2[i] is 0 or 1

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 a target, return the number of non-empty submatrices that sum to target.

A submatrix x1, y1, x2, y2 is the set of all cells matrix[x][y] with x1 <= x <= x2 and y1 <= 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, if x1 != 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. 1 <= matrix.length <= 300
  2. 1 <= matrix[0].length <= 300
  3. -1000 <= matrix[i] <= 1000
  4. -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的更多相关文章

  1. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  2. Leetcode Weekly Contest 86

    Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...

  3. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  4. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

  5. LeetCode之Weekly Contest 91

    第一题:柠檬水找零 问题: 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10  ...

  6. LeetCode Weekly Contest

    链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...

  7. LeetCode Weekly Contest 47

    闲着无聊参加了这个比赛,我刚加入战场的时候时间已经过了三分多钟,这个时候已经有20多个大佬做出了4分题,我一脸懵逼地打开第一道题 665. Non-decreasing Array My Submis ...

  8. 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 ...

  9. LeetCode之Weekly Contest 102

    第一题:905. 按奇偶校验排序数组 问题: 给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入: ...

随机推荐

  1. CSS 书写禅机

    这是未来的趋势所向,如是我行. 注意:原文发表于 2017-9-6,随着框架不断演进,部分内容可能已不适用. CSS 日渐惹人憎恶. 究其原因颇多,归根结底,皆因 CSS 给人的感觉总是飘渺迷蒙.变幻 ...

  2. (十二)数据库查询处理之Query Execution(1)

    (十二)数据库查询处理之Query Execution(1) 1. 写在前面 这一大部分就是为了Lab3做准备的 每一个query plan都要实现一个next函数和一个init函数 对于next函数 ...

  3. R语言barplot ,掌握本篇的内容,基本的条形图都可以画了

    本篇主要想复现文章中的一张图,原图来源(Antibiotic resistome and its association with bacterial communities during sewag ...

  4. Django 报错 Reverse for 'content' not found. 'content' is not a valid view function or pattern name.

    Django 报错 Reverse for 'content' not found. 'content' is not a valid view function or pattern name. 我 ...

  5. Shiro反序列化漏洞检测、dnslog

    目录 信息收集 poc 参考 信息收集 poc # pip install pycrypto import sys import base64 import uuid from random impo ...

  6. css 超过一行省略号

    //超过一行省略号 overflow: hidden; white-space: nowrap; text-overflow: ellipsis; //超过两行省略号 overflow: hidden ...

  7. 剑指 Offer 32 - II. 从上到下打印二叉树 II + 层次遍历二叉树 + 按层存储

    剑指 Offer 32 - II. 从上到下打印二叉树 II Offer_32 题目描述: 题解分析: 这道题我一开始想到的解决方法较粗暴,就是使用两个变量来记录当前层的节点数和下一层的结点数. 以上 ...

  8. There only 10 people use the same phone as you(i春秋CTF题解)

      (1)访问网址进行CTF测试,仅出现登陆与注册的页面 (2)进行注册尝试登陆并进行burp抓取数据包: (3)注册成功,进行登陆尝试查看信息是否具有提示,在登录的页面只有两个点击页面,一个为:Ch ...

  9. 《Asp.Net Core3 + Vue3入坑教程》 - 6.异常处理与UserFriendlyException

    简介 <Asp.Net Core3 + Vue3入坑教程> 此教程适合新手入门或者前后端分离尝试者.可以根据图文一步一步进操作编码也可以选择直接查看源码.每一篇文章都有对应的源码 目录 & ...

  10. 如何优雅的移植JavaScript组件到Blazor

    Blazor作为一个新兴的交互式 Web UI 的框架,有其自身的优缺点,如果现有的 JavaScript 组件能移植到 Blazor,无疑让 Blazor 如虎添翼,本文就介绍一下自己在开发 Bul ...