Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Sunday, February 9, 2020

Leetcode 415 @ Add Strings


Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.
Note:
  1. The length of both num1 and num2 is < 5100.
  2. Both num1 and num2 contains only digits 0-9.
  3. Both num1 and num2 does not contain any leading zero.
  4. You must not use any built-in BigInteger library or convert the inputs to integer directly.

非常简单的题目,先把两个string reverse,这样才可以从头加到尾是从低位到高位,用一个sum, next表示当前的和,以及进位。while loop 判断有没有超过最大的string长度,以及进位是不是还有值没有处理。加完以后,再reverse string就是答案了。另外,处理string to int时,别忘了 +- ‘0’。 


class Solution {
public:
    string addStrings(string num1, string num2) {
        reverse(num1.begin(), num1.end());
        reverse(num2.begin(), num2.end());
        int s1 = num1.size(), s2 = num2.size();
        int i = 0, next = 0, sum = 0;
        string res;
        while(i < max(s1, s2) || next != 0)
        {
            sum += next;
            sum += i < s1 ? num1[i] - '0' : 0;
            sum += i < s2 ? num2[i] - '0': 0;
            res += sum % 10 + '0';
            next = sum/10;
            sum = 0;
            i++;
        }
        reverse(res.begin(), res.end());
        return res;
    }
};

Thursday, October 3, 2013

Count and Say@leetcode

刷题必备书籍:Cracking the Coding Interview: 150 Programming Questions and Solutions 

简历:The Google Resume: How to Prepare for a Career and Land a Job at Apple, Microsoft, Google, or any Top Tech Company
算法学习书籍:Introduction to Algorithms
编程珠玑:Programming Pearls (2nd Edition)
C++ 学习:The C++ Programming Language, 4th Edition
经典操作系统书籍,龙书:Operating System Concepts
创业:The Start-up of You: Adapt to the Future, Invest in Yourself, and Transform Your Career

Count and Say


AC Rate: 439/1647
My Submissions
The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...
1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.
Given an integer n, generate the nth sequence.
Note: The sequence of integers will be represented as a string.
就是string的操作,那个tmp string做中间记录,一个n用于数,还有count,思路不难,要注意的就是string的操作,这里
str.append(str, char)是不行的,char不能append到string上,因为string 是const,而char是variable, append只能
用于string之间,而char添加直接用 str = str + char 加号就行了。

换上自己风格的代码看上去就是舒服些。。。

Friday, September 6, 2013

Palindrome String@leetcode

刷题必备书籍:Cracking the Coding Interview: 150 Programming Questions and Solutions 

简历:The Google Resume: How to Prepare for a Career and Land a Job at Apple, Microsoft, Google, or any Top Tech Company
算法学习书籍:Introduction to Algorithms
编程珠玑:Programming Pearls (2nd Edition)
C++ 学习:The C++ Programming Language, 4th Edition
经典操作系统书籍,龙书:Operating System Concepts
创业:The Start-up of You: Adapt to the Future, Invest in Yourself, and Transform Your Career
Given a string s, partition s such that every substring of the partition is a palindrome.
Return all possible palindrome partitioning of s.
For example, given s = "aab",
Return
  [
    ["aa","b"],
    ["a","a","b"]
  ]
» Solve this problem

好久没刷题,刚开始没思路,后来想了想,就和subsets一样。但凡这种需要把结果纪录下来,而且又要遍历的,大体的code structure就是此题这样。top-down地搜索,把中间结果纪录下来。


Leetcode 316. Remove Duplicate Letters

 这道题表面问的是如何删除重复,实际在问如何从多个字符选取一个保留,从而让整个字符串按升序排列。那么策略就是对于高顺位的字符比如‘a',就要选靠前位置的保留,而低顺位字符如’z'则应该尽量选取靠后位置保留。 算法大概思路:每看到一个字符,我们要决定是否保留 1. ...