剑指Offer31 把数组排成最小的数
/*************************************************************************
> File Name: 31_SortArrayMin.cpp
> Author: Juntaran
> Mail: JuntaranMail@gmail.com
> Created Time: 2016年09月02日 星期五 11时10分42秒
************************************************************************/ #include <stdio.h>
#include <string>
#include <bits/stdc++.h> using namespace std; string itos(int x)
{
return (x> ? itos(x/) : "") + char(x% + '');
} // qsort比较函数
int cmp(const void *a, const void *b)
{
return *(int*)a - *(int*)b;
} // sort比较函数
bool compare(int a, int b)
{
return (itos(a) + itos(b)) < (itos(b) + itos(a));
} string PrintMinNumber(int* nums, int length)
{
string s = "";
if (nums==NULL || length<=)
return s;
// sort(nums, nums+length, compare);
qsort(nums, length, sizeof(int), cmp); for (int i = ; i < length; ++i)
s += itos(nums[i]);
cout << s << endl;
return s;
} int main()
{
int nums[] = {, , };
int length = ;
PrintMinNumber(nums, length); return ;
}
剑指Offer31 把数组排成最小的数的更多相关文章
- leetcode 179. Largest Number 、剑指offer33 把数组排成最小的数
这两个题几乎是一样的,只是leetcode的题是排成最大的数,剑指的题是排成最小的 179. Largest Number a.需要将数组的数转换成字符串,然后再根据大小排序,这里使用to_strin ...
- [剑指Offer]45-把数组排成最小的数
题目链接 https://www.nowcoder.com/practice/8fecd3f8ba334add803bf2a06af1b993?tpId=13&tqId=11185&t ...
- 剑指Offer——把数组排成最小的数
题目描述: 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. 分析: 排 ...
- 剑指offer--32.把数组排成最小的数
用to_string()将整形转化为字符串,对字符串进行比较 --------------------------------------------------------------------- ...
- 剑指Offer-39.把数组排成最小的数(C++/Java)
题目: 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. 分析: 将数组 ...
- 用js刷剑指offer(把数组排成最小的数)
题目描述 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. 思路 对ve ...
- 剑指offer 把数组排成最小的数 atoi和itoa,pow
pow(x,y)在#include<math.h>文件中,计算x的y次方. C++引入头文件:#include <stdlib.h> 或者 #include <cstdl ...
- 4-剑指offer: 把数组排成最小的数
题目描述 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. 代码: cl ...
- 剑指Offer - 九度1504 - 把数组排成最小的数
剑指Offer - 九度1504 - 把数组排成最小的数2014-02-06 00:19 题目描述: 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输 ...
随机推荐
- Mahalanobis Distance(马氏距离)
(from:http://en.wikipedia.org/wiki/Mahalanobis_distance) Mahalanobis distance In statistics, Mahalan ...
- javascript 汉字生成拼音
在网上下载的一个汉字生成拼音的js,很有用,大家一起分享! var PinYin = {"a":"/u554a/u963f/u9515","ai&qu ...
- vtk读取文件中点坐标[转]
vtk基础编程(2)-读取数据文件中的坐标点 1. 案例说明 在实际计算中,常常需要大量的数据, 这个时候数据文件就必不可少, 例如 数据文件points.dat, 中存放了三个点的坐标, 0.0 0 ...
- iOS 消息推送实现 APNS
本文只是记录一下如何在自己的电脑上配置APNS推送环境,其它的如推送的原理,流程什么的这里就不写了. 一. 去Apple 开发者中心,创建App ID.注意App ID不能使用通配符.并注意添加Pus ...
- EXTJS AJAX解析XML数据
public String getAllAreaInfos() { try { List<Areainfo> list = null; if(areaName!=null&& ...
- Overview and tips for using STM32F303
www.stmcu.org/download/index.php?act=down&id=5264 IntroductionThe purpose of this application no ...
- IOS学习经验总结--来自知乎网友
转自知乎:http://www.zhihu.com/question/20016551 我当时刚学iOS开发的时候一样的感觉 总想知道原理 内部怎么回事 感觉在像在雾里但是iOS开发就是这样 他是封闭 ...
- Android Recovery Ui 分析
Android recovery和android本质上是两个独立的rootfs, 仅仅是recovery这个rootfs存在的意义就是为android这个rootfs服务,因此被解释为Android ...
- Html5游戏开发开始前的一些数学基础
计算一个向量的值 var vectorMagnitude = Math.sqrt(Math.pow(vector.x, 2) + Math.pow(vector.y, 2)); 单位向量 var ve ...
- Shadow Mapping 的原理与实践(一)
早在上世纪七十年代末,Williams在他的“Casting Curved Shadows on Curved Surface”一文中提出了名为Shadow Map的阴影生成技术.之后,他人在此基础上 ...