zoukankan      html  css  js  c++  java
  • leetcode problem sum

    2. Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

    Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
    Output: 7 -> 0 -> 8

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
            int sum = 0;
            ListNode a(0);
            ListNode* NODE = &a;
            while(l1 || l2 || sum){
                if(l1){
                    sum += l1->val;
                    l1 = l1->next;
                }
                if(l2){
                    sum += l2->val;
                    l2 = l2->next;
                }
                NODE->next = new ListNode(sum % 10);
                NODE = NODE->next;
                sum /= 10;
            }
            return a.next;
        }
    };
    View Code

     3. Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters.

    Examples:

    Given "abcabcbb", the answer is "abc", which the length is 3.

    Given "bbbbb", the answer is "b", with the length of 1.

    Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

    c++:

    暴力O(N2):Your runtime beats 24.22% of cpp submissions.

    class Solution {
    public:
        int lengthOfLongestSubstring(string s) {
        int repeat[300], max = -1, temp = 0;
        int len = s.size();
        if(len == 0) return 0;
            for(int i = 0; i < len; i++){
                temp = 0;
                for(int j = 0; j < 200; j++) repeat[j] = 0;
                for(int k = i; k < len; k++){
                    int l = s[k] - ' ';
                    //cout<< " l " << l << endl;
                    if(repeat[l] >= 1) break;
                    if(repeat[l] == 0) {temp++;repeat[l]++;}
                }
                //cout<< temp <<endl;
                if(temp >= max) max = temp;
            }
            return max;
        }
    };
    View Code

    O(n):Your runtime beats 87.46% of cpp submissions.

    class Solution {
    public:
        int lengthOfLongestSubstring(string s) {
            int nmap[128];
            memset(nmap, -1, sizeof(nmap));
            int maxLength = 0, lastRepeatPosition = -1, len = s.size();
            for (int curPosition = 0; curPosition != len; curPosition++) {
                int position = nmap[s[curPosition]];
                if (position > lastRepeatPosition)
                    lastRepeatPosition = position;
                    nmap[s[curPosition]] = curPosition;
                    maxLength = max(maxLength, curPosition - lastRepeatPosition);
            }
            return maxLength;
        }
    };
    View Code

    129. Sum Root to Leaf Numbers

     
    • Total Accepted: 93320
    • Total Submissions: 269426
    • Difficulty: Medium
    • Contributors: Admin

    Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.

    An example is the root-to-leaf path 1->2->3 which represents the number 123.

    Find the total sum of all root-to-leaf numbers.

    For example,

        1
       / 
      2   3
    

    The root-to-leaf path 1->2 represents the number 12.
    The root-to-leaf path 1->3 represents the number 13.

    Return the sum = 12 + 13 = 25.

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        int sumNumbers(TreeNode* root) {
            int sum_current = 0, total = 0;
            if(!root) return 0;
            return DFS(root, sum_current, total);
        }
        
        int DFS(TreeNode* current, int sum, int total){
            if(current == nullptr)
                return 0;
            if(current != nullptr)
                sum = sum * 10 + current->val;
            if(current->left == nullptr && current->right == nullptr){
                total += sum;
                return total;
            }
            return DFS(current->left, sum, total) + DFS(current->right, sum, total);
        }
    };
    View Code
  • 相关阅读:
    # 抗战电视剧《河山》观后感 #
    SAP MM 自定义条件类型出现在采购信息记录的'条件'界面里 ?
    SAP 对HU做转库操作,系统报错
    SAP MM 采购订单与相关合同的价格差异问题分析
    2019年终总结之SAP项目实践篇
    大学教师,要做教授,请不要做叫兽!
    SAP MM 一个含有多个账号分配对象的行项目的PO及其收货
    华哥演技好!--- 电视剧《魔都风云》观后感
    SAP 如何看某个TR是否传入了Q或者P系统?
    Linux IO 模型
  • 原文地址:https://www.cnblogs.com/yoyo-sincerely/p/6027986.html
Copyright © 2011-2022 走看看