一道机试题
来源:互联网 发布:讲解spring源码的书籍 编辑:程序博客网 时间:2024/05/21 11:32
Given a sorted array, remove the duplicates in place such that each element appear only once
and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example, Given input array A = [1,1,2] ,
运行结果:
and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example, Given input array A = [1,1,2] ,
Your function should return length = 2, and A is now [1,2] .
My Method:
/*Given a sorted array, remove the duplicates in place such that each element appear only onceand return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.For example, Given input array A = [1,1,2] ,Your function should return length = 2, and A is now [1,2] .*/#include<stdio.h>#include<stdlib.h>void main(){int a[4] = { 1, 1, 2, 2, };//数组样式int len = sizeof(a)/sizeof(a[0]);<span style="white-space:pre"></span>//len为数组长度int stc_len = len;int temp;for (int i = 0; i < len - 1; i++){if (a[i] == a[i + 1])for (int j = i; j < stc_len - 1; j++){a[j] = a[j + 1];stc_len--;}}printf("数组的长度为%d\n", stc_len);for (int i = 0; i < stc_len; i++)printf("%d ", a[i]);printf("\n");system("pause");}
/*Given a sorted array, remove the duplicates in place such that each element appear only onceand return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.For example, Given input array A = [1,1,2] ,Your function should return length = 2, and A is now [1,2] .*/#include<stdio.h>#include<stdlib.h>void main(){int a[4] = { 1, 1, 2, 2, };//数组样式int len = sizeof(a)/sizeof(a[0]);//len为数组长度int index = 0;for (int i = 1; i < len; i++){if (a[index] != a[i])a[++index] = a[i];}index++;for (int i = 0; i < index; i++)printf("%d ", a[i]);system("pause");}
运行结果:
数组的长度为21 2请按任意键继续. . .
对于无序的数组应该这么做呢???
0 0
- 华为一道机试题
- 一道zhangying机试题
- 一道机试题
- 一道C机试题目
- 一道华为的机试题
- 一道试题
- 一道试题
- 一道试题
- 一道试题
- 面试的一道机试题: 排序 难度(**)
- 自己做的一道机试题
- 迅雷的一道机试题___URL解析
- 华为的一道机试题,单词倒序
- 一道关于HttpClient的机试题
- 一道微软面试题
- 一道面试题
- 华为一道面试题
- 一道面试题
- (问题记录)Failed to resolve: com.android.support:support-v4:23.1.1
- mysql Handler_read_rnd_next value 值超大(10G+)的处理
- C#之RabbitMQ系列(一)
- Linux配置启动挂载:fstab文件详解
- android菜单详解三:上下文菜单
- 一道机试题
- 最简单的FTP客户端、服务器端
- Linux下squid普通代理步骤
- hibernate中的三种状态
- spring,mybatis事务管理配置与@Transactional注解使用[转]
- java对redis的基本操作
- 静态成员(变量、函数)
- UVa-1252 Twenty Questions (位运算)
- C++面向对象高级编程(下)第二周-Geekband