Skip to content

Conversation

ziennaa
Copy link

@ziennaa ziennaa commented Oct 11, 2025

Pull Request Template

Description

Please include a summary of the problem and about the approach to solve the solution. Please also include relevant motivation and context.
List any dependencies that are required for this change.

This PR adds the implementation for LeetCode Problem 179 – Largest Number.
The goal is to arrange a list of non-negative integers such that they form the largest possible number when concatenated.

Approach Summary

  • Convert all integers to strings.
  • Sort them using a custom comparator (cmp) which ensures that for any two strings a and b, the concatenation a+b is lexicographically greater than b+a.
  • Concatenate the sorted strings to form the final result.
  • Handle the edge case where all numbers are 0.

Motivation

To contribute a clean, optimized, and readable solution using modern C++ STL features that runs efficiently and adheres to the project’s code style.

Dependencies

No new dependencies were added — uses only <vector>, <string>, and <algorithm>.

Code Snippet

class Solution {
public:
    static bool cmp(string a, string b) {
        return a + b > b + a;
    }

    string largestNumber(vector<int>& nums) {
        vector<string> s;
        for (int i : nums)
            s.push_back(to_string(i));

        sort(s.begin(), s.end(), cmp);

        string res = "";
        for (string &str : s)
            res += str;

        // handle case like [0, 0]
        if (res[0] == '0') return "0";
        return res;
    }
};

Put check marks:

Have you made changes in README file ?

  • Added problem & solution under correct topic.
  • Specified Space & Time complexity.
  • Specified difficulty level, tag & Note(if any).

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Please also note any relevant details for your test configuration.
The function was tested locally with multiple input cases to verify correctness and stability.

Test Cases

  • [10, 2]"210"
  • [3, 30, 34, 5, 9]"9534330"
  • [0, 0]"0"

Test Config

  • Compiler: g++ 17

  • Environment: Ubuntu 24.04 / Windows 10

  • No external dependencies

  • Test A – basic valid inputs

  • Test B – edge case with multiple zeros


Make sure all below guidelines are followed else PR will get Reject:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code so that it is easy to understand
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • Any dependent changes have been merged and published in downstream modules

Copy link

welcome bot commented Oct 11, 2025

I can tell this is your first pull request! Thank you I'm so honored. 🎉🎉🎉 I'll take a look at it ASAP!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant