Xiao❤Hao

求知若渴 虚心若愚


  • 首页

  • 标签

  • 分类

  • 归档

LeetCode-011-container-with-most-water

发表于 2018-12-19 | 分类于 LeetCode | 阅读次数:

题目地址

  • 英文:https://leetcode.com/problems/container-with-most-water/
  • 中文:https://leetcode-cn.com/problems/container-with-most-water/

题意:

给定 n 个非负整数 a1,a2,…,an,每个数代表坐标中的一个点 (i, ai) 。在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0)。找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。

说明:你不能倾斜容器,且 n 的值至少为 2。

示例:

  • 输入: [1,8,6,2,5,4,8,3,7]
  • 输出: 49

解题思路一:

相当于求:Area = Max(min(height[i], height[j]) * (j-i)) {其中0 <= i < j < height,size()}

直接暴力竟然过了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <climits>
using namespace std;

class Solution
{
public:
int maxArea(vector<int> &height)
{
int mx = 0, tmp;
for (int i = 0; i < height.size(); i++)
{
for (int j = i + 1; j < height.size(); j++)
{
tmp = (j - i) * min(height[i], height[j]);
mx = max(mx, tmp);
}
}
return mx;
}
};

// int main(int argc, char const *argv[])
// {
// vector<int> p;
// int t[] = {1, 8, 6, 2, 5, 4, 8, 3, 7};
// int iLen = sizeof(t) / sizeof(t[0]);
// for (int i = 0; i < iLen; i++)
// p.push_back(t[i]);
// Solution obj = Solution();
// obj.maxArea(p);
// return 0;
// }

解题思路二:

维护两个指针,一个从开始进行-i,一个从末尾进行-j。

此时,如果height[i] < height[j],那么一定有 (i+1,j)组成的面积大于等于(i,j-1)

请注意,进行第二步的时候,只是选择最优的移动一格求解最大值,并不代表,(i+1,j)大于(i,j),所有进行每一步的时候需求与当前最大值进行比较。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <climits>
using namespace std;

class Solution
{
public:
int maxArea(vector<int> &height)
{
int mx = 0, tmp;
int i = 0, j = height.size() - 1;
while (i < j)
{
tmp = (j - i) * min(height[i], height[j]);
mx = max(mx, tmp);
if (height[i] > height[j])
j--;
else
i++;
}
return mx;
}
};

// int main(int argc, char const *argv[])
// {
// vector<int> p;
// int t[] = {1, 8, 6, 2, 5, 4, 8, 3, 7};
// int iLen = sizeof(t) / sizeof(t[0]);
// for (int i = 0; i < iLen; i++)
// p.push_back(t[i]);
// Solution obj = Solution();
// printf("%d\n", obj.maxArea(p));
// return 0;
// }

github

  • https://github.com/lamborghini1993/LeetCode

LeetCode-007-reverse-integer

发表于 2018-12-17 | 分类于 LeetCode | 阅读次数:

题目地址

  • 英文:https://leetcode.com/problems/reverse-integer/
  • 中文:https://leetcode-cn.com/problems/reverse-integer/

题意:

给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。

注意:

假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−231, 231 − 1]。

请根据这个假设,如果反转后整数溢出那么就返回 0。

解题思路python:

  • 转化为字符串,然后直接反转字符串
  • 注意判断负号以及溢出即可
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    class Solution:
    m_Min = -2**31
    m_Max = 2**31 - 1

    def reverse(self, x):
    """
    :type x: int
    :rtype: int
    """
    if x < 0:
    str_x = str(abs(x))
    x = "-"
    else:
    str_x = str(x)
    x = ""
    x += str_x[::-1]
    result = int(x)
    print(self.m_Max, self.m_Min)
    if result < self.m_Min or result > self.m_Max:
    return 0
    return result


    print(-2 % 10) # python结果为8
    Solution().reverse(123)
    Solution().reverse(-123)

解题思路C++:

  • 一步一步进行:取模然后 * 10 + lastresult
  • 因为可能溢出,所以与最大值、最小值除以10来比较
  • 最大值:2147483647
  • 最小值:-2147483648
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>

    class Solution
    {
    public:
    int min = INT_MIN / 10, max = INT_MAX / 10;
    int reverse(int x)
    {
    int r = 0, tmp;
    while (x != 0)
    {
    tmp = x % 10;
    x = x / 10;
    // 2147483647 -2147483648
    if (r > max || (r == max && tmp > 7))
    return 0;
    if (r < min || (r == min && tmp < -8))
    return 0;
    r = r * 10 + tmp;
    }
    return r;
    }
    };

    // int main(int argc, const char* argv[])
    // {
    // Solution obj = Solution();
    // printf("%d\n", -2 % 10); // C++ 结果为-2
    // printf("%d\n", obj.reverse(123));
    // printf("%d\n", obj.reverse(-123));
    // return 0;
    // }

