Skip to content
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

commit #1057

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open

commit #1057

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified problems/0001_Two_Sum/ass.cpp
100755 → 100644
Empty file.
Empty file modified problems/0001_Two_Sum/jacklynd.c
100755 → 100644
Empty file.
Empty file modified problems/0002_Add_Two_Numbers/add_two.py
100755 → 100644
Empty file.
Empty file modified problems/0002_Add_Two_Numbers/ass.cpp
100755 → 100644
Empty file.
28 changes: 28 additions & 0 deletions problems/0002_Add_Two_Numbers/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 这道题目主要是,使用链表来表示大数
# 通过遍历两个链表,将对应的位上的数字相加
# 相加的时候,要注意进位的问题
# 同时,应该注意两个链表长度不同的问题
# 最后,还应该检查最后两个数字相加后,是否有进位的问题


class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None


class Solution(object):
def addTwoNumbers(self, l1, l2):
root = ListNode(0)
node = root
res = 0
while l1 or l2:
x = l1.val if l1 else 0
y = l2.val if l2 else 0
s = res + x + y
node.next = ListNode(s % 10)
res = s // 10
if l1 is not None: l1 = l1.next
if l2 is not None: l2 = l2.next
if res > 0: node.next = ListNode(1)
return root.next
Empty file.
Empty file modified problems/0004_Median_of_Two_Sorted_Arrays/jacklynd.cpp
100755 → 100644
Empty file.
18 changes: 18 additions & 0 deletions problems/0006_ZigZag_Conversion/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution:
def convert(self, s, numRows):
if numRows == 1: return s
result = [[] for i in range(numRowns)]
row = 0
direct = 1
for c in s:
result[row].append(c)
if row >= numRows - 1:
direct = -1
elif row == 0:
direct = 1
row += direct
answer = ''
for row in result:
for col in row:
answer += col
return answer
Empty file modified problems/0007_Reverse_Integer/jacklynd.c
100755 → 100644
Empty file.
Empty file modified problems/0008_String_to_Integer_atoi/jacklynd.c
100755 → 100644
Empty file.
Empty file modified problems/0013_Roman_to_Integer/jacklynd.c
100755 → 100644
Empty file.
Empty file modified problems/0014_Longest_Common_Prefix/jacklynd.c
100755 → 100644
Empty file.
Empty file modified problems/0015_3Sum/jacklynd.c
100755 → 100644
Empty file.
Empty file modified problems/0016_3Sum_Closest/jacklynd.c
100755 → 100644
Empty file.
Empty file modified problems/0019_Remove_Nth_Node_From_End_of_List/ass.c
100755 → 100644
Empty file.
Empty file modified problems/0021_Merge_Two_Sorted_Lists/ass.c
100755 → 100644
Empty file.
46 changes: 46 additions & 0 deletions problems/0053_Maximum_Subarray/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 本题有两种解法,分别是动态规划和分而治之
# 动态规划,主要是定义一个dp数组,用来保存到index为止,数组的子序列的和
# dp[0]等于数组的第一个元素的值,从数组第一个元素开始,其下标为i,如果dp[i-1] > 0,那么dp[i]=dp[i-1] + nums[i], 如果dp[i-1] < 0,那么dp[i]=nums[i]
# 在计算dp数组的时候,记录每次子序列和的最大值,该最大值就是所求子序列和的最大值
class solution:
def maxSubArray(self, nums):
if not nums:
return 0
dp = len(nums) * [0]
sub_max = dp[0] = nums[0]

for i in range(1, len(nums)):
if dp[i-1] > 0:
dp[i] = dp[i-1] + nums[i]
else:
dp[i] = nums[i]
sub_max = max(dp[i], sub_max)
return sub_max

# 分而治之的方式,主要是利用二分的方式,把数组一分为二,首先求出左边子数组的最大和,然后再求出右边子数组的最大和,然后从中间元素开始,
# 进行遍历求出当前子序列的最大和
class Solution:
def maxSubArray(self, nums):
def find_max(nums, left, right):
if left >= right:
return nums[left]

