Skip to content

【019-week1】-学习总结 #241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
defuncc opened this issue Apr 20, 2019 · 0 comments
Open

【019-week1】-学习总结 #241

defuncc opened this issue Apr 20, 2019 · 0 comments

Comments

@defuncc
Copy link

defuncc commented Apr 20, 2019

num = [2, 7, 11, 15]

target = 9

set1 = []

for i in num:

if i < target and (target - i) in num:

# print(num.index(i), num.index(target - i))

set1.append(num.index(i)) and set1.append(num.index(target - i))

# set1.append(num.index(i))

# set1.append(num.index(target - i))

# print(num.index(i) and num.index((target - i)),end='') # 非零即真问题0,1不能用and与0,1

print(set1)

"""
所以对于if的判断应该翻译为真假、或者纯在不存在
对于直接返回下标索引的方法会存在只会找第一次出现的元素,即不稳定问题
同效转化问题,使不稳定变为稳定。基于分析判断,下标索引即是数字。它是以什么形式表现的,把握关系的实质。
把特殊问题推演到一般,条件范围发生变化,之前的结构是否适用、稳定。
把问题尽可能统一解决,而不是分开。这个统一就是问题不同表现的实质联系。
之前编程常遇到的问题,1是索引越界 2是数据类型不对
逐步试验,确定可确定的
"""

============================================================================

set1 = []

for i in range(len(num)):

if i <= len(num) - 2:

if num[i] + num[i + 1] == target:

set1.append(i), set1.append(i + 1)

print(set(set1))

加法无穷尽,不如以确定的减确定的求未确定。

先找到以结果减输入的值是否在范围类,若在,则找出来打印出下标。

"""
更据自己所学,运用自己已有的知识素能解决问题,是实践的认知,相对其他经过实践检验锤炼的方法是土的,但是是开元的

因为掌握了基本实现原理,可以更新可以改造

"""

"""
1.判断条件,即通过相减确定值是否在值域里。
2.找出值所在的下标
则是两次循环
在一次循环时,就带入判断条件
输入
条件逻辑判断、运算
输出
"""

class Solution(object):

def twoSum(self, nums, target):

list1 = []

list2 = []

list3 = []

i = 0

# for i in range(len(nums)):

while i < len(nums):

if target - nums[i] in nums:

list2.append(target - nums[i])

list1.append({i: nums[i]})

if nums[i] in list2:

list3.append({i, nums[i]})

i = i + 1

print(list1)

twosum = Solution()

nums = [2, 7, 11, 15]

target = 9

twosum.twoSum(nums, target)

==========================================

class Solution(object):
def twoSum(self, nums, target):
list1 = []
i = 0
while i < len(nums):
# if nums[i] < 0 and target < 0:
# target1 = target
# # global target
if target - nums[i] in nums:
list1.append(i)
if nums[i] * 2 == target:
if nums.count(nums[i]) == 1:
list1.pop()
i += 1
print(list1)
twosum = Solution()

nums = [2, 7, 11, 15]

target = 9

nums = [3, 2, 4]

target = 6

nums = [-1, -2, -3, -4, -5]
target = -8
twosum.twoSum(nums, target)

仔细查看错误提示信息所指为何,再结合实现逻辑梳理问题所在。

@defuncc defuncc changed the title 019-学习总结 【019-week1】-学习总结 Apr 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant