LeetCode118 LeetCode119 LeetCode151 LeetCode202 Java

LeetCode 118  

static public List<List<Integer>> generate(int numRows) {
        
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        List<Integer> smallRes = null;

        smallRes = new ArrayList<Integer>();

        // 大循环内填充List<List<Integer>> res
        for (int i = 1; i <= numRows; i++) {
            // 小循环内填充List<Integer> smallRes
            smallRes = new ArrayList<Integer>();
            for (int j = 0; j < i; j++) {
                // 取出来res内上一级的内容
                if (j == 0 || j == i - 1) {
                    smallRes.add(1);
                } else {
                    // 这里错写过j-1
                    List<Integer> last = res.get(i - 2);
                    Integer count = 0;
                    int a = last.get(j - 1);
                    int b = last.get(j);
                    count = a + b;
                    smallRes.add(count);
                }
            }

            res.add(smallRes);
        }
        return res;

    }

LeetCode  119

static public List<Integer> getRow(int rowIndex) {
        
        List<Integer> list = new ArrayList<Integer>(rowIndex);
        if (rowIndex == 0) {
            list.add(1);
            return list;
        } else if (rowIndex == 1) {
            list.add(1);
            list.add(1);
            return list;

        }

        for (int i = 1; i <= rowIndex; i++) {
            list.add(1);
            for (int j = i - 1; j > 0; j--) {
                // 只用到上一行的内容,set与add
                System.out.println("j=="+j+"=="+list.get(j)+"=="+list.get(j-1));
                list.set(j, list.get(j) + list.get(j - 1));
            }
        }
        list.add(1);
        return list;
    }


    LeetCode 151
      
 static public String reverseWords(String s) {
        // 时间复杂度结果为O(n^2)
        // ""输入不能通过
        if (s.length() <= 0) {
            s = new String();
            return s;
        }
        // 全部是空格的不能通过,去掉多余空格
        int end = s.length() - 1;
        int begin = 0;
        while (begin <= end && s.charAt(begin) == ' ') {
            begin++;
        }
        while (begin <= end && s.charAt(end) == ' ') {
            end--;
        }
        if (end < begin) {
            System.out.println("---s-");
            s = new String();

            return s;
        }
        // StringBuffer反之Time Limit 发生
        StringBuffer res = new StringBuffer();
        // 中间存储
        String mid = new String();
        int n = end;

        while (n >= begin) {

            if (s.charAt(n) == ' ') {
                int smallC = mid.length() - 1;
                System.out.println("===="+mid.length());
                while (smallC >= 0) {
                    res = res.append(mid.charAt(smallC));
                    smallC--;

                }
                //依靠mid.length()去除中间多出的空格
                if (mid.length() >= 1) {
                    res.append(' ');
                }

                mid = new String();
            } else if (n == begin) {
                //依靠空格判断做一次mid顺序置换,如果到最后一个单词的时候没办法处理,专门设定一个n==begin判断
                System.out.println("====="+s.charAt(n));
                mid = mid + s.charAt(n);
                int smallC = mid.length() - 1;
                while (smallC >= 0) {
                    res = res.append(mid.charAt(smallC));
                    smallC--;
                }
            } else {
                mid = mid + s.charAt(n);
            }
            n--;
        }
        return res.toString();
    }

LeetCode 202
static public boolean isHappy(int n) {

        ArrayList array = new ArrayList();
        // System.out.println(Integer.MAX_VALUE);
        while (true) {
            if (array.contains(n)) {
                return false;
            } else {
                array.add(n);
            }
            n = count(n);
            if (n == 1)
                return true;
        }
    }

    // 计算每一位的平方和
    static public int count(int num) {
        int count = 0;
        int a;
        while (true) {
            a = num % 10;
            num = num / 10;
            count += a * a;
            if (num <= 0) {
                break;
            }
        }
        return count;
    }


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值