摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ /** * Note: The ret
阅读全文
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ /** * Note: The ret
阅读全文
摘要:/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ /** * Note: The ret
阅读全文
摘要:int evalRPN(char** tokens, int tokensSize) { int* stack=(int*)malloc(sizeof(int)*tokensSize); for(int i=0;i<tokensSize;i++) stack[i]=0; int top=-1; fo
阅读全文
摘要:char* removeDuplicates(char* s) { int ns=0; while(s[ns]!=0) ns++; if(ns<=1) return s; char* stack=(char*)malloc(sizeof(char)*ns); for(int i=0;i<ns;i++
阅读全文
摘要:typedef struct { int a[100]; int top; int size; } MyStack; MyStack* myStackCreate() { MyStack* stack=(MyStack*)malloc(sizeof(MyStack)); stack->top=0;
阅读全文
摘要:C用数组模拟即可 typedef struct { int a[100]; int front; int tail; int size; } MyQueue; MyQueue* myQueueCreate() { MyQueue* q=(MyQueue*)malloc(sizeof(MyQueue)
阅读全文
摘要:bool repeatedSubstringPattern(char* s) { int ns=0; while(s[ns]!=0) ns++; if(ns<=1) return false; bool same =true; char temp=s[0]; int i=1; for(;i<ns;i
阅读全文
摘要:#include<stdio.h> void reverse(char* s,int head,int tail){ while(head<=tail){ char temp=s[head]; s[head]=s[tail]; s[tail]=temp; head++; tail--; } } ch
阅读全文
摘要:现在解决一个中等难度的题得要30min,争取在复试的时候提升到只有15min!!!! void revesestring(char* s,int head,int tail){ while(head<=tail){ char temp=s[head]; s[head]=s[tail]; s[tail
阅读全文
摘要:#include<stdio.h> int main(){ char c[10000]; scanf("%s",c); int i=0; while(c[i]!=0){ int t=c[i]-'0'; if(0<=t&&t<=9){ printf("number"); }else{ printf("
阅读全文
摘要:void reverse_string(char* s,int head,int tail){ while(head<=tail){ char t=s[head]; s[head]=s[tail]; s[tail]=t; head++; tail--; } } char* reverseStr(ch
阅读全文
摘要:void reverseString(char* s, int sSize) { int head=0,tail=sSize-1; while(head<=tail){ char t=s[head]; s[head]=s[tail]; s[tail]=t; head++; tail--; } } 1
阅读全文
摘要:自己写了一个hash表。 原来学过的数据结构关于hash的那章还是有实际用的,不是书架子。 typedef struct node{ int sum; int count; struct node* repeatnext; }hash; void init_hashtable(hash h[]){
阅读全文
摘要:没有意识到这是一个环。 原理就是,其区间是有限的(int型变量存在最大值pow(2,31)-1),而循环却是无限的,那么一定会发生重复的情况。 如果不重复,那么和区间有限矛盾。 int c_sum(int n){ int sum=0; while(n!=0){ int t=n%10; n/=10;
阅读全文
摘要:/** * Note: The returned array must be malloced, assume caller calls free(). */ int min(int i,int j){ if(i<j) return i; return j; } int* intersect(int
阅读全文
摘要:/** * Note: The returned array must be malloced, assume caller calls free(). */ int* intersection(int* nums1, int nums1Size, int* nums2, int nums2Size
阅读全文
摘要:\ int hash(char c){ return c-'a'; } bool canConstruct(char* ransomNote, char* magazine) { if(!ransomNote) return true; if(!magazine) return false; int
阅读全文
摘要:int hash(char c){ return c-'a'; } bool isAnagram(char* s, char* t) { int a[26]={0}; int b[26]={0}; int i=0; while(s[i]!=0){ a[hash(s[i++])]++; } i=0;
阅读全文
摘要:/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode *detectCycle(struct ListNode
阅读全文
摘要:利用链表的特性,如果相交的话,后面就不可能岔开! 你可以想象把他们有同一个尾巴,然后从尾巴往前看。 所以只要知道两个链表的长度,就可以在同一起跑线上一起比较了。 /** * Definition for singly-linked list. * struct ListNode { * int va
阅读全文
摘要:/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* removeNthFromEnd(struct List
阅读全文
摘要:没必要纠结太麻烦的方法。 简单的就两种,一个就是新拿出来空间来装。 要不然就是直接前后数据换一下就行了。 然后需要考虑表空的情况。 结果: /** * Definition for singly-linked list. * struct ListNode { * int val; * struct
阅读全文
摘要:/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* reverseList(struct ListNode*
阅读全文
摘要:写了个递归 /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* delect(struct ListNode
阅读全文
摘要:/** * Return an array of arrays of size *returnSize. * The sizes of the arrays are returned as *returnColumnSizes array. * Note: Both returned array a
阅读全文
摘要:int hash(char c){ return c-'A'+1; } bool judge_Same(int a[],int b[]){ for(int i=0;i<200;i++){ if(b[i]!=0 && b[i]>a[i]) return false; } return true ; }
阅读全文
摘要:int totalFruit(int* fruits, int fruitsSize) { if(fruitsSize<=2) return fruitsSize; int a[2]={-1,-1};//蓝子空 int max=0,n=1; int head=0, tail=0;//从head摘到t
阅读全文
摘要:滑动窗口的妙用!! int minSubArrayLen(int target, int* nums, int numsSize) { int sum=nums[0];//区间head到tail的和 int head=0,tail=0; int minn=numsSize; int tag=0; i
阅读全文
摘要:学习了下用qsort解决。 /** * Note: The returned array must be malloced, assume caller calls free(). */ int cmp(const void* a,const void* b){ return *(int*)a-*(
阅读全文
摘要:这题学到了很多。 malloc后要初始化。 申请字符串要N+1个单位 字符串以0结尾等等 char* final(char* s,int n){ char* tem=(char*)malloc(sizeof(char)*(n+1)); for(int i=0;i<=n;i++) { tem[i] =
阅读全文
摘要:int removeDuplicates(int* nums, int numsSize) { if(numsSize==0||numsSize==1) return numsSize; int i=0,j=0; int pre=-999,n=0; while(j<numsSize){ if(num
阅读全文
摘要:bool isPerfectSquare(int num) { if(num==1) return true; if(num==2) return false; long head=2,tail=num-1; while(head<=tail){ long mid=(head+tail)/2; lo
阅读全文
摘要:int mySqrt(int x) { return sqrt(x); }
阅读全文
摘要:/** * Note: The returned array must be malloced, assume caller calls free(). */ int* searchRange(int* nums, int numsSize, int target, int* returnSize)
阅读全文
摘要:int searchInsert(int* nums, int numsSize, int target) { int head=0,tail=numsSize-1; while(head<=tail){ int mid=(head+tail)/2; if(nums[mid]<target){ he
阅读全文
摘要:原本我想用头尾交换的双指针的,但是又要判断头尾是否相等,感觉不干净的感觉。 就换成了类似筛选的前后双指针。一个是指向要放的位置,一个指向查看的位置。 int removeElement(int* nums, int numsSize, int val) { int i=0,j=0; while(j<
阅读全文
摘要:现在开始刷代码随想录里的题了。 int search(int* nums, int numsSize, int target) { int head=0,tail=numsSize-1; while(head<=tail){ int mid=(head+tail)/2; if(nums[mid]<t
阅读全文
摘要:今天出成绩了,感觉徘徊在被刷的边缘,要好好努力了。 这题我想法试建立hash映射成有序的数字,只要字符串个数相同,并且映射和相同那么就是异位串。 后来这个想法是错的。以为假设已经已知一个和,和组成这个和的个数,但这个子数并不唯一,比如10=1+2+7。10=2+3+5。 这样就会误判。就算能找到唯一
阅读全文
摘要:思路就是从头开始找,然后每次在从重复节点的后一个找。 class Solution { public: int lengthOfLongestSubstring(string s) { int i=0,j=0,nowmax=1; int max=1; if(s.size()==0||s.size()
阅读全文
摘要:因为还是双指针的题目。 我想到的短板效应,看两头,能接住的水也就是取决于最短的一方。 每次看两头后,在里面找比最短还短的地方,那个就是有水的地方。找到水后在把它填平,防止重复找到。然后两头往中间靠就行了。 int min(int i,int j){ if(i>j) return j; return
阅读全文
摘要:先排序,再暴力找就好了。如果当前元素大于0或者前两个元素和大于0就不用找了。然后结果超时了。 然后借鉴了双指针的解法,发现双指针其实就是把单向循环优化成双向循环。 class Solution { public: vector<vector<int>> threeSum(vector<int>& n
阅读全文
摘要:原本想o(n2)遍历的,结果超时了,果然没这么简单就解决。 class Solution { public: int s(vector<int> height,int i,int j){ int s1=min(height[i],height[j])*(j-i); return s1; } int
阅读全文
摘要:类似像一个筛选过程,如果非0就加入数组,不非0就不加。 void moveZeroes(int* nums, int numsSize) { int x=0,y=0; while(x<numsSize){ if(y>=numsSize) break; if(nums[y]!=0){ nums[x]=
阅读全文
摘要:就是让你判断输入受限的双端队列的输出的正确性。 其实就是模拟双端队列出队的过程,要不左边出队,要不右边出队,而入队已经一定了。 用一个数组模拟输入受限的双端队列就行了。 但是写这题可太难受了,写了我大概2个半小时,各种各种小错误,没考虑周全的地方。 #include<iostream> using
阅读全文
摘要:o(n)现在水平不够。 采用先快排序,再找。O(nlogn),注意每次划分枢纽选择中间节点(中间节点和首节点互换) int divide(int* nums,int head,int tail){ int x=nums[(head+tail)/2]; nums[(head+tail)/2]=nums
阅读全文
摘要:刷力扣还有点不太习惯,主要是C++只学了皮毛。 看了官方活用map就是好啊。 把字母都排好序 然后判断就好了。 map<string,vector<string>> m; for(int i=0;i<strs.size();i++){ string tem=strs[i]; sort(tem.beg
阅读全文
摘要:原本想排下序的,但要求返回下标,又感觉要添加其他东西了,太麻烦了。暴力找就好了。
阅读全文
摘要:建议直接网上看思路.... #include<stdio.h> int max(int i,int j){ if(i>j) return i; return j; } int maxlength[1001][1001]; int main(){ int n,m; while(scanf("%d %d
阅读全文
摘要:这个解决问题的思路使用动态规划,即用已知状态去得到未知状态。 思路逻辑是这样 sum[i]记录以A[i]为末上升子序列的和的最大值 然后从j 从 0-i-1 遍历 如果A[j]<A[i] 那么 sum[i]=sum[j]+A[i]; 然后找出sum[i]中的的最大值,就是以A[i]为末上升子序列的和
阅读全文
摘要:题目例子给的很好,还有不要遗漏全是负数的情况。 #include<stdio.h> #include<math.h> int main(){ long long n=0; while(scanf("%ld",&n)!=EOF){ long long sum=0; long long max=0; i
阅读全文
摘要:#include<iostream> using namespace std; int main(){ int n=0; int f[90]; f[1]=1; f[2]=2; while(cin >> n){ for(int i=3;i<=n;i++){ f[i]=f[i-1]+f[i-2]; }
阅读全文
摘要:考迪杰斯特拉算法。 #include<stdio.h> struct node{ int n1; int n2; int weight; }; typedef struct node edge; edge e[1000]; void init_dist(int dist[],int n){ for(
阅读全文
摘要:求图的最小生成树。克鲁斯卡尔算法来解决。就是选择n-1条最小边且无回路。 回路判断用并查集就行。 即要加入的边(两个节点)具有相同的父节点说明如果这两个节点本来就存在路径,再加入一条边就会产生回路,舍去。 #include<iostream> #include<algorithm> using na
阅读全文
摘要:最近实在是太偷懒了,再这样下去复试直接寄。要努努力了。 简单一点可以用并查集,不嫌麻烦DFS/BFS都可以判断是否连通。 #include<stdio.h> void swap(int* x,int* y){ if(*x >*y){ int t=*x; *x=*y; *y=t; } } int fa
阅读全文
摘要:题目看半天看不懂。 題目把我恶心坏了。看网上说按字典顺序输出,到底是什么意思半天没搞懂。 #include<iostream> #include<string> using namespace std; int d[8][2]={ {-1,-2},{1,-2},{-2,-1},{2,-1},{-2,
阅读全文
摘要:这题就是找N的倍数m,M要求是由1和0组成且非0。 可以用图来看,从1出发临边是1和0,然后广度遍历,第一个能能整除N的数输出就行。 #include<iostream> #include<queue> using namespace std; int main(){ int n=-1; while
阅读全文