Largest Number || LeetCode
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 1000
int cmp(char *s1,char *s2){
char temp1[];
char temp2[];
strcpy(temp1,s1);
strcat(temp1,s2);
strcpy(temp2,s2);
strcat(temp2,s1);
if(strcmp(temp1,temp2)<)return ;
else return ;
}
char* into_string(int n){
char *p;
p=(char*)malloc(*sizeof(char));
sprintf(p,"%d",n);
return p;
}
char* largestNumber(int* nums, int numsSize) {
char re[][];
char temp[];
char *p;
int i,j;
p=(char*)malloc(MAX*sizeof(char));
for(i=;i<numsSize;i++)strcpy(re[i],into_string(nums[i]));
for(i=;i<numsSize;i++){ //用的冒泡,不好意思@@ 虽然这是这题的核心算法,思想就是判断两个字串要不要交换位置的条件是,将这两个字串分别以两种方式拼接,再比较哪一 //种拼接更优
for(j=i+;j<numsSize;j++)
if(cmp(re[i],re[j])){
strcpy(temp,re[i]);
strcpy(re[i],re[j]);
strcpy(re[j],temp);
}
}
for(i=,p[]='\0';i<numsSize;i++)
strcat(p,re[i]);
while(*p=='')p++;
if(*p=='\0')return "";
return p;
}
Largest Number || LeetCode的更多相关文章
- Largest Number——LeetCode
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- [LeetCode] Largest Number 最大组合数
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- JavaScript中sort方法的一个坑(leetcode 179. Largest Number)
在做 Largest Number 这道题之前,我对 sort 方法的用法是非常自信的.我很清楚不传比较因子的排序会根据元素字典序(字符串的UNICODE码位点)来排,如果要根据大小排序,需要传入一个 ...
- Leetcode:Largest Number详细题解
题目 Given a list of non negative integers, arrange them such that they form the largest number. For e ...
- [LeetCode][Python]Largest Number
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/largest ...
- LeetCode之“排序”:Largest Number
题目链接 题目要求: Given a list of non negative integers, arrange them such that they form the largest numbe ...
- 【Leetcode】179. Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- leetcode 179. Largest Number 、剑指offer33 把数组排成最小的数
这两个题几乎是一样的,只是leetcode的题是排成最大的数,剑指的题是排成最小的 179. Largest Number a.需要将数组的数转换成字符串,然后再根据大小排序,这里使用to_strin ...
- [LeetCode] 179. Largest Number 最大组合数
Given a list of non negative integers, arrange them such that they form the largest number. Example ...
随机推荐
- 暴力/set Codeforces Round #291 (Div. 2) C. Watto and Mechanism
题目传送门 /* set的二分查找 如果数据规模小的话可以用O(n^2)的暴力想法 否则就只好一个一个的换(a, b, c),在set容器找相匹配的 */ #include <cstdio> ...
- python开发_mysqldb安装
在python的API上面,看到了MySQLdb,即python可以操作mysql数据库 接下来,我就把我这两天的工作给大伙絮叨絮叨: 准备条件: 1.MySQL-python-1.2.4b4.win ...
- html成绩单表格
<!DOCTYPE html> <html> <head> <meta name="generator" content="HT ...
- Codeforces Round #213 (Div. 2) B. The Fibonacci Segment
#include <iostream> #include <algorithm> #include <vector> using namespace std; in ...
- COJ500 杨老师的路径规划(MST) (我是认真的)
用LCT来维护生成树,动态加边(s,t,w)时,新建节点x,权值为边权w. 1.若s与t不连通,则连接s-x,x-t,答案+w 2.若s与t连通,找出s-t路径上的最大权w2,若w<w2,删除w ...
- Ubuntu 12.04 下安装 VirtualBox 及虚拟机winxp的安装
参考文档: http://wenku.baidu.com/view/a51ac26c9b6648d7c1c746d7.html 1.首先,先去官网(http://www.virtualbox.org) ...
- JAVA中使用FTPClient实现文件上传下载
在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件.下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在commons-net包中)实现上传下载文件 ...
- [百科] - SIP(会话发起协议)
SIP(会话发起协议)SIP是类似于HTTP的基于文本的协议.SIP可以减少应用特别是高级应用的开发时间.由于基于IP协议的SIP利用了IP网络,固定网运营商也会逐渐认识到SIP技术对于他们的深远意义 ...
- 安卓学习笔记---Activity
由于学期实训的要求,我开始学习安卓了.从本月一号开始,学了五天了.时间短,刚学到的东西容易忘,我记一下笔记. 首先是对Activity的理解.activity首先是一个java类,我们创建一个新的ac ...
- 解析数据存储MySQL
为了适应不同项目对不同感兴趣属性的解析存储,数据存储结构采用纵向的属性列表方式,即一个url页面多个属性存储多条记录方式,并且按照text,html, data,num几大典型类型分别对应存储. 创建 ...