leetCode解题报告5道题(十一)(一)

2014-11-24 13:06:20 · 作者: · 浏览: 4

题目一:Subsets

Given a set of distinct integers, S, return all possible subsets.

Note:

  • Elements in a subset must be in non-descending order.
  • The solution set must not contain duplicate subsets.

    For example,
    If S = [1,2,3], a solution is:

    [
      [3],
      [1],
      [2],
      [1,2,3],
      [1,3],
      [2,3],
      [1,2],
      []
    ]

    分析:

    题目的题意挺简单的,我就不做说明,主要这道题目可以用递归的方法来做,也可以直接用迭代来取代,我们用迭代的方式来做这道题目,没有什么特别的算法,直接上代码,代码里有注释哈!


    AC代码:

    public class Solution {
        private ArrayList
        
         > results = new ArrayList
         
          >(); public ArrayList
          
           > subsets(int[] S) { int len = S.length; results.add(new ArrayList
           
            ()); //先增加一个 [] 空集 int size = results.size(); int nowDealCount = 0; //表示当前要处理的长度 while (nowDealCount != len){ for (int i=0; i
            
              nowList = (ArrayList
             
              )results.get(i); if (nowDealCount == 0){//如果处理的长度为0,表示是最开始只有一个 空集 的时候 for (int j=0; j
              
                addList = new ArrayList
               
                (); addList.add(S[j]); results.add(addList); } }else if (nowList != null && nowList.size() == nowDealCount){ int maxNum = (int)nowList.get(nowDealCount-1); for (int j=0; j
                
                  copyList = new ArrayList
                 
                  (nowList); copyList.add(S[j]); results.add(copyList); } } } } size = results.size(); nowDealCount++; } return results; } }
                 
                
               
              
             
            
           
          
         
        

    目二:Subsets II

    Given a collection of integers that might contain duplicates, S, return all possible subsets.

    Note:

    • Elements in a subset must be in non-descending order.
    • The solution set must not contain duplicate subsets.

      For example,
      If S = [1,2,2], a solution is:

      [
        [2],
        [1],
        [1,2,2],
        [2,2],
        [1,2],
        []
      ]

      分析:

      这道题目跟上面那道基本上思路是类似的,只是这道题目多了可能重复的元素,这时候我们就要考虑重复的元素不能有相同集合的问题了,先把Num[]数组进行排序,然后跟题目一一样的解法, 只是在此过程中我们需要知道每个添加到results中的集合的最后一个元素(即最大元素)的下标,我们用一个indexList来存储。

      这样的话每次要再添加新元素组成新的集合,就只要从下标位置+1开始,到len结束来搜索就行了。但要注意的是,如果当前要添加到集合中的元素和前一个元素相等,那么就不添加,如 : [1, 2, 2], 当我们处理到nowDealCount==1时

      要往 [1] 中添加元素,我们会添加一个 [2], 组成[1, 2]但是当到第二个[2]的时候,我们不能再添加一个[1, 2]了,否则就重复出现了!


      AC代码:

      public class Solution {
          private ArrayList
            
             > results = new ArrayList
             
              >(); public ArrayList
              
               > subsets(int[] S) { int len = S.length; results.add(new ArrayList
               
                ()); //先增加一个 [] 空集 int size = results.size(); int nowDealCount = 0; //表示当前要处理的长度 while (nowDealCount != len){ for (int i=0; i
                
                  nowList = (ArrayList
                 
                  )results.get(i); if (nowDealCount == 0){//如果处理的长度为0,表示是最开始只有一个 空集 的时候 for (int j=0; j
                  
                    addList = new ArrayList
                   
                    (); addList.add(S[j]); results.add(addList); } }else if (nowList != null && nowList.size() == nowDealCount){ int maxNum = (int)nowList.get(nowDealCount-1); for (int j=0; j
                    
                      copyList = new ArrayList
                     
                      (nowList); copyList.add(S[j]); results.add(copyList); } } } } size = results.size(); nowDealCount++; } return results; } }
                     
                    
                   
                  
                 
                
               
              
             
            


      题目三:Unique Paths

      A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).

      The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

      How many possible unique paths are there

      Note: m and n will be at most 100.

      分析:

      题目的意思从一个矩阵的左上角的位置开始走,只能向右走或者向下走,最后要走到矩阵的右下角,求解出一共有多少种不同的走法.

      我们拿一个示例来分析下,当m=3, n=7的时候

      一个3 * 7的矩阵

      vc/Cw+a1xMq919M8L3A+CjxwPjxicj4KPC9wPgo8cD48c3Ryb25nPndheXNbbS0xXVtjb2xdID0gMTsgKCAwIDw9IGNvbCA8IG4pPC9zdHJvbmc+PC9wPgo8cD48c3Ryb25nPndheXNbcm93XVtuLTFdID0gMTsgKDAgPD0gcm93IDwgbSk8L3N0cm9uZz48L3A+CjxwPjxzdHJvbmc+d2F5c1tyb3ddW2NvbF0gPSB3YXlzW3JvdyYjNDM7MV1bY29sXSAmIzQzOyB3YXlzW3Jvd11bY29sJiM0MzsxXSAgKDA8PSByb3cgPD1tLTIsIDA8PSBjb2wgPD1uL