Arrange an Array to Form a Smallest Digit
/**
* Input an array of positive integers, arrange the integers to form new digits,
* and output the smallest digit among all the new ones.
* Input Example 1:
* {2, 1}
* Output Example 1:
* 12
*
* Input Example 2:
* {32, 321}
* Output Example 2:
* 32132
*
* Input Example 3:
* {4589, 101,41425,9999}
* Output Example 3:
* 1014142545899999;
*/ // 功能:将输入的数组排成最小的数
// 输入: int a[]:整型数组
// int nCount:数组长度
// char * strRst 返回值
// 输出:
// 返回:成功返回0 异常返回-1 #include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm> using namespace std; bool cmp(string str1, string str2)
{
return (str1 + str2 < str2 + str1);
} int smallestDigit(int a[], int nCount, char *strRst)
{
if(NULL == a || nCount <= 0 || NULL == strRst)
{
return -1;
} vector<string> numStrs;
stringstream strStream;
string num;
/*! int转string */
for(int i = 0; i < nCount; ++i)
{
strStream << a[i];
strStream >> num;
strStream.clear();
numStrs.push_back(num);
} sort(numStrs.begin(), numStrs.end(), cmp); int len = 0;
for(int i = 0; i < numStrs.size(); ++i)
{
len = numStrs[i].size();
strncpy(strRst, numStrs[i].c_str(), len);
strRst += len;
}
*strRst = '\0'; return 0;
} int main()
{
int a[] = {4589, 101, 41425, 9999};
char buf[1000];
smallestDigit(a, 4, buf);
cout << buf << endl; return 0;
}
Arrange an Array to Form a Smallest Digit的更多相关文章
- 5403. Find the Kth Smallest Sum of a Matrix With Sorted Rows
You are given an m * n matrix, mat, and an integer k, which has its rows sorted in non-decreasing or ...
- [LeetCode] Find K-th Smallest Pair Distance 找第K小的数对儿距离
Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pai ...
- [Swift]LeetCode719. 找出第 k 小的距离对 | Find K-th Smallest Pair Distance
Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pai ...
- [Swift]LeetCode908. 最小差值 I | Smallest Range I
Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and ...
- [Swift]LeetCode910. 最小差值 II | Smallest Range II
Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and ad ...
- LeetCode 908 Smallest Range I 解题报告
题目要求 Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K ...
- [LeetCode] 561. Array Partition I_Easy tag: Sort
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...
- [LeetCode&Python] Problem 908. Smallest Range I
Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and ...
- 解题报告-908. Smallest Range I
题目 : Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K ...
随机推荐
- windows安装zabbix监控
在windows下安装zabbix agent,方法非常简单. 首先到zabbix官方下载windows版本agent,地址:http://www.zabbix.com/download.php,找到 ...
- spring-data-jpa初步认识
什么是spring data jpa? spirng data jpa是spring提供的一套简化JPA开发的框架,按照约定好的[方法命名规则]写dao层接口,就可以在不写接口实现的情况下,实现对数据 ...
- SQL记录-PLSQL基本语法与数据类型
PL/SQL基本语法 PL/SQL是一种块结构的语言,这意味着PL/SQL程序被划分和编写代码的逻辑块.每块由三个子部分组成: S.N. 段和说明 1 声明 此部分开头使用关键字DECLARE.它是一 ...
- 浅谈cookie与session的区别
cookie用的是在客户端保持状态的方案(它是在用户端的会话状态的存贮机制),前端也可以来设置他 所有浏览器都识别,并且会缓存在浏览器中. cookie是以key=value这种键值对的形式保存,每个 ...
- FCN 32分析:
FCN 32s
- luogu P3760 [TJOI2017]异或和
传送门 对于每个二进制位考虑有多少区间和这一位上为1 从前往后扫每个前缀和,如果当前这个前缀和某一个二进制位上为1,因为区间和由这个前缀和减去前面的前缀和得来,如果减去了这一位为0的前缀和,那么 减去 ...
- Synchronized和lock的区别和用法
一.synchronized和lock的用法区别 (1)synchronized(隐式锁):在需要同步的对象中加入此控制,synchronized可以加在方法上,也可以加在特定代码块中,括号中表示需要 ...
- mysql 原理 ~ 二阶段提交协议通说
一 简介: 今天是第二篇,讲解的是mysql的事务日志 二 具体 1 WAL技术(先写日志,再写磁盘) 2 binlog redolog 二阶段提交协议 目的 保持 redo log和binl ...
- mysql 查询优化 ~explain解读之type的解读
一 简介:今天咱们来聊聊explain中type的相关解读 二 类型: system: 表中只有一条数据. 这个类型是特殊的 const 类型. const: 针对主键或唯一索引的等值查询扫描, 最 ...
- Eclipse文件路径
经常我们需要读取某个文件,一般情况下,在Eclipse工程中,路径为./src/....