package com.算法专练.力扣.移除字母异位词后的结果数组;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @author xnl
* @Description:
* @date: 2022/8/29 22:29
*/
public class Solution {
public static void main(String[] args) {
Solution solution = new Solution();
}
/**
* 排序然后比较,注意的是相邻的数,并且排序之后结果相同的才是异位
* @param words
* @return
*/
public List<String> removeAnagrams(String[] words) {
List<String> res = new ArrayList<>();
String pre = "";
for (int i = 0; i < words.length; i++) {
char[] chars = words[i].toCharArray();
Arrays.sort(chars);
String cur = new String(chars);
if (i > 0 && pre.equals(cur)){
continue;
}
pre = cur;
res.add(words[i]);
}
return res;
}
}
力扣:2273. 移除字母异位词后的结果数组
最新推荐文章于 2024-08-25 17:36:33 发布