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 ...
随机推荐
- 面向对象【day07】:类的特性介绍(四)
本节内容 1.概述 2.访问属性 3.私有属性 4.总结 一.概述 在上篇博客中我们已经讲了一些关于类的知识,我们来回顾以下: 定义类(class dog(object))-> 实例化(d = ...
- 过滤选择器first与子元素过滤选择器first-child的区别
1.表格代码如下: <table id="table"> <tr> <td>id</td> <td>name</t ...
- 用Java实现几种常见的排序算法
用Java语言实现的各种排序,包括插入排序.冒泡排序.选择排序.Shell排序.快速排序.归并排序.堆排序.SortUtil等. 插入排序: package org.rut.util.algorith ...
- 内联函数 —— C 中关键字 inline 用法解析
一.什么是内联函数 在C语言中,如果一些函数被频繁调用,不断地有函数入栈,即函数栈,会造成栈空间或栈内存的大量消耗. 为了解决这个问题,特别的引入了inline修饰符,表示为内联函数. 栈空间就是指放 ...
- vue 中this指向遇到的坑
vue中的this指向问题 如果方法中没有使用箭头函数,记得把this赋值给另一个变量再使用.
- Linux记录-筛选日志sed、find、tail,du,awk命令
1.查看某一段时间的日志 #cat hdfs-audit.log | sed -n '/2018-04-11 10:00:00/,/2018-04-11 10:01:00/ p' | more - ...
- 神级程序员通过两句话带你完全掌握Python最难知识点——元类!
千万不要被所谓"元类是99%的python程序员不会用到的特性"这类的说辞吓住.因为 每个中国人,都是天生的元类使用者 学懂元类,你只需要知道两句话: 道生一,一生二,二生三,三生 ...
- A+B (带有,的数字)
题目描述 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号","隔开. 现在请计算A+B的结果,并以正常形式输出. 输入描述: 输入包含多组数据数据,每组数据占一行,由 ...
- xss漏洞利用
简述 跨站脚本攻击(也称为XSS)指利用网站漏洞从用户那里恶意盗取信息.攻击者通过在链接中插入恶意代码,就能够盗取用户信息.攻击者通常会在有漏洞的程序中插入 JavaScript.VBScript. ...
- XML之Well-Formed文档规则
由于课程原因,近日粗略学习XML,载以博客是为担心忘记,以供日后复习之用. XML标准中明确规定了XML文件应当遵守的规则,大致上分成基本规则和DTD(Document Type Definition ...