leetcode题解
qq_19442771
这个作者很懒,什么都没留下…
展开
-
leetcode Integer to Roman(Java)
题目链接:点击打开链接类型:数学处理解法:构建转换词库(从大到小)public class Solution { private static int[] intCompare = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; private static String[] romanCompare = {"M",原创 2017-06-16 09:32:40 · 139 阅读 · 0 评论 -
leetcode ZigZag Conversion(Java)
题目链接:点击打开链接类型:字符串处理解法:观察法public class Solution { public String convert(String s, int numRows) { int len = s.length(); if (numRows == 1 || len <= numRows) return s; StringBuilder原创 2017-06-11 20:25:13 · 186 阅读 · 0 评论 -
leetcode Reverse Integer(Java)
题目链接:点击打开链接类型:数学运算解法:移位运算public class Solution { public int reverse(int x) { long result = 0; boolean negative = false; if (x < 0) { x = -x; negative = true; }原创 2017-06-11 22:53:58 · 174 阅读 · 0 评论 -
leetcode String to Integer (atoi)(Java)
题目链接:点击打开链接类型:字符串处理,边界处理解法:public class Solution { public int myAtoi(String str) { if (str == null || str.length() < 1 ) return 0; str = str.trim(); int len原创 2017-06-12 10:01:00 · 141 阅读 · 0 评论 -
leetcode Palindrome Number(Java)
题目链接:点击打开链接类型:数学处理解法:Reverse Integerpublic class Solution { public boolean isPalindrome(int x) { if (x < 0) return false; int reverse = 0; int compa原创 2017-06-12 15:28:47 · 139 阅读 · 0 评论 -
leetcode Merge Two Sorted Lists(Java)
题目链接:点击打开链接类型:合并解法:递归/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution原创 2017-06-24 22:08:37 · 121 阅读 · 0 评论 -
leetcode Generate Parentheses(Java)
题目链接:点击打开链接类型:DFS解法:public class Solution { public List generateParenthesis(int n) { List res = new ArrayList<>(); dfs(res, "", n, n); return res;原创 2017-06-24 22:46:01 · 206 阅读 · 0 评论 -
leetcode Regular Expression Matching(Java)
题目链接:点击打开链接类型:双字符串处理解法:递归public class Solution { public boolean isMatch(String s, String p) { if (p.length() < 1) return s.length() == 0; if (p.length() == 1) return (s.length(原创 2017-06-13 22:11:13 · 127 阅读 · 0 评论 -
leetcode Merge k Sorted Lists(Java)
题目链接:点击打开链接类型:合并解法:分治/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution原创 2017-06-27 09:17:04 · 185 阅读 · 0 评论 -
leetcode Container With Most Water(Java)
题目链接:点击打开链接类型:two points解法:遍历(超时)public class Solution { public int maxArea(int[] height) { int len = height.length; int max = 0; if (len < 2) return 0; int side = 0; int curr原创 2017-06-14 20:49:58 · 122 阅读 · 0 评论 -
leetcode Longest Palindromic Substring(Java)
题目链接:点击打开链接题目类型:字符串处理解法:暴力遍历,但是超时了。。。public class Solution { private String curr; public int isPalidrome(int start, int end) { int i = start; int j = end; while (i < j) { if (原创 2017-06-09 22:24:41 · 132 阅读 · 0 评论 -
leetcode 4Sum(Java)
题目链接:点击打开链接类型:数学处理解法:public class Solution { public List> fourSum(int[] nums, int target) { List> result = new ArrayList>(); int len = nums.length; if (nums == null || len < 4) retu原创 2017-06-21 10:46:26 · 157 阅读 · 0 评论 -
leetcode Median of Two Sorted Arrays(Java)
题目链接:点击打开链接类型:合并数组解法:遍历数组,排列生成新的数组public class Solution { public double findMedianSortedArrays(int[] nums1, int[] nums2) { int len1 = nums1.length; int len2 = nums2.length;原创 2017-06-08 16:01:23 · 115 阅读 · 0 评论 -
leetcode Roman to Integer(Java)
题目链接:点击打开链接类型:数学处理解法:public class Solution { public int romanToInt(String s) { int len = s.length(); int[] nums = new int[len]; for(int i = 0 ; i < len ; ++i原创 2017-06-16 10:49:07 · 158 阅读 · 0 评论 -
leetcode Longest Common Prefix(Java)
题目链接:点击打开链接类型:字符串处理解法:找到最短的字符串长度,依次进行处理public class Solution { public String longestCommonPrefix(String[] strs) { if (strs.length < 1) return ""; int min_l原创 2017-06-16 13:21:01 · 120 阅读 · 0 评论 -
leetcode Two Sum(Java)
题目链接:https://leetcode.com/problems/two-sum/#/description遍历问题暴力解法:遍历数组,算法复杂度O(n²)public class Solution { public int[] twoSum(int[] nums, int target) { int[] test = {0,0}; for(原创 2017-06-05 16:19:50 · 145 阅读 · 0 评论 -
leetcode 3Sum(Java)
题目链接:点击打开链接类型:数学处理解法:public class Solution { public List> threeSum(int[] nums) { List> result = new ArrayList>(); if (nums == null || nums.length < 3) return result;原创 2017-06-17 23:19:16 · 164 阅读 · 0 评论 -
leetcode 3Sum Closest(Java)
题目链接:点击打开链接类型:数学处理解法:先排序,再比较。public class Solution { public int threeSumClosest(int[] nums, int target) { if (nums == null || nums.length < 3) return 0; Arrays.sort(nums); int le原创 2017-06-18 10:55:17 · 155 阅读 · 0 评论 -
leetcode Add Two Numbers(Java)
题目链接:点击打开链接类型:数学运算关键字:倒序相加、进位/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class原创 2017-06-06 16:17:45 · 171 阅读 · 0 评论 -
leetcode Longest Substring Without Repeating Character(Java)
题目链接:点击打开链接类型:解法:记录每一子串起始位置和长度,并与当前最大长度进行比较public class Solution { public int lengthOfLongestSubstring(String s) { if (s.length() == 0) return 0; if (s.length() == 1) return 1; int max原创 2017-06-07 19:36:28 · 120 阅读 · 0 评论 -
leetcode Letter Combinations of a Phone Number(Java)
题目链接:点击打开链接类型:字符串处理解法:建立词典库,将新来的每个数字字符对应的多个字符加到每个结果字符串中public class Solution { public List letterCombinations(String digits) { List s = new LinkedList<>(); String[] map = {"","","abc","d原创 2017-06-19 10:46:40 · 168 阅读 · 0 评论 -
leetcode Remove Nth Node From End of List(Java)
题目链接:点击打开链接类型:Remove Nth Node From End of List解法:/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; }原创 2017-06-20 18:02:57 · 140 阅读 · 0 评论 -
leetcode Swap Nodes in Pairs(Java)
题目链接:点击打开链接类型:链表解法:递归/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution原创 2017-06-27 14:26:42 · 220 阅读 · 0 评论