行转列:zip()
给你由 n 个小写字母字符串组成的数组 strs,其中每个字符串长度相等。
这些字符串可以每个一行,排成一个网格。例如,strs = ["abc", "bce", "cae"] 可以排列为:
abc
bce
cae
你需要找出并删除 不是按字典序升序排列的 列。在上面的例子(下标从 0 开始)中,列 0('a', 'b', 'c')和列 2('c', 'e', 'e')都是按升序排列的,而列 1('b', 'c', 'a')不是,所以要删除列 1 。
返回你需要删除的列数。
var minDeletionSize = function(strs) {
const n = strs.length, m = strs[0].length
let r = 0
for (let j = 0; j < m; j++) {
for (let i = 1; i < n; i++) {
if (strs[i][j] < strs[i - 1][j]) {
r++
break
}
}
}
return r
};
func minDeletionSize(strs []string) int {
n, m, r := len(strs), len(strs[0]), 0
for j := 0; j < m; j++ {
for i := 1; i < n; i++ {
if strs[i][j] < strs[i - 1][j] {
r++
break
}
}
}
return r
}
class Solution {
function minDeletionSize($strs) {
$n = count($strs);
$m = strlen($strs[0]);
$r = 0;
for ($j = 0; $j < $m; $j++) {
for ($i = 1; $i < $n; $i++) {
if ($strs[$i][$j] < $strs[$i - 1][$j]) {
$r++;
break;
}
}
}
return $r;
}
}
function minDeletionSize(strs: string[]): number {
const n = strs.length, m = strs[0].length
let r = 0
for (let j = 0; j < m; j++) {
for (let i = 1; i < n; i++) {
if (strs[i][j] < strs[i - 1][j]) {
r++
break
}
}
}
return r
};
class Solution {
public int minDeletionSize(String[] strs) {
int n = strs.length;
int m = strs[0].length();
int r = 0;
for (int j = 0; j < m; j++) {
for (int i = 1; i < n; i++) {
if (strs[i].charAt(j) < strs[i - 1].charAt(j)) {
r++;
break;
}
}
}
return r;
}
}
class Solution(object):
def minDeletionSize(self, strs):
n, m, r = len(strs), len(strs[0]), 0
for j in range(0, m) :
for i in range(1, n) :
if strs[i][j] < strs[i - 1][j] :
r += 1
break
return r
class Solution:
def minDeletionSize(self, strs: List[str]) -> int:
return sum(any(x > y for x, y in pairwise(col)) for col in zip(*strs))
class Solution:
def minDeletionSize(self, strs: List[str]) -> int:
return sum(list(col) != sorted(col) for col in zip(*strs))
int minDeletionSize(char ** strs, int strsSize){
int n = strsSize;
int m = strlen(strs[0]);
int r = 0;
for (int j = 0; j < m; j++) {
for (int i = 1; i < n; i++) {
if (strs[i][j] < strs[i - 1][j]) {
r++;
break;
}
}
}
return r;
}
class Solution {
public:
int minDeletionSize(vector<string>& strs) {
int n = strs.size();
int m = strs[0].size();
int r = 0;
for (int j = 0; j < m; j++) {
for (int i = 1; i < n; i++) {
if (strs[i][j] < strs[i - 1][j]) {
r++;
break;
}
}
}
return r;
}
};
public class Solution {
public int MinDeletionSize(string[] strs) {
int n = strs.Length;
int m = strs[0].Length;
int r = 0;
for (int j = 0; j < m; j++) {
for (int i = 1; i < n; i++) {
if (strs[i][j] < strs[i - 1][j]) {
r++;
break;
}
}
}
return r;
}
}