Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- Stack<Integer> stack = new Stack<>();
- public String largestNumber(int[] nums) {
- for(int i = 0; i < nums.length;i++){
- int smallest = i;
- for(int j = i+1; j < nums.length; j++){
- if(compare(nums[j], nums[smallest])){
- smallest = j;
- }
- }
- int temp = nums[i];
- nums[i] = nums[smallest];
- nums[smallest] = temp;
- }
- List<Integer> list = new ArrayList<>();
- for(int i = 0; i < nums.length;i++){
- while (nums[i]>10){
- int temp = 0;
- temp = nums[i] % 10;
- nums[i] = nums[i]/10;
- stack.push(temp);
- }
- stack.push(nums[i]);
- while(!stack.isEmpty()){
- list.add(stack.pop());
- }
- }
- int result = 0;
- for(int i= 0 ; i< list.size();i++){
- result += list.get(i)*(10^(list.size()-1-i));
- }
- String strI = Integer.toString(result);
- return strI;
- }
- public boolean compare(int a,int b){
- while(a>=10){
- a= a / 10;
- }
- while (b>=10){
- b = b/10;
- }
- if(a<b) return true;
- else return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement