LeetCode118 LeetCode119 LeetCode151 LeetCode202 Java
2015-10-08 21:51
459 查看
LeetCode 118
LeetCode 119
LeetCode 151
LeetCode 202
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; }
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解
- LeetCode [Day 5] Longest Palindromic Substring 题解