[心得]面试题分析与整理4
来源:互联网 发布:java简单多线程 编辑:程序博客网 时间:2024/05/22 06:53
13.求单链表环路的起点
此为面试金典126题
思路是一快一慢,相遇后同步移动至相等。
NodeLink getRingStart(NodeLink head){ if(!head) return 0; NodeLink fast, slow; fast = slow = head; while(fast && fast->next) { slow = slow->next; fast = fast->next->next; if(slow == fast) break; } if(!fast) return slow; else if(!fast->next) return fast; slow = head; while(slow != fast) { slow = slow->next; fast = fast->next; } return fast;}
14.给定N个数,找出超过一半的一个数。此为编程之美129页原题。
int findMorethanHalf(int *a, int n){ if(n<0) return 0; int res = a[0]; int times = 0; for(int i=0;i<n;++i) { if(!times) { res = a[i]; ++times; } else { if(a[i]==res) ++times; else --times; } } return res;}
扩展问题,如果有出现超过1/4
则需要至少保存3个数字
15.最长连续子序列之和
如果是环形
拆解成2n的数组来求解
编程金典318页,编程之美183页
#include <iostream>using namespace std;void longestSubSeq(int *a, int n);int main(){ int a[]={1,-1,2,-3,4,-5,6,-7}; longestSubSeq(a, 8); return 0;}void longestSubSeq(int *a, int n){ int thisSum, maxSum; thisSum = maxSum = 0; for(int i=0;i<n;++i) { thisSum+=a[i]; if(thisSum>maxSum) maxSum = thisSum; else if(thisSum<0) thisSum=0; } cout<<maxSum<<endl;}
16.字符串按字母顺序排序,要求不用库函数。
#include <iostream>#include <cstring>using namespace std;void sortString(char *str, int n);int main(){ char str[]="terminalm"; sortString(str, 10); return 0;}void sortString(char *str, int n){ int cnt[26]; for(int i=0;i<26;i++) cnt[i]=0; for(int i=0;i<n;i++) if(str[i]>='a' && str[i]<='z') ++cnt[str[i]-'a']; for(int i=0;i<26;i++) if(cnt[i]) for(int j=cnt[i];j>0;--j) cout<<(char)('a'+i); cout<<endl;}
目前的解法没有考虑大小写问题
后续可以改进
0 0
- [心得]面试题分析与整理4
- [心得]面试题分析与整理
- [心得]面试题分析与整理2
- [心得]面试题分析与整理3
- [心得]面试题分析与整理5
- [心得]面试题分析与整理6
- [心得]面试题分析与整理7
- php面试题收集与整理(一)
- php面试题收集与整理(二)
- php面试题收集与整理(三)
- 2016 JAVA与Android面试题整理
- 2016 JAVA与Android面试题整理
- php面试题收集与整理(一)
- JAVA与Android面试题整理
- 2016 JAVA与Android面试题整理
- 2016 JAVA与Android面试题整理
- 2016 JAVA与Android面试题整理
- JAVA与Android面试题整理 2016
- 碼農不是一天練成的
- 网络协议分析工具Wireshark的使用、用Wireshark观察ARP协议以及ping命令的工作过程分析以太网报文格式
- poj 3087 Shuffle'm Up(模拟)
- 挚爱的篮球
- HDU2016--数据的交换输出
- [心得]面试题分析与整理4
- 数据库查询操作(DQL)
- 【Qt】]两种解决Qt5显示中文乱码的方法
- 用java判断url是否有效
- Android基础 Android AsyncTask使用注意事项以及总结
- struct stat 简介
- 求和为s的连续正整数序列
- Android中运行时配置环境的改变
- va_start的理解