github

  • https://github.com/lamborghini1993/LeetCode

LeetCode-008-string-to-integer-atoi

发表于 2018-12-17 | 分类于 LeetCode | 阅读次数:

题目地址

  • 英文:https://leetcode.com/problems/string-to-integer-atoi/
  • 中文:https://leetcode-cn.com/problems/string-to-integer-atoi/

题意:

请你来实现一个 atoi 函数,使其能将字符串转换成整数。

首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。

当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。

该字符串除了有效的整数部分之后也可能会存在多余的字符,这些字符可以被忽略,它们对于函数不应该造成影响。

注意:假如该字符串中的第一个非空格字符不是一个有效整数字符、字符串为空或字符串仅包含空白字符时,则你的函数不需要进行转换。

在任何情况下,若函数不能进行有效的转换时,请返回 0。

说明:

假设我们的环境只能存储 32 位大小的有符号整数,那么其数值范围为 [−231, 231 − 1]。如果数值超过这个范围,qing返回 INT_MAX (231 − 1) 或 INT_MIN (−231) 。

解题思路

  1. 给开始标记判断是否为空格,然后记录符号为正,还是负
  2. 然后在正确的获取数字的情况下,判断如果符号为负就取当前数字的负数。
  3. 判断添加之后是否会溢出
  4. r = r * 10 + tmp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <climits>
using namespace std;

class Solution
{
public:
// 2147483647 -2147483648
int min = INT_MIN / 10, max = INT_MAX / 10;
int myAtoi(string str)
{
int r = 0;
int stautu = 0;
bool space = true;
for (int i = 0; i < str.length(); i++)
{
if (space && str[i] == ' ' && stautu == 0)
continue;
else if (space && stautu == 0 && str[i] == '-')
stautu = -1;
else if (space && stautu == 0 && str[i] == '+')
stautu = 1;
else if (str[i] >= '0' && str[i] <= '9')
{
space = false;
int tmp = str[i] - '0';
if (stautu == -1)
tmp = -tmp;
if (r > max || (r == max && tmp > 7))
return INT_MAX;
if (r < min || (r == min && tmp < -8))
return INT_MIN;
r = r * 10 + tmp;
}
else
break;
}
return r;
}
};

// int main(int argc, const char *argv[])
// {
// Solution obj = Solution();
// printf("%d\n", obj.myAtoi(" -42"));
// printf("%d\n", obj.myAtoi("4193 with words"));
// printf("%d\n", obj.myAtoi("words and 987"));
// printf("%d\n", obj.myAtoi("-91283472332"));
// printf("%d\n", obj.myAtoi("- 12 3"));
// printf("%d\n", obj.myAtoi(" -123 sdf34"));
// printf("%d\n", obj.myAtoi("-2147483649"));
// printf("%d\n", obj.myAtoi("0-1"));
// return 0;
// }

github

  • https://github.com/lamborghini1993/LeetCode

LeetCode-006-zigzag-conversion

发表于 2018-12-13 | 分类于 LeetCode | 阅读次数:

题目地址

  • 英文:https://leetcode.com/problems/zigzag-conversion/
  • 中文:https://leetcode-cn.com/problems/zigzag-conversion/

题意:

将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。

比如输入字符串为 “LEETCODEISHIRING” 行数为 3 时,排列如下:

L C I R
E T O E S I I G
E D H N

之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:”LCIRETOESIIGEDHN”。

解题思路:

  • 完全的模拟题目,找规律题目
  • 可以转化为先向下numRows-1次,然后向右上numRows-1次
  • 可以用一个列表存储没行数据,然后模拟到第几行就在对应行上加上字符
  • 最后将列表相加即为所得
  • PS:注意特判numRows为1的情况
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution:
def convert(self, s, numRows):
"""
:type s: str
:type numRows: int
:rtype: str
"""
if numRows == 1: # 特判一下为1的情况
return s
result = []
for i in range(numRows):
result.append("")
bUp2Down = False
for i, c in enumerate(s):
mod = i % (numRows-1)
if not mod:
bUp2Down = not bUp2Down

