第1题.1773. 统计匹配检索规则的物品数量
被题出错点:
(1)字符串判断值相同一定要用equals,千万不可以用恒等符号。=
(2)一开始我的for each用法:
List<String> item;
for(item:items){...}
但是提示不行,这并不符合java中foreach的定义方式:
foreach的语句格式:
for(元素类型t 元素变量x : 遍历对象obj){
引用了x的java语句;
}
最终代码:
class Solution {
public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
int count=0;
// List<String> item;
int id=0;
if(ruleKey.equals("color")){
id=1;
}
else if(ruleKey.equals("name")){
id=2;
}
for(List<String> list:items){
if(list.get(id).equals(ruleValue)){
count++;
}
}
return count;
}
}
第2题:1408. 数组中的字符串匹配
//思路是按照字符串长度进行排序,然后从长度小的到长度大的一次进行。
class Solution {
public List<String> stringMatching(String[] words) {
//思路是按照字符串长度进行排序,然后从长度小的道长度大的一次进行。
List<String> result=new ArrayList<String>();
QuickSort(words,0,words.length-1);
for(int i=0;i<words.length;i++){
for(int j=i+1;j<words.length;j++){
if(words[j].contains(words[i])){
result.add(words[i]);
break;
}
}
}
return result;
}
public int pivotic(String[]words,int low,int high){
String temp=words[low];
int pivotick=words[low].length();
while(low<high){
while(low<high&&words[high].length()>=pivotick) high--;
words[low]=words[high];
while(low<high&&words[low].length()<=pivotick) low++;
words[high]=words[low];
}
words[high]=temp;
return high;
}
public void QuickSort(String[]words,int low,int high){
if(low<high){
int pivotick=pivotic(words,low,high);
QuickSort(words,low,pivotick-1);
QuickSort(words,pivotick+1,high);
}
}
}
3.查找和替换模式
class Solution {
public List<String> findAndReplacePattern(String[] words, String pattern) {
//映射形式的,用map
List<String> list= new ArrayList<String>();
for(String string:words){
if(replacePattern(string,pattern)){
list.add(string);
}
}
return list;
}
public boolean replacePattern(String string,String pattern){
boolean result=true;
Map<Character,Character> map=new HashMap<>();
char[] chArray=string.toCharArray();
char[] paArray=pattern.toCharArray();
for(int i=0;i<chArray.length;i++){
if(!map.containsKey(paArray[i])){
if(map.containsValue(chArray[i])){
result=false;
break;
}
else{
map.put(paArray[i],chArray[i]);
}
}
else{
if(map.get(paArray[i])==chArray[i]){
continue;
}
else{
result=false;
break;
}
}
}
return result;
}
}
第4题:1023. 驼峰式匹配
class Solution {
public List<Boolean> camelMatch(String[] queries, String pattern) {
List<Boolean> list=new ArrayList<Boolean>();
for(String string:queries){
if(match(string,pattern)){
list.add(true);
}
else{
list.add(false);
}
}
return list;
}
public boolean match(String string,String pattern){ //只能加入小写字母
boolean result=true;
//char[] strArray=string.toCharArray();
//char[] paArray=pattern.toCharArray();
int indexStr=0;
int indexPa=0;
while(indexStr<string.length()){
if(indexPa<pattern.length()){
if(string.charAt(indexStr)>='A'&&string.charAt(indexStr)<='Z'){
if(string.charAt(indexStr)==pattern.charAt(indexPa)){
indexPa++;
indexStr++;
}
else{
result=false;
break;
}
}
else if(string.charAt(indexStr)>='a'&&string.charAt(indexStr)<='z'){
if(string.charAt(indexStr)==pattern.charAt(indexPa)){
indexPa++;
indexStr++;
}
else{
indexStr++; //小写可以随便加
}
}
}
else{
if(string.charAt(indexStr)>='a'&&string.charAt(indexStr)<='z'){
indexStr++;
}
else{
result=false;
break;
}
}
}
if(indexPa<pattern.length()){
result=false;
}
return result;
}
}