
LeetCode
tingfenghanlei
这个作者很懒,什么都没留下…
展开
-
LeetCode-02两数相加
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* a...原创 2018-11-06 10:42:46 · 300 阅读 · 0 评论 -
Leetcode—19 删除链表的倒数第N个节点
链表我还有点薄弱,这题虽然是中等题,但是我还是参考别人的。https://blog.youkuaiyun.com/qq_39241239/article/details/82319620C语言版本,写的挺通俗易懂的/** * Definition for singly-linked list. * struct ListNode { * int val; * struct...原创 2019-01-05 21:27:45 · 824 阅读 · 0 评论 -
LeetCode-17
这次的都不是我写的,感觉有点烦,就没写,下次还是会自己写一遍。C版本的比较麻烦,C++的简洁易懂,推荐大家都看下C++的。 C版本char** letterCombinations(char* digits, int* returnSize) { char *numLetter[10] ={ "", "", "abc", ...原创 2018-12-18 12:35:36 · 674 阅读 · 0 评论 -
Leetcode-18四数之和
C语言版本这题和三数之和原理一样,但是值得注意的是我这把首先申请内存的时候,如果按照最大内存申请,程序显示超时,改成size=1000,就能通过。说明内存申请上面也是可以优化的。/** * Return an array of arrays of size *returnSize. * Note: The returned array must be malloced, assume...原创 2018-12-23 13:15:06 · 540 阅读 · 0 评论 -
Leetcode-16最接近的三数之和
我写的int compare(const void * a, const void * b){ return (*(int *)a - *(int *)b);}int threeSumClosest(int* nums, int numsSize, int target) { int res; int flag1 = 0,flag2 =0; int pl...原创 2018-12-16 17:24:11 · 555 阅读 · 0 评论 -
Leetcode-13罗马数字转整数
这题主要考虑几个特殊情况。C语音版本int romanToInt(char* s) { int sum; int len = strlen(s); for (int i = 0; i<len ; i++) { if(s[i]=='C') { if(s[i+1]=='D') ...原创 2018-12-09 11:56:37 · 596 阅读 · 0 评论 -
Leetcode-15三数之和
C语音版本,暴力版本,这是我开始写的版本。但是时间和空间都不会通过。void quickSort(int* nums,int first,int end){ int temp,l,r; if(first>=end)return; temp=nums[first]; l=first;r=end; while(l<r){ w...原创 2018-12-14 10:21:41 · 587 阅读 · 0 评论 -
Leetcode-12整数转罗马数字
开始我以为不能用strcat这个函数,还另外写了这个函数。C版本,我感觉C语言写涉及到字符串的东西就很麻烦。int getLength(int num){ int i = 0; while(num) { i++; num = num /10; } return i;}char *getChar1(int n...原创 2018-12-06 17:11:58 · 657 阅读 · 0 评论 -
Leetcode-11盛最多水的容器
C语言版本,暴力搜索,写的比较快。int MIN(int a, int b){ return a>b?b:a;}int maxArea(int* height, int heightSize) { int max = 0; int temp = 0; for (int i = 0;i < heightSize; i++) { ...原创 2018-12-06 10:55:27 · 656 阅读 · 0 评论 -
LeetCode-14最长公共前缀
C语言版本char* longestCommonPrefix(char** strs, int strsSize) { int len = 0; int temp = 0; int num = 0; len = strlen(strs[0]); for(int i = 1; i<strsSize; i++) { temp ...原创 2018-12-12 16:00:28 · 587 阅读 · 0 评论 -
Leetcode-10正则表达式匹配
这个C语言版本花了一点时间,主要因为两个方面:1. 变量定义数组的长度,需要事先申请内存2. 题目本身的逻辑,看了个Java改编的C版本,好像Java版本本来也有点问题。/*1, If p.charAt(j) == s.charAt(i) : dp[i][j] = dp[i-1][j-1];2, If p.charAt(j) == '.' : dp[i][j] = dp[i-1...原创 2018-12-05 17:10:26 · 806 阅读 · 0 评论 -
Leetcode-08字符串转换整数 (atoi)
C++代码和C代码差不多,上C++代码推荐一个网页https://www.cnblogs.com/grandyang/p/4606334.htmlclass Solution {public: int myAtoi(string str) { if (str.empty()) return 0; int sign = 1, base = 0, ...原创 2018-12-03 10:59:56 · 951 阅读 · 0 评论 -
LeetCode-07 整数反转
C语言版本和C++版本几乎一样,就直接上C语言版本。int reverse(int x) { int temp = x; int y; int sum = 0; while(temp!=0) { y = temp % 10; temp = temp / 10; if(sum>...原创 2018-11-28 17:09:17 · 575 阅读 · 0 评论 -
Leetcode-09回文数
C语音版本int getLength(int x){ int i = 0; while(x){ x = x / 10; i++; } return i;}bool isPalindrome(int x) { int len = getLength(x); char *y = (char*)malloc(s...原创 2018-12-04 11:23:58 · 610 阅读 · 2 评论 -
LeetCode-06 Z字形变换
C++版本class Solution {public: string convert(string s, int numRows) { if (numRows <= 1) return s; string res = ""; int size = 2 * numRows - 2; for (int i = 0; i ...原创 2018-11-26 14:24:43 · 468 阅读 · 0 评论 -
LeetCode-05最长回文子串
C++class Solution {public: string longestPalindrome(string s) { if (s.empty()) return ""; int dp[s.size()][s.size()] = {0}, left = 0, right = 0, len = 0; for (int i = 0...原创 2018-11-16 16:11:14 · 397 阅读 · 0 评论 -
LeetCode-4两数相加
C版本int getMax(int a, int b){ return a>=b?a:b;}int getMin(int a, int b){ return a>=b?b:a;}double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) { ...原创 2018-11-14 14:01:17 · 376 阅读 · 0 评论 -
LeetCode 03-无重复字符的最长子串
C版本1 int getmax(int a,int b);int lengthOfLongestSubstring(char* s) { int nowlength=0,maxlength=0; int i,j; int begini=0,endi=0; while(s[endi]!=NULL&&s[endi+1]!=NULL) ...原创 2018-11-07 18:59:52 · 377 阅读 · 0 评论 -
LeetCode-20 有效的括号
好久不写LeetCode,感觉都有点生疏了C版本1:bool isValid(char* s) { char stack[1000000]; int top=-1; while(*s){ if(*s==')'){ if(top>=0 && stack[top]=='(')top--; ...原创 2019-01-31 17:14:27 · 367 阅读 · 0 评论