- 博客(23)
- 收藏
- 关注
原创 算法概论课后证明8.12
8.12 k-生成树问题是这样的: 输入:无向图G=(V,E) 输出:G的一个生成树,其中所有节点度数不超过k——如果该树存在。 请证明对任意k>=2: (a)k-生成树问题是一个搜索问题。 (b)k-生成树问题是NP-完全的。 证明:(a)显然k-生成树问题是可在多项式时间内验证的,因此是搜索问题。 (b)若k=2,此时的2-生成树实际上就是一条Rudrata路径。另外
2017-06-30 13:24:55 166
原创 Verify Preorder Serialization of a Binary Tree
public class Solution { public boolean isValidSerialization(String preorder) { String[] nodes = preorder.split(","); int diff = 1; for (String node: nodes) { if (--dif
2017-06-17 12:20:09 94
原创 Next Greater Element
public class Solution { public int nextGreaterElement(int n) { char[] a=(""+n).toCharArray(); int i = a.length - 2; while (i >= 0 && a[i + 1] i--;
2017-06-17 12:18:11 84
原创 Design Twitter
public class Twitter { private static class Tweet { int tweetId; int userId; Tweet(int tweetId, int userId) { this.tweetId = tweetId; this.userId = userId; } }
2017-06-03 11:48:49 113
原创 Bulls and Cows Add to List
public class Solution { public String getHint(String secret, String guess) { int bull = 0, cow = 0; int[] sarr = new int[10]; int[] garr = new int[10];
2017-05-27 14:26:28 84
原创 Super Ugly Number Add to List
class Solution(object): def nthSuperUglyNumber(self, n, primes): """ :type n: int :type primes: List[int] :rtype: int """ ugly = [1] * n
2017-05-22 15:22:26 130
原创 原来忘了发 Word Search
class Solution(object): def exist(self, board, word): """ :type board: List[List[str]] :type word: str :rtype: bool """ visited = {}
2017-05-15 12:53:08 80
原创 原来忘了发。Validate Binary Search Tree
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object):
2017-05-15 12:50:31 88
原创 ZigZag Conversion
class Solution(object): def convert(self, s, numRows): """ :type s: str :type numRows: int :rtype: str """ if numRows == 1 or numRows >= len(
2017-05-15 12:45:31 78
原创 之前忘了发。4Sum
class Solution(object): def fourSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[List[int]] """ nums.sort()
2017-05-15 12:42:44 72
原创 之前每周都有做,忘了发。Generate Parentheses
class Solution(object): def generateParenthesis(self, n): """ :type n: int :rtype: List[str] """ def generate(p, left, right, parens=[]):
2017-05-15 12:35:51 98
原创 之前每周都有做,但没发博客。 Integer to Roman Add to List
class Solution(object): def intToRoman(self, num): """ :type num: int :rtype: str """ M = ["", "M", "MM", "MMM"]; C = ["", "C", "CC", "CCC",
2017-05-15 12:31:28 96
原创 之前每周都有做,但是忘了发博客。 Container With Most Water
class Solution(object): def maxArea(self, height): """ :type height: List[int] :rtype: int """ L, R, width, res = 0, len(height) - 1, len(height) - 1,
2017-05-15 12:27:50 81
原创 之前每星期都有做,但是忘了发博客。Letter Combinations of a Phone Number
之前每星期都有做,但是忘了发博客。 class Solution(object): def letterCombinations(self, digits): """ :type digits: str :rtype: List[str] """ if '' == digits: return [
2017-05-15 12:23:00 90
原创 Swap Nodes in Pairs
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: Li
2017-03-26 17:31:45 144
原创 算法作业33
class Solution { public: int search(vector& nums, int target) { int l = 0, r = nums.size()-1; while (l int mid = (l+r) / 2; if (target == nums[mid])
2017-03-19 15:08:09 135
原创 Jump Game II
int n = nums.size(), i = 0, l = 0, k = 0; while (k i++; intj = k + 1; for(int i = l; i if (i + nums[i]
2017-03-11 14:04:28 112
原创 Longest Substring Without Repeating Characters
class Solution { public: int lengthOfLongestSubstring(string s) { int present[256] ; std::fill_n(present, 256, -1); int start, len, max_len; int i; for (start=
2017-03-05 19:10:54 93
原创 Longest Substring Without Repeating Characters
class Solution { public: int lengthOfLongestSubstring(string s) { int present[256] ; std::fill_n(present, 256, -1); int start, len, max_len; int i; for (start=
2017-03-05 19:09:06 90
原创 算法作业7
int reverse(int x) { long long a = 0; while(x) { a = a*10 + x%10; x /= 10; } return (aINT_MAX) ? 0 : a; }
2017-02-26 23:57:37 63
原创 算法作业7
int reverse(int x) { long long a = 0; while(x) { a = a*10 + x%10; x /= 10; } return (aINT_MAX) ? 0 : a; }
2017-02-26 23:56:49 77
原创 算法作业7
int reverse(int x) { long long a = 0; while(x) { a = a*10 + x%10; x /= 10; } return (aINT_MAX) ? 0 : a; }
2017-02-26 23:49:23 42
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人