GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode-algorithms/src/AddString.java at master · anishLearnsToCode/leetcode-algorithms

Expand file tree

/

AddString.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
27 lines (25 loc) · 947 Bytes
/

AddString.java

Copy path

File metadata and controls

27 lines (25 loc) · 947 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class AddString {
public static void main(String[] args) {
String number1 = "5";
String number2 = "10";
System.out.println(addStrings(number1, number2));
}
public static String addStrings(String num1, String num2) {
StringBuilder result = new StringBuilder();
int carry = 0, length = Math.max(num1.length(), num2.length());
for (int index = 0 ; index < length ; index++) {
int digit1 = getDigit(num1, index);
int digit2 = getDigit(num2, index);
int sum = digit1 + digit2 + carry;
result.append(sum % 10);
carry = sum / 10;
}
return result.append(carry > 0 ? carry : "").reverse().toString();
}
private static int getDigit(String number, int index) {
if (index < number.length()) {
return number.charAt(number.length() - index - 1) - '0';
}
return 0;
}
}
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.