leetcode
御风阁
藏,只为将最好的一面展示给对的人。
展开
-
LeetCode 345 反转元音字符串
import java.util.HashSet;public class Solution { static HashSet<Character> set = new HashSet<>(); static{ set.add('a'); set.add('e'); set.add('i'); set.add('o')原创 2016-04-25 21:00:46 · 683 阅读 · 0 评论 -
LeetCode 5 Longest Palindromic Substring
public String longestPalindrome(String s) { if(s==null){ return ""; } char[] arr = s.toCharArray(); int max = 0; int maxi = 0; int maxj = 0; for(int i = 0; i<转载 2016-05-21 21:29:41 · 218 阅读 · 0 评论 -
Leetcode 349. Intersection of Two Arrays
问题描述:求两个整数数组的交集;(一个月不刷Leetcode,现在已经沦落到只会简单题的地步)public class Solution { public int[] intersection(int[] nums1, int[] nums2) { if(nums1.length>nums2.length){ return intersection(nu原创 2016-06-07 20:59:52 · 337 阅读 · 0 评论 -
LeetCode 217 Contains Duplicate II java
(PS:算法很渣,也就简单的题能过) 基本思想很简单:HashSet查找还是挺快的,借助HashSet查找import java.util.HashSet;public class Solution2{ public boolean containsNearbyDuplicate(int[] nums, int k) { HashSet<Integer> set = new原创 2016-04-21 21:55:37 · 376 阅读 · 0 评论 -
LeetCode 127 Word Ladder java (看某位大神的解法做出来的)
问题重述: Given two words (beginWord and endWord), and a dictionary’s word list, find the length of shortest transformation sequence from beginWord to endWord, such that: Only one letter can be change原创 2016-04-21 21:50:21 · 1929 阅读 · 0 评论 -
LeetCode 241. Different Ways to Add Parentheses
public class Solution { Map<String, List<Integer>> map = new HashMap<>(); public List<Integer> diffWaysToCompute(String input) { if(map.containsKey(input)) return map.get(in转载 2016-05-02 14:51:09 · 277 阅读 · 0 评论 -
LeetCode 3. Longest Substring Without Repeating Characters
击败91%,不知道怎么改进了。 代码如下:public class Solution { public int lengthOfLongestSubstring(String s) { int[] counts = new int[128];//标记字符出现位置 for(int i = 0;i<counts.length;i++){原创 2016-05-01 21:51:14 · 283 阅读 · 0 评论 -
LeetCode 89. Gray Code(生成格雷码)
public class Solution { public List<Integer> grayCode(int n) { int all = 1<<n; List<Integer> result = new ArrayList<Integer>(all); result.add(0); if(n==0)return resu原创 2016-05-01 17:10:52 · 349 阅读 · 0 评论 -
LeetCode342. Power of Four
表示这个题很坑,因为0是4的0次幂,但是测试用例返回false。 代码如下:public class Solution { public boolean isPowerOfFour(int num) { if(num<=0){ return false; } String s = Integer.toBinaryStr原创 2016-04-28 19:37:57 · 239 阅读 · 0 评论 -
LeetCode 96.Unique Binary Search Trees
代码如下:public class Solution { static int[] src; public int numTrees(int n) { int max = Math.max(n+1,4); src = new int[max]; src[0] = 1; src[1] = 1; src[2]原创 2016-05-01 11:51:36 · 189 阅读 · 0 评论 -
正则表示式实例1--判断某个数是不是4的幂数
方法一,直接使用String.matches();public class TestRegex1 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int i; do{ System.out.print("输原创 2016-04-29 18:54:49 · 395 阅读 · 0 评论 -
LeetCode 53. Maximum Subarray
public class Solution { public int maxSubArray(int[] nums) { if (nums == null){ return 0; } int currentsum = nums[0]; int maxsum = nums[0]; for (i原创 2016-04-29 16:11:54 · 189 阅读 · 0 评论 -
LeetCode 121. Best Time to Buy and Sell Stock
public class Solution { public int maxProfit(int[] prices) { if(prices.length==0){ return 0; } int maxProfit = 0; int min = prices[0]; for(int i原创 2016-04-29 14:50:00 · 185 阅读 · 0 评论 -
LeetCode 2. Add Two Numbers
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode addTw原创 2016-04-28 14:47:33 · 215 阅读 · 0 评论 -
LeetCode 343. Integer Break
问题重述: n可以拆分成多个(两个及以上)数的和,求这几个数最大的乘积;例如8=4+4,8=3+3+2,最大的乘积是3*3*2=18代码如下:public class Solution { public int integerBreak(int n) { int[] result = new int[n - 1]; if(n == 2) return 1;原创 2016-04-27 16:40:38 · 250 阅读 · 0 评论 -
LeetCode 42. Trapping Rain Water
用了6ms,只击败了6%的提交者,表示好坑,之后会进一步改良与完善注释,先就这样:import java.util.LinkedList;public class Solution { public static class Pair{ public int height; public int index; public Pair(int原创 2016-04-26 22:04:04 · 306 阅读 · 0 评论 -
LeetCode 17. Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit strin原创 2016-09-16 20:59:46 · 222 阅读 · 0 评论