【问题标题】:Java list using arrays or any listing method使用数组或任何列表方法的 Java 列表
【发布时间】:2013-06-22 06:27:53
【问题描述】:

伙计们,我有一个很难完成的考试,因为我不知道我是否会使用仅包含 switch 语句的数组。我还是Java新手,所以请帮忙。这是问题。

Write a function:
class IntList {
  public int value;
  public IntList next;
}

class Solution {
  public int solution(IntList L, int M);
}

给定一个由N整数和一个正整数组成的单链表L

给定一个由N个整数和一个正整数M组成的单链表L,返回存储在列表中M-th元素中的值,从末尾开始计数,假设最后一个元素列表中有索引1。如果无法返回这样的值,该函数应返回−1。 例如,给定L,这样:

  L = 1 -> 2 -> 3 -> 4

M = 2,函数应该返回3,因为从列表末尾算起的第二个元素的值是3

【问题讨论】:

  • ... 为什么要为此使用 switch 语句?
  • 调用数组或列表中的单个元素。我不确定这是否是正确的方法,或者是否有可能这样做。
  • 对于这个例子,你知道正好有 4 个元素,理论上你可以使用 switch 语句。但这是一个非常糟糕的想法。在更一般的情况下,List 中的元素数量不同,切换将不起作用。

标签: java list integer


【解决方案1】:

如果我没记错的话,你需要编写一个程序,将 Kth 返回到链表中的最后一个元素。 - 如果它是一个单链表,你需要一个头指针。假设您有一个名为 IntList 头的头指针。以下是查找第 K 个到最后一个元素的代码

public int solution(IntList head, int k) {
  IntList temp = head;
  int count = 0;
  while(count!=k) {
    if(temp==null) {
      return -1;
    }
    temp = temp.next;
  }

  while(temp!=null) {
    head = head.next;
    temp = temp.next;
  }
  return head.value;
}

没有进行详尽的错误检查,但这应该可以。

【讨论】:

  • 这比我的方法要好,因为它只需要一次通过。但是我觉得我们应该不愿意提供完整的解决方案,特别是因为这是在一些考试的背景下。
【解决方案2】:

高级概念:

  1. 遍历列表找到长度N。

  2. 如果 N

  3. 遍历列表找到索引N-M处的元素。这里的索引指的是正常的从0到N-1的计数方案。

【讨论】:

    【解决方案3】:
    Here is the whole program!! 
    
    public class Program
    {
        public static void Main(string[] args)
        {
            IntList iList = new IntList(2, null);
            iList.add(789); 
            iList.add(45); 
            iList.add(19); 
            iList.add(8); 
            iList.add(154564);
            iList.add(32); 
            iList.add(88);
            iList.add(109);
            Solution sol = new Solution();
            Console.WriteLine(sol.solution(iList,8));
            Console.ReadLine();
        }
    }
        class Solution
        {
            public int solution(IntList L, int M)
            {
                IntList temp = L;
                int value=0;
                int i = 0;
                for (i = 0; i <= L.length() - 1; i++)
                {
                    temp = temp.next();
                    if (temp == null) break;
                    value = temp.value();
                    if (value == M)
                    {
                        if (i == 0) return L.length() - 1;
                        else return L.length()-1-i;
    
                    }
                }
                return -1;
            }
        }
    class IntList
    {
        private int val;
        private IntList nex;
    
        public IntList(int v, IntList n)
        {          
            val = v;
            nex = n;
        }
        public int length()
        {
            if (nex == null)
                return 1;
            else
                return 1 + nex.length();
        }
        public int value() { return val; }       
        public IntList next() { return nex; }
        public void setNext(IntList n) { nex = n; }
        public IntList findLast()
        {
            if (next() == null) return this;
            else return next().findLast();
        }
        public void add(int v)
        {
            findLast().setNext(new IntList(v, null));
        }
    }
    

    【讨论】:

      【解决方案4】:
        class Solution
          {
              public int solution(IntList L, int M)
              {
                  IntList temp = L;
                  int value=0;
                  int i = 0;
                  for (i = 0; i <= L.length() - 1; i++)
                  {
                      temp = temp.next();
                      if (temp == null) break;
                      value = temp.value();
                      if (value == M)
                      {
                          if (i == 0) return L.length() - 1;
                          else return L.length()-1-i;
      
                      }
                  }
                  return -1;
              }
          }
      

      这是解决方案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-28
        • 2011-07-21
        • 2014-09-25
        相关资源
        最近更新 更多