if bUp2Down:
result[mod] += c
else:
t = numRows-1-mod
result[t] += c
r = ""
for i in range(numRows):
r += result[i]
print(result[i])
return r


Solution().convert("LEETCODEISHIRING", 3)

github

  • https://github.com/lamborghini1993/LeetCode

LeetCode-005-longest-palindromic-substring

发表于 2018-12-12 | 分类于 LeetCode | 阅读次数:

题目地址

  • 英文:https://leetcode.com/problems/longest-palindromic-substring/
  • 中文:https://leetcode-cn.com/problems/longest-palindromic-substring/

题意:

给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。

解题思路一:

回文子串的规律是左右对称,所有我们可以遍历每个字符,向两边做扩展来判断最大的回文串长度

时间复杂度为:o(N*N)

解题思路二:

Manacher 算法

在一中其实很多回文串我们已经做过遍历了,所以我们想办法来利用之前得到的遍历数据

可见连接:https://segmentfault.com/a/1190000003914228#articleHeader3
写的挺详细的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution:
def longestPalindrome(self, s):
"""
:type s: str
:rtype: str
"""
s = "#" + "#".join(s) + "#"
RL = [0] * len(s)
maxRight = 0
pos = 0
maxLen = 0
maxIndex = 0
for i in range(len(s)):
if i < maxRight:
RL[i] = min(RL[2*pos-i], maxRight-i)
else:
RL[i] = 1
while i-RL[i] >= 0 and i+RL[i] < len(s):
l = s[i-RL[i]]
r = s[i+RL[i]]
if l != r:
break
RL[i] += 1
if RL[i] + i - 1 > maxRight:
maxRight = RL[i]
pos = i
if RL[i] > maxLen:
maxLen = RL[i]
maxIndex = i
p = s[maxIndex-maxLen+1:maxIndex+maxLen-1]
return p.replace("#", "")


Solution().longestPalindrome("babad")

github

  • https://github.com/lamborghini1993/LeetCode

LeetCode-004-median-of-two-sorted-arrays

发表于 2018-12-09 | 分类于 LeetCode | 阅读次数:

题目地址

  • 英文:https://leetcode.com/problems/median-of-two-sorted-arrays/
  • 中文:https://leetcode-cn.com/problems/median-of-two-sorted-arrays/

题意:

给定两个大小为 m 和 n 的有序数组 nums1 和 nums2。
请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。
你可以假设 nums1 和 nums2 不会同时为空。

