본문 바로가기

파이썬24

[Python] 2차원 배열 정렬 방법 Python의 list는 sort 함수를 기본적으로 제공한다. sort(key= 기준값, lambda 사용가능 , reverse= Boolean) 1. 기본 2차원 배열 정렬 list = [["kim", 80], ["kang", 90], ["jung", 50]] list.sort() print(list) sort 함수에 조건이 없다면 list[0]을 기준으로 오름차순 정렬된다. [['jung', 50], ['kang', 90], ['kim', 80]] 2. key에 labmda를 이용한 컬럼 정렬 sort에 key를 첫 번째 열(x[0])을 기준으로 세팅하게 된다면 위와 동일한 결과물을 얻을 수 있다. list.sort(key=lambda x:x[0]) 3. n번째 컬럼 기준 정렬 정렬 key를 변경함으.. 2022. 7. 31.
[Python] "return list.sort()"는 None 일 때 Python에서 List.sort()를 사용할 때 None이 return 된다. list = [1, 5, 4, 2, 3] print(list.sort()) 위의 코드은 None을 출력한다. 실제 sort()의 경우 return 값은 존재하지 않고 list 자체를 변경해 준다. 그러므로 list.sort()가 아닌 list를 출력한다면 변경 된 값을 확인할 수 있다. list = [1, 5, 4, 2, 3] list.sort() print(list) sort() 후 list를 출력하면 [1, 2, 3, 4, 5]가 출력된다. 2022. 7. 31.
[LeetCode] 38. Count and Say The count-and-say sequence is a sequence of digit strings defined by the recursive formula: countAndSay(1) = "1" countAndSay(n) is the way you would "say" the digit string from countAndSay(n-1), which is then converted into a different digit string. To determine how you "say" a digit string, split it into the minimal number of substrings such that each substring contains exactly one unique digit.. 2022. 7. 22.
[LeetCode] 35. Search Insert Position Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You must write an algorithm with O(log n) runtime complexity. Example 1: Input: nums = [1,3,5,6], target = 5 Output: 2 Example 2: Input: nums = [1,3,5,6], target = 2 Output: 1 Example 3: Input: nums = [1,3,5,6], target = 7.. 2022. 7. 22.
[LeetCode] 28. Implement strStr() Implement strStr(). Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Clarification: What should we return when needle is an empty string? This is a great question to ask during an interview. For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strs.. 2022. 7. 22.
[Leetcode] 27. Remove Element Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The relative order of the elements may be changed. Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements.. 2022. 7. 22.
[LeetCode] 26. Remove Duplicates from Sorted Array Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements aft.. 2022. 7. 22.
[LeetCode] 21. Merge Two Sorted Lists You are given the heads of two sorted linked lists list1 and list2. Merge the two lists in a one sorted list. The list should be made by splicing together the nodes of the first two lists. Return the head of the merged linked list. Example 1: Input: list1 = [1,2,4], list2 = [1,3,4] Output: [1,1,2,3,4,4] Example 2: Input: list1 = [], list2 = [] Output: [] Example 3: Input: list1 = [], list2 = [0].. 2022. 7. 21.
[LeetCode] 20. Valid Parentheses Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Example 1: Input: s = "()" Output: true Example 2: Input: s = "()[]{}" Output: true Example 3: Input: s = "(]" Output: false Constraints: 1 2022. 7. 21.
반응형