leetCode
qustJHJ
嵌入式学习,硬件电路学习
展开
-
findMedianSortedArrays 寻找中位数
c语言版本运行时间35ms,python版本95ms,参考http://blog.csdn.net/yutianzuijin/article/details/11499917/,博主写的特别好。/*There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the tw原创 2017-07-14 22:22:02 · 893 阅读 · 0 评论 -
reverse digits of an integer
/*Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321click to show spoilers.Note:The input is assumed to be a 32-bit signed integer. Your function should原创 2017-07-20 11:16:06 · 409 阅读 · 0 评论 -
stringToInteger 把字符串转换为int数
/**把一个字符串转换为整型值:* 开始位置的空格, 去掉* 符号位 ,记录* 字符串中的非法字符,直接退出并返回先前值* 转换后的结果是否越界,返回最大值或最小值**/#include #include #define INT_MAX 2147483647#define INT_MIN -2147483648int myAtoi(char*原创 2017-07-20 16:06:41 · 350 阅读 · 0 评论 -
removeNthFromEnd
/*Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, th原创 2017-07-29 11:54:47 · 770 阅读 · 0 评论 -
valid parentheses
使用栈来解决:/*Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are原创 2017-07-29 17:18:42 · 345 阅读 · 0 评论 -
palindromeNumber
/*Determine whether an integer is a palindrome. Do this without extra space.判断一个整型数是否是回文,回文关于中心对称;如果一个整形数是回文即说明这个数的正序与反序是相等的。特殊的情况: 负数不是回文 如果整数不为零且个位数如果为零也不是回文*/#include int isPalindrome原创 2017-07-20 20:20:47 · 457 阅读 · 0 评论 -
merge two list
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode { int val; struct ListNode *next; }; #define INT_MI原创 2017-07-29 20:28:01 · 484 阅读 · 0 评论 -
generate parentheses
# -*- coding: utf-8 -*-'''Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:[ "((()))", "(()(原创 2017-07-29 21:23:56 · 400 阅读 · 0 评论 -
swap nodes in pairs
/*Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. You原创 2017-07-30 14:05:21 · 300 阅读 · 0 评论 -
reverse nodes in k-group
/*Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.k is a positive integer and is less than or equal to the length of the linked list. If the number原创 2017-07-30 15:20:44 · 363 阅读 · 0 评论 -
regular expression matching 正则匹配
/*Implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input原创 2017-07-21 15:54:19 · 589 阅读 · 0 评论 -
remove duplicates from sorted array
/*Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place wi原创 2017-07-30 19:20:21 · 300 阅读 · 0 评论 -
implement strStr
/*Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.寻找子串并返回第一次出现的位置,采用KMP算法来解决。KMP算法的核心是无回溯,利用next数字记录子串每次失配后应该从子串的何处继续匹原创 2017-07-30 22:34:55 · 307 阅读 · 0 评论 -
container with most water
/*Given n non-negative integers a1, a2, ..., an, where each represents a pointat coordinate (i, ai). n vertical lines are drawn such that the two endpointsof line i is at (i, ai) and (i, 0). Find t原创 2017-07-21 20:42:56 · 346 阅读 · 0 评论 -
divided two integers
/*Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.思路: 通过移位运算来计算倍数 右移n位是2^n倍溢出条件: 1.divisor =0; 除数为0 2.dividend=INT_MIN && di原创 2017-07-31 11:31:41 · 320 阅读 · 0 评论 -
Substring with Concatenation of All Words 滑动窗口方法 串联所有单词
# *- coding:utf-8 -*'''这道题感觉难度不小,想了好长时间,没有找到最优解法,看到大神们用一种滑动窗口方法,确实经典。滑动窗口思路: 一个窗口长度是恒定的(与一个单词长度相等),滑动一次窗口的长度,查看目前 窗口组成的word是否在words中,并要保证word只出现一次,记录状态;如果word满足条件, 就继续向右移动窗口得到新的word重原创 2017-07-31 20:49:21 · 406 阅读 · 0 评论 -
longestPalindrome 最长回文串
/**中心扩散法:* 如果中心字符串s是回文,那么以中心对称的字符串向左右两边扩展s1,如果左右两边* 字符关于中心对称,那么s1也是回文,遍历所有中心点,重复上述过程,找到最长回文* 子串。* 中心对称分两种情况,奇数个字符以某个字母对称;偶数个字符以中间两个字符对称* 时间复杂度:O(n^2) 空间复杂度:O(1)*/#include #in原创 2017-07-19 17:47:50 · 629 阅读 · 0 评论 -
zigzag convert
/**zigzag covnert*/#include #include #include /*n=numRowsΔ=2n-2 1 2n-1 4n-3Δ= 2 2n-2 2n 4n原创 2017-07-20 09:30:20 · 231 阅读 · 0 评论 -
31.Next Permutation 下一个排列
这里写代码片- coding:utf-8 -class Solution(object): def nextPermutation(self, nums): “”” :type nums: List[int] :rtype: void Do not return anything, modify nums in-place inst原创 2017-08-01 15:52:29 · 831 阅读 · 0 评论 -
32. Longest Valid Parentheses
# *- coding:utf-8 -*'''Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.For "(()", the longest valid parenthese原创 2017-08-01 22:10:09 · 213 阅读 · 0 评论 -
44. Wildcard Matching
/*Implement wildcard pattern matching with support for '?' and '*'.'?' Matches any single character.'*' Matches any sequence of characters (including the empty sequence).The matching should cove原创 2017-08-16 11:30:11 · 203 阅读 · 0 评论 -
33. Search in Rotated Sorted Array
/*Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in原创 2017-08-02 10:08:33 · 262 阅读 · 0 评论 -
45. Jump Game II
/*Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your goal is原创 2017-08-16 15:18:27 · 201 阅读 · 0 评论 -
34. Search for a Range
/*Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If the ta原创 2017-08-02 14:51:27 · 277 阅读 · 0 评论 -
35. Search Insert Position
/*Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array原创 2017-08-02 15:50:44 · 323 阅读 · 0 评论 -
46. Permutations
/*Given a collection of distinct numbers, return all possible permutations.For example,[1,2,3] have the following permutations:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]原创 2017-08-16 20:37:46 · 177 阅读 · 0 评论 -
47. Permutations II
/*Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2] have the following unique permutations:[ [1,1,2], [1,2,1], [2,1,1]原创 2017-08-16 22:28:45 · 164 阅读 · 0 评论 -
36. Valid Sudoku
# *- coding:utf-8 -*'''Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with thecharacter '.'.A原创 2017-08-02 21:29:56 · 212 阅读 · 0 评论 -
48. Rotate Image
/*矩阵顺时针旋转90度,找到旋转前后对应的行列坐标关系,然后一圈一圈的往里面旋转 坐标旋转对应的关系:坐标(i,j)--->(j,n-1-i), ---> (i,j)-------- | | | | 1 2 3 | | |(n-1-j,i)-->4原创 2017-08-17 10:02:34 · 203 阅读 · 0 评论 -
49. Group Anagrams
/*Given an array of strings, group anagrams together.For example, given: ["eat", "tea", "tan", "ate", "nat", "bat"], Return:[ ["ate", "eat","tea"], ["nat","tan"], ["bat"]]Note: All input原创 2017-08-17 17:22:28 · 210 阅读 · 0 评论 -
50. Pow(x, n)
/*Implement pow(x, n).*/#include #include using namespace std;class Solution {public: double myPow(double x, int n) { if(n==0) return 1; double res=1; unsigned long long p=n;原创 2017-08-17 20:16:44 · 186 阅读 · 0 评论 -
37. Sudoku Solver
# *- coding:utf-8 -*'''Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character '.'.You may assume that there will be only one unique solu原创 2017-08-03 20:25:14 · 209 阅读 · 0 评论 -
61. Rotate List
/*Given a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL.思路: 不能忽略k大于链表长度的情况 形成环,并对长度取余*原创 2017-08-18 10:16:58 · 195 阅读 · 0 评论 -
38. Count and Say
/*The count-and-say sequence is the sequence of integers with the first five terms as following:1. 12. 113. 214. 12115. 1112211 is read off as "one 1" or 11.11 is read off原创 2017-08-04 11:06:42 · 356 阅读 · 0 评论 -
intToRoman
# -*- coding: utf-8 -*-'''把阿拉伯数字转换为罗马数字,采用查表法罗马数字的定义:(维基百科)罗马数字共有7个,即Ⅰ(1)、Ⅴ(5)、Ⅹ(10)、Ⅼ(50)、Ⅽ(100)、Ⅾ(500)和Ⅿ(1000)。按照下述的规则可以表示任意正整数。需要注意的是罗马数字中没有“0”,与进位制无关。一般认为罗马数字只用来记数,而不作演算。重复数次:一个罗马数字重复几次,就表示这个原创 2017-07-27 08:53:05 · 554 阅读 · 0 评论 -
romanToInt
def romanToInt(self, s): """ Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999 :type s: str :rtype: int原创 2017-07-27 10:04:30 · 378 阅读 · 0 评论 -
82. Remove Duplicates from Sorted List II
/*Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1-原创 2017-08-18 14:32:17 · 246 阅读 · 0 评论 -
83. Remove Duplicates from Sorted List
/*Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.思路: 当一个值重复出现时,只保留第一次出现的原创 2017-08-18 14:54:23 · 172 阅读 · 0 评论 -
86 Partition List
/*Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each原创 2017-08-18 16:24:02 · 229 阅读 · 0 评论 -
longestCommonPrefix
/*Write a function to find the longest common prefix string amongst an array of strings.运行代码一直提示 runtime error ,并且在试图[]时错误,刚开始以为strs为空,判断为NULL就返回NULL,还是相同的错误!查看别人的代码,发现忽视了空字符串的情况"",空字符串是分配存储空间的,而原创 2017-07-27 14:07:52 · 404 阅读 · 0 评论