
基础面试算法(c/c++)
c/c++面试基础算法
群野
cording
展开
-
面试算法集
求最长回文串链表的C++实现(查找中间节点、判断是否存在环)约瑟夫环链表算法二叉树的c++实现——递归的应用strToInt和intToStr的自我定义找出字符串里出现最多的第一个字母找出字符串里出现最多的第一个字母阶乘——大数存储在大字符串寻找第index次出现子串的位置(BF算法OC写法)排序之c与oc比较高级整数二进制算法c++ 动态数组的实现...原创 2019-01-22 23:56:23 · 369 阅读 · 0 评论 -
水仙花数_pyhon实现
输出100~999之间的水仙花数。水仙花数即各位数上数的立方和相加等于该数。用pyhon实现如下:原创 2022-07-05 12:43:49 · 143 阅读 · 0 评论 -
逻辑运算符‘&&’和‘||’优先级 ——找出数组里3个相加和最大的数
此处切忌不能写错 i >= 0 && nums[i] != nums[i-1]原创 2022-06-13 16:57:00 · 243 阅读 · 0 评论 -
查找链表中的中间元素
/* findMidElement *函数功能:查找链表中的中间元素 *输入:head链表头指针,p被删除元素位置 *返回值:被中间节点元素中的数据域.如果没有返回NULL */ ListNode *findMidElement(){ return findMidElement(root); } ListNode *findMidElement (ListNode *head){ if (he原创 2022-05-27 18:21:34 · 372 阅读 · 0 评论 -
验证链表是否有环
/* 验证链表是否有环,如:1->2->3->4->6->5 ↑———8<———│ 有返回1,否则返回0 */ int checkCircle(ListNode *head) { if (head == NULL) return 0; Node *p = head; Node *q = head->next;原创 2022-05-27 18:16:58 · 192 阅读 · 0 评论 -
一组整数(正负数),找出相加绝对值最小的两个数,及其绝对值
void minAbsoluteValueofsum(int a[], int n){ int minSum(INT_MAX); int minus = 0; int plus = 0; for(int i =0; i<n; i++){ for(int j=i+1; j<n; j++){ int tmpSum = a[i]+a[j]; if(tmpSum > 0){原创 2022-05-17 11:54:42 · 350 阅读 · 0 评论 -
c++合并动态数组vector,求中位数
Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.The overall run time complexity should be O(log (m+n)).Example 1:Input: nums1 = [1,3], nums2 = [2]Output: 2.00000Explanation: merged array原创 2022-05-09 15:20:36 · 1011 阅读 · 0 评论 -
求字符串最长子串
Given a string s, find the length of the longest substring without repeating characters.Example 1:Input: s = "abcabcbb"Output: 3Explanation: The answer is "abc", with the length of 3.Example 2:Input: s = "bbbbb"Output: 1Explanation: The answer i原创 2022-04-21 16:01:05 · 371 阅读 · 0 评论 -
亚马逊面试算法演练
function foo(items) {Calculate the max online people:Input:[[2, 5],[3, 7],[8, 9]]Output: 2 var i;一个网吧,给一个二维数组,每一个数组里有两个值,代表一个人开始上网和结束上网的时间,让计算最多同时有多少个人在上网。#include <iOStream>using namespace std;const int maxN = 100;int cnt[maxN];int mai原创 2022-04-15 22:44:40 · 444 阅读 · 0 评论 -
iOS Objective-C 面试整理
面试的好处如同考试,获得肯定或者知道不足。对于ios,object编程的一些面试题做一些整理,如下:1、notification是多线程的吗?通知是监听者模式在ios编程里的应用,ios 的通知机制,包括通知发布,通知监听,通知移除。通知的发布看书简单的接口调用,其实是cocoa为用户做了监听者模式的封装,我们运行时没有发现其阻塞我们的操作,发布通知时后台另启线程,接收时回调到主线程进行处理。2、在项目什么时候选择使用GCD,什么时候选择NSOperation?项目中使用NSOperation的优点原创 2022-03-18 20:07:41 · 675 阅读 · 0 评论 -
c++ 动态数组的实现
自定义动态模版数组#include <iostream>using namespace std;template <class T>class CDynamicArray { T *pData; int nExistedNum; int nMaxNum; public: CDynamicArray(); CDyna...原创 2019-01-29 19:06:05 · 553 阅读 · 0 评论 -
排序之c与oc比较
1、插入法 c写法:void insertSort(int a[ ], int n){ int i,j,p; for (i = 1; i<n; i++) { t=a[ i ]; for( j = i-1; j >= 0; j--) { if( t < a[ j ...原创 2019-01-29 18:31:04 · 145 阅读 · 0 评论 -
BF算法——在大字符串寻找第index次出现子串的位置
BF算法,即暴风(Brute Force)算法,是普通的模式匹配算法,BF算法的思想就是将目标串S的第一个字符与模式串T的第一个字符进行匹配,若相等,则继续比较S的第二个字符和 T的第二个字符;若不相等,则比较S的第二个字符和T的第一个字符,依次比较下去,直到得出最后的匹配结果。BF算法是一种蛮力算法。- (int)indexForPos:(NSString*)S with:(NSString*...原创 2019-01-28 18:37:33 · 326 阅读 · 0 评论 -
strToInt和intToStr的自我定义
#include <stdio.h>#include <stdlib.h>#include <iostream>using namespace std;char *intToStr(int &Number){ char *str = (char *)malloc(12*sizeof(char)); memset(str, 0...原创 2019-01-27 18:33:24 · 1420 阅读 · 0 评论 -
c语言输出整数二进制
二进制算法都说的除2倒转,效率极低不说,还没考虑负数情况 , 用位运算法可以解决该问题:#include <iostream>using namespace std;void printBinary(int &argc){ cin>>argc; for (int i=31;i>=0;i--)//高位到低位输出 { ...原创 2019-01-24 14:55:46 · 5794 阅读 · 0 评论 -
找出字符串里出现最多的第一个字母
字符串由字符组成,大写、小写字符的ASCII码是连续的,然而大小写的ASCII码并不是连续的。熟记’a’的ASCII码是97,'A’的ASCII码是65。如果字符串都为小写,需要26个元素来纪录各个字符出现的次数;如果考虑大小写则需要26*2个元素来纪录各个字符出现的次数;如果考虑ASCII可显示字符,则需要126-32+1个元素进行纪录,其中最新的ASCII码是32。字符串都为小写void ...原创 2019-01-23 19:24:35 · 240 阅读 · 0 评论 -
c语言大数存储——阶乘
众所周知,最大的基础数据类型long long,所占字节数为:8,所能表示的数据范围为:-9223372036854775808~+9223372036854775807,差不多1800亿亿。如果大于这个将出现数据存储越界,从而可能出现错误。所以应采用数组存储每位上的数据。void jieCheng(int N){ int a[64]; int n, i, c, p; ...原创 2019-01-23 11:00:38 · 4439 阅读 · 0 评论 -
约瑟夫环的C语言实现
约瑟夫函数:f(m, k, i)为m为总人数,报数为k,第i个出环的人的编号,分析得出:当,i=1时, f(m,k,i)=(m+k-1)%m;当,i!=1时,f(m,k,i)=( f(m,k,i)+k )%m。int JosePhus(int m, int k, int i){ if (i==1) return(m+k+1)%m; else ret...原创 2019-01-23 00:05:36 · 153 阅读 · 0 评论 -
求最长回文串
先说明一下回文数:对称的字符串即回文串,如“1”、“11”、“121”、“abba”、“ababa”等1暴力全举string longestPalindrome(string &amp;amp;amp;s){ long length = s.size(); int maxlength = 1; int start = 0; for (int i=0; i&amp;amp;lt;lengt...原创 2018-03-17 12:04:55 · 329 阅读 · 0 评论 -
C++链表的实现(链表反转、合并)
1、链表的实现struct Node { int data; Node *next;};class Link {public: Node *root; Link() { root = new Node; } /* Create *函数功能:创建链表. *输入:各节点的data ...原创 2018-03-17 11:26:37 · 2010 阅读 · 0 评论 -
二叉树的c++实现——递归的应用
1、二叉树的定义#include &lt;iostream&gt;using namespace std;struct tree { int data; tree *left, *right;};class Btree{ tree *root;public: Btree() { root = NULL; } ...原创 2018-03-08 00:10:42 · 263 阅读 · 0 评论 -
在大字符串寻找共出现几次子串的位置(KMP算法C写法)
int matchCount(char* sourceString, char* subString){ int subCnt[100]; subCnt[0] = 0; unsigned int len1 = strlen(sourceString); unsigned int len2 = strlen(subString); for (int i=1, int j=0; i <原创 2013-02-27 13:57:41 · 808 阅读 · 0 评论 -
c++快速排序
void quickSort(int a[], int low, int high){ if (low >= high) { return; } int i = low;int j = high; int pivot = a[low]; while (i if (a[i] i+原创 2013-01-16 19:40:05 · 476 阅读 · 0 评论 -
在大字符串寻找第index次出现子串的位置(BF算法OC写法)
- (int)indexForPos:(NSString*)S with:(NSString*)T and:(int)index{ intposition =0; intc =1;inti = position; while(ilength] && c intj=0; w原创 2012-12-19 22:50:02 · 691 阅读 · 0 评论