1. String addition

If one of the Pointers is less than zero, the digit is treated as zero

class Solution {
    public String addStrings(String num1, String num2) {
        int jinwei = 0;
        StringBuffer s = new StringBuffer();
        int i = num1.length() - 1, j = num2.length() - 1;
        while (i >= 0 || j >= 0) {
            int x = i >= 0 ? num1.charAt(i) - '0' : 0;
            int y = j >= 0 ? num2.charAt(j) - '0' : 0;
            s.append((x + y + jinwei) % 10);
            jinwei = (x + y + jinwei) / 10;
            i--;
            j--;
        }
        if(jinwei ! =0) {
            s.append(jinwei);
        }
        returns.reverse().toString(); }}Copy the code
  1. Number of words in a string

Trim = trim; split = trim

class Solution { public int countSegments(String s) { String t = s.trim(); if (t.equals("")) { return 0; } return t.split("\\s+").length; }}Copy the code
  1. Arrange a coin

Sum = long; sum = long

class Solution {
    public int arrangeCoins(int n) {
        long sum = 0;
        for (int i = 1; i <= n; i++) {
            sum += i;
            if (sum == n) {
                return i;
            } else if (sum > n) {
                return i - 1; }}return 0; }}Copy the code