
leetcode
doudou215960
男儿至死心如铁
展开
-
Leetcode 561 . Array Partition I 关于插入排序的疑问
这道题目很简单,排序后将偶数位相加就可以了。但要求排序时间复杂度不能为O(n^2),所以可以选择快速排序。我一开始没想到用快速排序,使用的插入排序。runcode时提示runtime error。输入为[7,3,1,0,0,6]。我在本地使用vs2017测试正常,我手写验证也没有出界,很郁闷,盼望大神指导。疑问已经解决,j值不可以为-1,也是醉了。。。。以下为代码:int arrayP...原创 2019-02-28 08:47:55 · 157 阅读 · 0 评论 -
Leetcode 449. Serialize and Deserialize BST
/*** Definition for a binary tree node.* struct TreeNode {* int val;* struct TreeNode *left;* struct TreeNode *right;* };*//** Encodes a tree to a single string. */struct s...原创 2019-04-24 12:50:25 · 129 阅读 · 0 评论 -
Leetcode 315. Count of Smaller Numbers After Self
struct BSTreeNode{ int val; struct BSTreeNode *left; struct BSTeeNode *right; int count;};void BST_insert(struct BSTreeNode *root,struct BSTreeNode *p){ if(root->val>p-&g...原创 2019-04-25 14:25:52 · 141 阅读 · 0 评论 -
leetcode 46. Permutations
这道题有很多种思路,我这个是利用一个数组记录已经使用过的数字,持续深搜,当记录的数字等于numsSize时,保存,结束调用,回溯上一步。void swap(int *p,int *q){ int temp; temp=*p; *p=*q; *q=temp;}void dfs(int *item,int count,int *nums,int n,int *...原创 2019-05-01 13:32:02 · 252 阅读 · 0 评论 -
Leetcode 131的一点点思考
这道题是很典型的回溯算法题,卡了我两天,最后发现自己在sizeof认识上有误区,我把我的误区分享给大家。 char *s = "hello"; char s1[] = "hello"; int a, b; a = sizeof(s);//a=4 b = sizeof(s1);//b=6 char str[2][6] = { "hello","hel...原创 2019-05-21 08:57:32 · 244 阅读 · 0 评论 -
输入一行整数,整数之间用空格隔开,读取并计算它们的和。
代码摘自刘汝佳 算法竞赛入门经典int main() { string line; while (getline(cin, line)) { int sum = 0, x; stringstream ss(line); while (ss >> x) { sum += x; }...原创 2019-06-19 11:34:17 · 11414 阅读 · 0 评论 -
PAT 甲级 1001 A+B format
这道题很简单,值得注意的只有一点,当处在字符串末尾时不要加","#include<string>#include<iostream>#include<algorithm>using namespace std;int main(){ int a, b,count=0; cin>>a>>...原创 2019-06-16 20:07:06 · 124 阅读 · 0 评论 -
PAT 甲级 1002 A+B for polynomials
利用hash表的思想#include<iostream>#include<vector>using namespace std;typedef struct {int exp;double coef;}node;node a[11], b[11];int main() {int ka, ...原创 2019-06-17 19:56:42 · 163 阅读 · 0 评论 -
Leetcode 187. Repeated DNA Sequences
C++代码采用位运算+hashmapint hash_map[1<<20]={0};class Solution {public: vector<string> findRepeatedDnaSequences(string s) { vector<string> ret; int i,key=0,m...原创 2019-07-23 18:50:11 · 154 阅读 · 0 评论 -
leetcode 102. Binary Tree Level Order Traversal
一道很经典的BFS题目,答题思路是在BFS中设置一个for循环,直到该层输出完毕再进入下一层。用C刷Leetcode简直了,所有的数据结构都得自己实现。。。。struct Queue{ struct TreeNode *root; int front; int rear;};void initQueue(struct Queue *Q){ Q->r...原创 2019-04-28 12:14:11 · 121 阅读 · 0 评论 -
Leetcode 229. Majority Element II
这是摩尔投票问题的升级版int* majorityElement(int* nums, int numsSize, int* returnSize){ int countm=0; int countn=0; int m=0; int n=0; int fre=numsSize/3; int *ret=malloc(sizeof(int)*2...原创 2019-04-26 12:55:49 · 107 阅读 · 0 评论 -
leetcode 295 利用一个大顶堆和一个小顶堆实现
typedef struct { int *data1; int index1; int *data2; int index2;} MedianFinder;/** initialize your data structure here. */MedianFinder* medianFinderCreate() { MedianFinder *h;...原创 2019-03-29 17:45:21 · 362 阅读 · 0 评论 -
leetcode 92 C语言 代码写的很混乱
写的很混乱,见笑了,要考虑M=1的特殊情况struct ListNode* reverseBetween(struct ListNode* head, int m, int n) { struct ListNode *p,*q,*r,*pre,*s; int i=1; p=head; if (m == 1) { q = NULL; ...原创 2019-03-30 14:34:20 · 276 阅读 · 0 评论 -
leetcode 45. Jump Game II C语言
代码如下int jump(int* nums, int numsSize) { int *index=malloc(sizeof(int)*numsSize); int i,max_index1,max_index2,j,count; if(numsSize==1) return 0; count=0; for(i=0;i<nums...原创 2019-04-04 12:34:24 · 217 阅读 · 0 评论 -
946. Validate Stack Sequences C语言 fasater than 100%
bool validateStackSequences(int* pushed, int pushedSize, int* popped, int poppedSize) { int top=0; int i,j,flag=1; i=j=0; int *stack=malloc(sizeof(int)*(pushedSize+2)); stack[0]=0;...原创 2019-04-04 14:11:37 · 207 阅读 · 0 评论 -
leetcode 224 实现简单计算器
编译器:VS2017语言:C基本算法网上有很多,就是用两个栈来实现。我这个算法不太好,将‘(’也入栈了,复杂了许多。我贴下源代码,供大家参考。long char2int(char *p,char *q){ long result=0; while(p!=q) { result=result*10+*p-48; p=p+1; ...原创 2019-03-27 11:49:27 · 489 阅读 · 3 评论 -
leetcode 955. Delete Columns to Make Sorted II C语言
非常变态的一道题目,极限情况很多。。。附上代码int minDeletionSize(char **A, int ASize) { int count = 0; int flag = 1; int flag1 = 1; int i, len, j, s; int a[100], top, t; top = 0; int b[50], ...原创 2019-04-07 09:09:48 · 204 阅读 · 0 评论 -
leetcode 215 C语言 利用维护一个拥有K个元素的小根堆实现
时间复杂度为logk*nstruct LittleHeap { int *data; int index; int Maxsie;};void InitHeap(struct LittleHeap *h, int len){ h->data = malloc(sizeof(int)*len); h->index = 0; h-...原创 2019-03-28 15:54:04 · 247 阅读 · 0 评论 -
leetcode 78. Subsets C语言
使用了位运算int** subsets(int* nums, int numsSize, int** columnSizes, int* returnSize) { int len = 1 << numsSize; int temp[100]; *returnSize = len; int **ret = malloc(sizeof(int*)*le...原创 2019-04-07 21:56:55 · 294 阅读 · 0 评论 -
376. Wiggle Subsequence C语言 自动状态转换机
int wiggleMaxLength(int* nums, int numsSize) { int i, length, STATE; length = 1; STATE = 0; const int BEGIN = 0; const int UP = 1; const int DOWN = 2; if (numsSize == 0)...原创 2019-04-03 09:46:01 · 134 阅读 · 0 评论 -
归并排序链表实现
class Solution {public: ListNode* sortList(ListNode* head) { if(head==NULL||head->next==NULL) return head; ListNode *pre=NULL,*slow=head,*fast=head; while(f...原创 2019-08-26 12:51:16 · 206 阅读 · 0 评论