center = (left + right) // 2
lmax = find_max(nums, left, center - 1)
rmax = find_max(nums, center + 1, right)
t = mmax = nums[center]

for i in range(center - 1, left - 1, -1):
t += nums[i]
mmax = max(t, mmax)
t = mmax

for i in range(center + 1, right, 1):
t += nums[i]
mmax = max(t, mmax)
return max(mmax, lmax, rmax)

if not nums:
return 0
else:
return find_max(nums, 0, len(nums) - 1)
11 changes: 11 additions & 0 deletions problems/0058_Length_of_Last_Word/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# 此题难度角度,主要就是依据空格对字符串进行分词
# 在此过程中,需要注意的是需要把字符串默认的空格去掉
class Solution:
def lengthOfLastWord(self, s):
cnt, tail = 0, len(s) - 1
while tail >= 0 and s[tail] == ' ':
tail -= 1
while tail >= 0 and s[tail] != ' ':
cnt += 1
tail -= 1
return cnt
28 changes: 28 additions & 0 deletions problems/0091_Decode_Ways/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 利用动态规划的思想来解决
# 当前的字符串和前一个字符串组合之后的数值如果在解码的范围内
# 就可以把当前的字符串和上一次的独立字符串进行合并
# 合并的结果加上上一个字符串解码的结果就是当前字符串解码的结果
# 如果是0,就需要特殊处理,0如果和前一个字符串组合后在解码范围内
# 新产生的解码串就全部是合并的,没有独立的字符串
# 如果不能和前一个字符串合并的话,那就是无法解码的字符串,直接返回0
class Solution:
def numDecodings(self, s: str) -> int:
if not s or s[0] == '0':
return 0
t1 = t2 = 1
for i in range(1, len(s)):
su = int(s[i-1] + s[i])
if su >=1 and su <= 26:
if s[i] != '0':
t3 = t2
t2 += t1
t1 = t3
else:
t2 = t1
t1 = 0
else:
if s[i] != '0':
t1 = t2
else:
return 0
return t2
Empty file modified problems/0100_Same_Tree/ass.c
100755 → 100644
Empty file.
Empty file modified problems/0203_Remove_Linked_List_Elements/jacklynd.c
100755 → 100644
Empty file.
Empty file modified problems/0257_binary_tree_paths/fengdongdong.cpp
100755 → 100644
Empty file.
8 changes: 8 additions & 0 deletions problems/0303_Range_Sum_Query/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# 实现比较简单,主要利用python的切片操作
class NumArray:

def __init__(self, nums: List[int]):
self.nums = nums

def sumRange(self, i: int, j: int) -> int:
return sum(self.nums[i:j+1])
14 changes: 0 additions & 14 deletions problems/0378_Kth_Smallest_Element_in_a_Sorted_Matrix /0378.py

This file was deleted.

Empty file modified problems/0515_Find_Largest_Value_in_Each_Tree_Row/ass.c
100755 → 100644
Empty file.
Empty file modified problems/0566_Reshape_the_Matrix/016_klgentle.py
100755 → 100644
Empty file.
Empty file modified problems/0566_Reshape_the_Matrix/jacklynd.c
100755 → 100644
Empty file.
Empty file modified problems/0637_Average_of_Levels_in_Binary_tree/fengdongdong.cpp
100755 → 100644
Empty file.
Empty file modified problems/0876_Middle_of_the_Linked_List/fengdongsheng.cpp
100755 → 100644
Empty file.
Empty file modified problems/0997_Find_the_Town_Judge/ass.c
100755 → 100644
Empty file.
4 changes: 4 additions & 0 deletions submittors/2_lionfly.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
fd0ecff1fb6e30105632bfdc007d83efdc86acf1
173cc1fd589331069b57c75f641312a68d024841
1656c2a3348ceda2fb88bede686c77677b2af09a
18ec884c3891a2818ff26a23214e103c114dbeac
d3891e08919d7454c21c43651b7dc1e2403c4cab
43dfc0ca99e4cb0a7ac437cc740e4a6e5d1bca19