【问题标题】:Check if all values are repeated at least two times in linked list检查所有值是否在链表中至少重复两次
【发布时间】:2013-03-19 12:54:04
【问题描述】:

我拥有与 http://www.cplusplus.com/forum/beginner/73928/ 中定义的完全相同的 ElemList

如果所有值都重复了两次或更多次,您能否建议一些关于如何编写返回 true 的函数的提示?例如

1,1,1,2,2 - true
1,2 - false

我觉得它肯定需要一个动态数组,但想不出算法。

【问题讨论】:

  • 不连续重复怎么办? 1,2,1,2,是true 还是false
  • 这将被视为真实,不需要排序元素。

标签: c++ list linked-list


【解决方案1】:

是的,创建一个std::map<int,int>,您可以在其中计算列表中每个数字的出现次数。此计算需要遍历所有列表。

然后,再次遍历您刚刚创建的 std::map,并查看所有值是否大于或等于 2。

【讨论】:

    【解决方案2】:
    bool twoormore()
        {
            int count = 0;// for counting elements in list
            int temp;// temprorary element for sorting and logical part
            int cik;// how much times the value has been mentioned
            bool res = true;// function result
            int * arr;// pointer for the upcoming dynamic array
            for(start();!end();next())
            {
                count++;// counting the elements
    
            }
            if(count != 0){
                arr = new int[count];//creating array
                int i = 0;
                for(start();!end();next())
                {
                    arr[i++] = current->num;//filling array
                }
                /** array sorting **/
                for(int i = 0;i < count;i++)
                    for(int j = 0; j < count; j++)
                    {
                        if(arr[j] > arr[i])
                        {
                            temp = arr[i];
                            arr[i] = arr[j];
                            arr[j] = temp;
                        }
                    }
                /** sort ends **/
                temp = arr[0]; // setting first element ar temp.. for upcoming check
                cik = 1;// it's been its first time
                for(int i = 1;i < count;i++)
                {
                    if(arr[i] == temp)
                    {
                        cik++; continue;// if upciming element is equal to temprorary , then add 1 to counter.. and continue looping
                    }else
                    {
                        if(cik > 1)
                        {
                            temp = arr[i];// if everything ok, but element value changes.
                            cik = 1;// sets defualt
                            continue;
                        }
                        else
                        {
                            res = false;// other way, the value wasnt there two times
                            break;
                        }
                    }
    
    
                }
                delete arr;//deleting allocated space for array
                return res;// returning bool, true or false.
            }
        }
    

    【讨论】:

      【解决方案3】:

      函数看起来像这样(未经测试):

        std::map<int,int> m_mapCount;
        std::map<int,int>::iterator m_Iterator;
      
        for (l.start(); !l.end(); l.next()) // put the content of your linkedlist to map
        {
             m_mapCount[l.current->num] += 1;
        }
      
        for (m_Iterator=m_mapCount.begin(); m_Iterator!=m_mapCount.end(); m_Iterator++)
        {
            if(m_Iterator->second >= 2) return true;
        }
      

      【讨论】:

        猜你喜欢
        • 2020-08-29
        • 1970-01-01
        • 2015-09-22
        • 1970-01-01
        • 1970-01-01
        • 2023-03-19
        • 1970-01-01
        • 2022-01-22
        • 2023-04-05
        相关资源
        最近更新 更多