发布于2021-04-18 14:55 阅读(532) 评论(0) 点赞(3) 收藏(1)
被题出错点:
(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;
}
}
//思路是按照字符串长度进行排序,然后从长度小的到长度大的一次进行。
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);
}
}
}
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;
}
}
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;
}
}
原文链接:https://blog.csdn.net/qq_43978754/article/details/115754439
作者:天天在家
链接:http://www.javaheidong.com/blog/article/159859/c64cd4c423cd3891c74c/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!