解题思路:

  1. 直接拼接两个有序列表,然后求中值,时间复杂应该是O(N+M),但是竟然没有超时
  • 时间复杂度为O(N+M)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    class Solution:
    def findMedianSortedArrays(self, nums1, nums2):
    """
    :type nums1: List[int]
    :type nums2: List[int]
    :rtype: float
    """
    lstResult = []
    i = j = 0
    while i < len(nums1) or j < len(nums2):
    if j >= len(nums2):
    lstResult.append(nums1[i])
    i += 1
    continue
    if i >= len(nums1):
    lstResult.append(nums2[j])
    j += 1
    continue
    if nums1[i] < nums2[j]:
    lstResult.append(nums1[i])
    i += 1
    else:
    lstResult.append(nums2[j])
    j += 1
    iLen = len(lstResult)
    if iLen % 2:
    return lstResult[iLen // 2]
    return (lstResult[iLen // 2 - 1] + lstResult[iLen // 2]) / 2


    obj = Solution()
    print(obj.findMedianSortedArrays([1, 3], [2]))
    print(obj.findMedianSortedArrays([1, 2], [3, 4]))

github

  • https://github.com/lamborghini1993/LeetCode

LeetCode-003-longest-substring-without-repeating-characters

发表于 2018-12-09 | 分类于 LeetCode | 阅读次数:

题目地址

  • 英文:https://leetcode.com/problems/longest-substring-without-repeating-characters/
  • 中文:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/

题意:

给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。

解题思路:

要求不重复字符的最长字符,只需要遍历维护当前不重复最长字串初始下标,
然后每次取最大值即可

  • 时间复杂度为O(N)
  • 空间复杂度为O(N)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    class Solution:
    def lengthOfLongestSubstring(self, s):
    """
    :type s: str
    :rtype: int
    """
    dInfo = {}
    iMax = 0
    iBeginIndex = -1 # 因为下标是从0开始的,所以赋值为-1
    for i, c in enumerate(s):
    if c in dInfo:
    iBeginIndex = max(iBeginIndex, dInfo[c]) #每次维护不重复串的初始下标
    iMax = max(i - iBeginIndex, iMax)
    dInfo[c] = i
    return iMax


    def Test():
    """测试样例"""
    lstTest = ["", "abcabvd", "ab", "34f3", "pwwkew", "abba", "abbcccbba", "aabaab!bb"]
    lstResult = [0, 5, 2, 3, 3, 2, 2, 3]
    obj = Solution()
    for i, s in enumerate(lstTest):
    output = obj.lengthOfLongestSubstring(s)
    if lstResult[i] != output:
    print("结果不匹配 Input:%s Output:%s Expected:%s" % (s, output, lstResult[i]))
    break
    print("done")


    if __name__ == "__main__":
    Test()
    print(Solution().lengthOfLongestSubstring("aabaab!bb"))

github

  • https://github.com/lamborghini1993/LeetCode

LeetCode-002-add-two-numbers

发表于 2018-12-09 | 分类于 LeetCode | 阅读次数:

题目地址

  • 英文:https://leetcode.com/problems/add-two-numbers/
  • 中文:https://leetcode-cn.com/problems/add-two-numbers/

题意:

给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。

如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。

您可以假设除了数字 0 之外,这两个数都不会以 0 开头。

解题思路:

相当于大数求和,遍历两个链表,类比做两个数相加,将余数加到列表,值放到下一位相加。

  • 时间复杂度为O(N+M)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    # Definition for singly-linked list.
    # class ListNode:
    # def __init__(self, x):
    # self.val = x
    # self.next = None

    class Solution:
    def addTwoNumbers(self, l1, l2):
    """
    :type l1: ListNode
    :type l2: ListNode
    :rtype: ListNode
    """
    lst = []
    tmp = 0
    while l1 or l2:
    a = b = 0
    if l1:
    a = l1.val
    l1 = l1.next
    if l2:
    b = l2.val
    l2 = l2.next
    c = a + b + tmp
    lst.append(c % 10)
    tmp = c // 10
    if tmp:
    lst.append(tmp)
    return lst

github

  • https://github.com/lamborghini1993/LeetCode

LeetCode-001-two-sum

发表于 2018-12-08 | 分类于 LeetCode | 阅读次数:

题目地址

  • 英文:https://leetcode.com/problems/two-sum/
  • 中文:https://leetcode-cn.com/problems/two-sum/

题意:

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。

你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。

解题思路一:

双重循环遍历,判断nums[i] + nums[j] = target,很显然这种方式太暴力

  • 时间复杂度为O(N*N)
  • 空间复杂度:O(1)

解题思路二:

竟然给定了target,我们就可以根据一个值求出另一个值
然后在判断这个值是否在nums数组中,即可。

  • 时间复杂度为O(N*M)
  • 空间复杂度:O(1)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    class Solution:
    def twoSum(self, nums, target):
    """
    :type nums: List[int]
    :type target: int
    :rtype: List[int]
    """
    for i, v in enumerate(nums):
    value = target - v
    if value not in nums:
    continue
    j = nums.index(value)
    if i != j:
    return [i, j]
    return None

解题思路三:

在二的基础上,判断是否在数组中,可以遍历一次用字典存储起来,然后获取另一个值

  • 时间复杂度为O(N)
  • 空间复杂度:O(N)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    class Solution:
    def twoSum(self, nums, target):
    """
    :type nums: List[int]
    :type target: int
    :rtype: List[int]
    """
    dInfo = {}
    for x, xValue in enumerate(nums):
    yValue = target - xValue
    if yValue in dInfo:
    return [dInfo[yValue], x]
    dInfo[xValue] = x
    return None

github

  • https://github.com/lamborghini1993/LeetCode

Git submodule使用和一键配置

发表于 2018-12-05 | 分类于 git , submodlue | 阅读次数:

Git submodule介绍

经常碰到这种情况:当你在一个Git 项目上工作时,你需要在其中使用另外一个Git 项目。也许它是一个第三方开发的Git 库或者是你独立开发和并在多个父项目中使用的。这个情况下一个常见的问题产生了:你想将两个项目单独处理但是又需要在其中一个中使用另外一个。

在Git 中你可以用子模块submodule来管理这些项目,submodule允许你将一个Git 仓库当作另外一个Git 仓库的子目录。这允许你克隆另外一个仓库到你的项目中并且保持你的提交相对独立。

添加子模块

本文将统一使用`git@github.com:lamborghini1993/Test.git`来当做子模块使用

1
2
3
4
5
6
7
8
$ git submodule add git@github.com:lamborghini1993/Test.git Test
正克隆到 '/home/duoyi/mygit/other/Test2'...
remote: Enumerating objects: 8, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 8 (delta 1), reused 7 (delta 0), pack-reused 0
接收对象中: 100% (8/8), 完成.
处理 delta 中: 100% (1/1), 完成.

添加子模块后运行git status, 可以看到目录有增加1个文件.gitmodules, 这个文件用来保存子模块的信息。

1
2
3
4
5
6
7
8
9
$ git status 
位于分支 master
您的分支与上游分支 'origin/master' 一致。

要提交的变更:
(使用 "git reset HEAD <文件>..." 以取消暂存)

新文件: .gitmodules
新文件: Test

查看子模块

1
2
$ git submodule 
f8b5eedbdf35f99b0ac6ff1ff6f5c6d506dd4328 Test (heads/master)

克隆包含子模块的项目

克隆包含子模块的项目有二种方法:一种是先克隆父项目,再更新子模块;另一种是直接递归克隆整个项目。

  • 先克隆父项目,再更新子模块:

    1. git clone https://github.com/lamborghini1993/Test2.git other
    2. 查看子模块

      1
      2
      $ git submodule
      -f8b5eedbdf35f99b0ac6ff1ff6f5c6d506dd4328 Test

      子模块前面有一个-,说明子模块文件还未检入(空文件夹)。

    3. 初始化子模块git submodule init(初始化模块只需在克隆父项目后运行一次)
    4. 更新子模块

      1
      2
      3
      $ git submodule update
      正克隆到 '/home/duoyi/mygit/other/Test'...
      子模组路径 'Test':检出 'f8b5eedbdf35f99b0ac6ff1ff6f5c6d506dd4328'

      子模块前面有一个-,说明子模块文件还未检入(空文件夹)。

  • 递归克隆整个项目

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    $ git clone https://github.com/lamborghini1993/Test2.git other2 --recursive 
    正克隆到 'other2'...
    remote: Enumerating objects: 6, done.
    remote: Counting objects: 100% (6/6), done.
    remote: Compressing objects: 100% (4/4), done.
    remote: Total 6 (delta 0), reused 3 (delta 0), pack-reused 0
    展开对象中: 100% (6/6), 完成.
    子模组 'Test'(git@github.com:lamborghini1993/Test.git)未对路径 'Test' 注册
    正克隆到 '/home/duoyi/mygit/other2/Test'...
    remote: Enumerating objects: 8, done.
    remote: Counting objects: 100% (8/8), done.
    remote: Compressing objects: 100% (7/7), done.
    接收对象中: 100% (8/8), 完成.
    处理 delta 中: 100% (1/1), 完成.
    remote: Total 8 (delta 1), reused 7 (delta 0), pack-reused 0
    子模组路径 'Test':检出 'f8b5eedbdf35f99b0ac6ff1ff6f5c6d506dd4328'
  • 推荐使用第一种方法,因为第二种方法存在父目录克隆了,但是子目录克隆失败的情况。

修改子模块

  • 修改子模块记得一定要先提交子模块,然后在提交父模块

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    ➜  Test git:(f8b5eed) touch a.txt

    ➜ Test git:(f8b5eed) ✗ git status
    头指针分离于 f8b5eed
    未跟踪的文件:
    (使用 "git add <文件>..." 以包含要提交的内容)

    a.txt

    提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
    ➜ Test git:(f8b5eed) ✗ git add *

    ➜ Test git:(f8b5eed) ✗ git commit -m "test"
    [分离头指针 5cc4941] test
    1 file changed, 0 insertions(+), 0 deletions(-)
    create mode 100644 a.txt

    ➜ Test git:(5cc4941) git push
    fatal: 您当前不在一个分支上。
    现在为推送当前(分离头指针)的历史,使用

    git push origin HEAD:<远程分支名字>

    ➜ Test git:(5cc4941) git push origin HEAD:master
    对象计数中: 2, 完成.
    Delta compression using up to 4 threads.
    压缩对象中: 100% (2/2), 完成.
    写入对象中: 100% (2/2), 250 bytes | 250.00 KiB/s, 完成.
    Total 2 (delta 0), reused 0 (delta 0)
    To github.com:lamborghini1993/Test.git
    f8b5eed..5cc4941 HEAD -> master
    ➜ Test git:(5cc4941) cd ..

    ➜ other git:(master) ✗ git status
    位于分支 master
    您的分支与上游分支 'origin/master' 一致。

    尚未暂存以备提交的变更:
    (使用 "git add <文件>..." 更新要提交的内容)
    (使用 "git checkout -- <文件>..." 丢弃工作区的改动)

    修改: Test (新提交)

    修改尚未加入提交(使用 "git add" 和/或 "git commit -a")

    ➜ other git:(master) ✗ git commit -a -m "test"
    [master 757352f] test
    1 file changed, 1 insertion(+), 1 deletion(-)

    ➜ other git:(master) git push
    对象计数中: 2, 完成.
    Delta compression using up to 4 threads.
    压缩对象中: 100% (2/2), 完成.
    写入对象中: 100% (2/2), 239 bytes | 239.00 KiB/s, 完成.
    Total 2 (delta 1), reused 0 (delta 0)
    remote: Resolving deltas: 100% (1/1), completed with 1 local object.
    To github.com:lamborghini1993/Test2.git
    461dca1..757352f master -> master

    ➜ other git:(master) git status
    位于分支 master
    您的分支与上游分支 'origin/master' 一致。

    无文件要提交,干净的工作区
  • 注意:提交子模块时一定要指明远程分支git push origin HEAD:master

更新子模块

  1. 在父目录下执行:git submodule update
  2. 在子目录下执行:git pull origin HEAD:master
  • 更新子目录时最好,一起更新,不然父目录会出现修改
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    ➜  Test git:(f8b5eed) git pull origin HEAD:master
    来自 github.com:lamborghini1993/Test
    f8b5eed..5cc4941 -> master
    更新 f8b5eed..5cc4941
    Fast-forward
    a.txt | 0
    1 file changed, 0 insertions(+), 0 deletions(-)
    create mode 100644 a.txt

    ➜ Test git:(5cc4941) ls
    a.txt 新建文本文档.txt

    ➜ Test git:(5cc4941) cd ..

    ➜ other2 git:(master) ✗ git status
    位于分支 master
    您的分支与上游分支 'origin/master' 一致。

    尚未暂存以备提交的变更:
    (使用 "git add <文件>..." 更新要提交的内容)
    (使用 "git checkout -- <文件>..." 丢弃工作区的改动)

    修改: Test (新提交)

    修改尚未加入提交(使用 "git add" 和/或 "git commit -a")

    ➜ other2 git:(master) ✗ git pull
    remote: Enumerating objects: 3, done.
    remote: Counting objects: 100% (3/3), done.
    remote: Compressing objects: 100% (1/1), done.
    remote: Total 2 (delta 1), reused 2 (delta 1), pack-reused 0
    展开对象中: 100% (2/2), 完成.
    来自 https://github.com/lamborghini1993/Test2
    461dca1..757352f master -> origin/master
    更新 461dca1..757352f
    Fast-forward
    Test | 2 +-
    1 file changed, 1 insertion(+), 1 deletion(-)

    ➜ other2 git:(master) git status
    位于分支 master
    您的分支与上游分支 'origin/master' 一致。

    无文件要提交,干净的工作区

删除子模块

git 并不支持直接删除Submodule需要手动删除对应的文件

  1. 删除子模块文件夹

    1
    2
    3
    4
    5
    ➜  other2 git:(master) git rm --cached Test 
    rm 'Test'
    ➜ other2 git:(master) ✗ ls
    README.md Test
    ➜ other2 git:(master) ✗ rm -rf Test
  2. 删除.gitmodules文件中相关子模块信息
    [submodule “Test”]
    path = Test
    url = git@github.com:lamborghini1993/Test.git

  3. 删除.git/config中的相关子模块信息
    [submodule “Test”]
    url = git@github.com:lamborghini1993/Test.git

  4. 删除.git文件夹中的相关子模块文件

1234
XiaoHao

XiaoHao

人生苦短、我用python

32 日志
9 分类
15 标签
GitHub CSDN
© 2020 XiaoHao
由 Hexo 强力驱动
|
主题 — NexT.Pisces v5.1.4