Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何解决这种编码方法之一?_Arrays_Hashmap - Fatal编程技术网

Arrays 如何解决这种编码方法之一?

Arrays 如何解决这种编码方法之一?,arrays,hashmap,Arrays,Hashmap,这是我的作业: 如果两个字符串是非空的,并且它们的第一个字符相同,那么我们会说它们“匹配”。循环并返回给定的非空字符串数组,如下所示:如果一个字符串与数组中较早的字符串匹配,则交换数组中的2个字符串。当阵列中的某个位置被交换时,它将不再匹配任何内容。使用贴图,只需通过阵列一次即可解决此问题 allSwap(["ab", "ac"]) → ["ac", "ab"] allSwap(["ax", "bx", "cx", "cy", "by", "ay", "aaa", "azz"]) → ["ay

这是我的作业:

如果两个字符串是非空的,并且它们的第一个字符相同,那么我们会说它们“匹配”。循环并返回给定的非空字符串数组,如下所示:如果一个字符串与数组中较早的字符串匹配,则交换数组中的2个字符串。当阵列中的某个位置被交换时,它将不再匹配任何内容。使用贴图,只需通过阵列一次即可解决此问题

allSwap(["ab", "ac"]) → ["ac", "ab"]

allSwap(["ax", "bx", "cx", "cy", "by", "ay", "aaa", "azz"]) → ["ay", "by", "cy", "cx", "bx", "ax", "azz", "aaa"]

allSwap(["ax", "bx", "ay", "by", "ai", "aj", "bx", "by"]) → ["ay", "by", "ax", "bx", "aj", "ai", "by", "bx"]

在映射中,将第一个字母存储为键,并将键的最新索引存储为值。如果地图中不存在字母,请将其添加到地图中。当一个字母已经存在于地图中时,将其从地图中删除并与该索引交换

/**
*交换数组中第一个字母相同的字符串,
*从左到右阅读。一旦字符串被交换,
*它将不再被交换。输入数组将发生变异。
* 
*@param strings要从中执行交换的字符串
*@交换后返回字符串
*/
公共静态字符串[]allSwap(最终字符串[]字符串){
//第一个字符的地图,以及最后一次看到它们的索引
final-Map-potentialSwap=new-HashMap();
对于(int-thisIndex=0;thisIndex

公共字符串[]所有交换(字符串[]字符串){
//第一个字符的地图,以及最后一次看到它们的索引
Map Map=newhashmap();
for(int i=0;i
这也可以

 public String[] allSwap(String[] strings) {
      
      
      Map<String,Integer> map = new HashMap<String,Integer>();
      
      
      for(int i=0;i<strings.length;i++){
        
        if(!map.containsKey(strings[i].substring(0,1))){
          map.put(strings[i].substring(0,1),i);
        }
        else{
          String temp = strings[map.get(strings[i].substring(0,1))];
          strings[map.get(strings[i].substring(0,1))] = strings[i];
          strings[i] = temp;
          map.remove(strings[i].substring(0,1));
        }
        
      }
      
    return strings;
    }
public String[]allSwap(String[]strings){
Map Map=newhashmap();

对于(inti=0;这很有帮助,但我想知道当(a)这似乎是一个家庭作业问题,以及(b)时,教育的目的可能是什么OP似乎事先没有做任何努力。如果他们交上这个解决方案,他们可能什么也学不到。@halfer在回答家庭作业问题时,我总是发现向某人展示好的代码是一个巨大的好处。我不知道他们之前是否有过任何尝试,但我敢打赌,他们已经做到了e、 他们将是一个正派的人,并且真正阅读代码,如果他们不理解某些东西,就会提问。@halfer当然我认识到家庭作业问题很容易被滥用,所以我会齐心协力只在算法相当复杂的情况下才回答,因为这意味着提问者的技能水平相当高我学习语言,并在学校取得了成绩。我不是来评判别人的,只是把我的知识放在前面。学校的工作是测试他们是否学到了什么。好吧,谢谢你深思熟虑的回答。你的好意可能会被浪费,因为我的经验是,这些问题甚至没有得到回答e、 但你的业力肯定会增加!谢谢你的回答。这不是我的家庭作业问题。我没有在顶部写“这是我的家庭作业”。我对这本书是新手,我想它已经默认写在那里了。
 public String[] allSwap(String[] strings) {
      
      
      Map<String,Integer> map = new HashMap<String,Integer>();
      
      
      for(int i=0;i<strings.length;i++){
        
        if(!map.containsKey(strings[i].substring(0,1))){
          map.put(strings[i].substring(0,1),i);
        }
        else{
          String temp = strings[map.get(strings[i].substring(0,1))];
          strings[map.get(strings[i].substring(0,1))] = strings[i];
          strings[i] = temp;
          map.remove(strings[i].substring(0,1));
        }
        
      }
      
    return strings;
    }