我觉得每天来两道,练习练习,再看看人家是怎么优化的。
1.给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
c++:暴力求解,204ms,9.1M
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> output;
for(int i=0; i<nums.size()-1; i++)
{
for(int j=i+1; j<nums.size(); j++)
{
if(nums[i]+nums[j] == target)
{
output.push_back(i);
output.push_back(j);
return output;
}
}
}
return output;
}
};
python:暴力求解,1760ms,13.7M
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
for i in range(len(nums)):
if(target-nums[i] in nums and i!=nums.index(target-nums[i])):
return [i,nums.index(target-nums[i])]
看了下其他人怎么做的,他们好聪明啊,用hashmap。
c++:20ms 9.9MB
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int,int> hash;
for(int i=0; i<nums.size(); i++){
if(hash.count(target-nums[i])){
return {hash[target-nums[i]],i};
}
hash[nums[i]] = i;
}
return {-1,-1};
}
};
python:56ms 14.2MB
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
map = {}
for index, num in enumerate(nums):
another_num = target - num
if(another_num in map):
return [map[another_num], index]
map[num] = index
return None
2.给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。
c++: 32ms 10.5MB
/**
* 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 flag = 0;
ListNode* out = new ListNode(0);
ListNode* sub = out;
while(l1 != NULL || l2 != NULL || flag != 0)
{
int n1 = 0;
int n2 = 0;
int sum = 0;
if(l1 != NULL)
{
n1 = l1->val;
l1 = l1->next;
}
if(l2 != NULL)
{
n2 = l2->val;
l2 = l2->next;
}
sum = n1 + n2 + flag;
flag = sum / 10;
ListNode* sub1 = new ListNode(sum % 10);
sub->next = sub1;
sub = sub1;
}
return out->next;
}
};
python: 76ms 12.9MB
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
out = ListNode(0)
sub = out
flag = 0;
while(l1 != None or l2 != None):
x = l1.val if l1 else 0
y = l2.val if l2 else 0
sum = x + y + flag
flag = sum // 10
sub.next = ListNode(sum%10)
sub = sub.next
if(l1 != None): l1 = l1.next
if(l2 != None): l2 = l2.next
if flag>0:
sub.next = ListNode(1)
return out.next
3.给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
c++: 4ms 9.1MB
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int max = 0;
int len = 0;
int i = 0;
int j = 0;
int N = s.length();
for(; i<N; i++)
{
for(j=len; j<i; j++)
{
if(s[i] == s[j])
{
len = j + 1;
break;
}
}
if(i-len+1 > max)
{
max = i - len + 1;
}
}
cout << max << endl;
return max;
}
};
python: 112ms 12.8MB
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
i,maxlength = 0,0
str = {}
for j in range(len(s)):
if( s[j] in str):
i = max(str[s[j]], i)
maxlength = max(maxlength, j-i+1)
str[s[j]] = j+1;
return maxlength
python其他人版:76ms 12.9MB
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
max_length = 0
i = 0
test = ''
for j in s:
if j not in test:
test += j
i += 1
else:
if i >= max_length:
max_length = i
index = test.index(j)
test = test[(index+1):] + j
i = len(test)
if i > max_length:
max_length = i
return max_length