leetcode-7-basic

解题思路:

这道题需要注意的是s和t长度相等,但都为空的情况。只需要扫描一遍s建立字典(char, count),然后扫描t,如果有

未出现的字母,或者键值小于0,就可以返回false了。

bool isAnagram(string s, string t) {
        if (s.length() != t.length())
            return false;
        if (s.length() == 0 && t.length() == 0)
            return true;
        map<char,int> dict;
        for (int i = 0; i < s.length(); i++) {
            if (dict.find(s[i]) == dict.end()) {
                dict.insert(dict.end(), make_pair(s[i],1));
            } else
                dict.find(s[i])->second++;
        }
        for (int i = 0; i < t.length(); i++) {
            if (dict.find(t[i]) == dict.end())
                return false;
            else {
                dict.find(t[i])->second--;
                if (dict.find(t[i])->second < 0)
                    return false;
            }
        }

        return true;
    } 

但是,因为题目中说是小写字母,所以可以用数组来做。思路类似,相比使用map插入更简单。

bool isAnagram(string s, string t) {
        if (s.length() != t.length()) return false;
        int n = s.length();
        int counts[26] = {0};
        for (int i = 0; i < n; i++) {
            counts[s[i] - ‘a‘]++;
            counts[t[i] - ‘a‘]--;
        }
        for (int i = 0; i < 26; i++)
            if (counts[i]) return false;
        return true;
    }


解题思路:

本来是用遍历两次数组来算的,外层0~n-1,内层i+1~n-1,找到即跳出。但是如果数组很大,这样无疑很耗时。

由于数组是升序,所以考虑从左右两端开始查找。如果numbers[left]+numbers[right]==target,就找到了;

大于的话right左移,小于的话left右移。当left>=right时,终止。

vector<int> twoSum(vector<int>& numbers, int target) {
        vector<int> result;
        int left = 0;
        int right = numbers.size() - 1;
        while(left < right) {
            if (numbers[left] + numbers[right] == target) {
                result.push_back(left + 1);
                result.push_back(right + 1);
                break;
            }
            else if (numbers[left] + numbers[right] > target) {
                right --;
            }
            else {
                left ++;
            }
        }
        return result;
    }

  

  

时间: 2024-10-05 03:21:53

leetcode-7-basic的相关文章

[LeetCode][JavaScript]Basic Calculator

Basic Calculator Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces . You may assume that the giv

【Leetcode】Basic Calculator II

题目链接:https://leetcode.com/problems/basic-calculator-ii/ 题目: Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division s

【Leetcode】Basic Calculator

题目链接:https://leetcode.com/problems/basic-calculator/ 题目: Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and em

Leetcode 227 basic caculator

Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero. You may assume that the given ex

Leetcode 227. Basic Calculator II JAVA语言

Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero. You may assume that the given ex

[LeetCode#227] Basic Calculator II

Problem: Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero. You may assume that the

Java for LeetCode 227 Basic Calculator II

Implement a basic calculator to evaluate a simple expression string. The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero. You may assume that the given ex

LeetCode OJ Basic Calculator II

Basic Calculator II 题目 思路 和这个一样:Basic Calculator I 代码 class ExpressionTransformation { public: string trans_to_postfix_expression_to_s(string); // 将得到的表达式转化为后缀表达式 long long int calculate_from_postfix_expression(); // 根据后缀表达式计算值 private: vector<string

Leetcode 224: Basic Calculator

Basic Calculator Total Accepted: 2801 Total Submissions: 18103 Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers

Java for LeetCode 224 Basic Calculator

Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces . You may assume that the given expression is