[
["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
Output: true
2020年7月12日 星期日
LeetCode 36. Valid Sudoku [Medium] [C++] 解題筆記
2020年7月11日 星期六
LeetCode 35. Search Insert Position [Easy] [C++] 解題筆記
Given a sorted array 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 may assume no duplicates in the array.
EX:
Input: [1,3,5,6], 5
Output: 2
LeetCode 34. Find First and Last Position of Element in Sorted Array [Medium] [C++] 解題筆記
Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1].
EX:
Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]
Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]
LeetCode 32. Longest Valid Parentheses [Hard] [C++] 解題筆記
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.
EX:
Input: "(()"
Output: 2
Explanation: The longest valid parentheses substring is "()"
Input: ")()())"
Output: 4
Explanation: The longest valid parentheses substring is "()()"
LeetCode 31. Next Permutation [Medium] [C++] 解題筆記
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place and use only constant extra memory.
EX:
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1
2020年7月10日 星期五
LeetCode 30. Substring with Concatenation of All Words [Hard] [C++] 解題筆記
[0,9]Explanation: Substrings starting at index 0 and 9 are "barfoo" and "foobar" respectively. The output order does not matter, returning [9,0] is fine too.
2020年7月9日 星期四
LeetCode 29. Divide Two Integers [Medium] [C++] 解題筆記
Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator.
Return the quotient after dividing dividend by divisor.
The integer division should truncate toward zero, which means losing its fractional part. For example, truncate(8.345) = 8 and truncate(-2.7335) = -2.
EX:
Input: dividend = 7, divisor = -3
Output: -2
Explanation: 7/-3 = truncate(-2.33333..) = -2.
2020年7月8日 星期三
LeetCode 28. Implement strStr() [Easy] [C++] 解題筆記
Implement strStr().
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
EX:
Input: haystack = "hello", needle = "ll"
Output: 2 Input: haystack = "aaaaa", needle = "bba" Output: -1
LeetCode 27. Remove Element [Easy] [C++] 解題筆記
Given an array nums and a value val, remove all instances of that value in-place and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
EX:
Given nums = [0,1,2,2,3,0,4,2], val = 2,
Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4. Note that the order of those five elements can be arbitrary. It doesn't matter what values are set beyond the returned length.
LeetCode 26. Remove Duplicates from Sorted Array [Easy] [C++] 解題筆記
Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
EX:
Given nums = [0,0,1,1,1,2,2,3,3,4],
Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively. It doesn't matter what values are set beyond the returned length.
2020年7月7日 星期二
LeetCode 25. Reverse Nodes in k-Group [Hard] [C++] 解題筆記
1->2->3->4->5 For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
LeetCode 24. Swap Nodes in Pairs [Medium] [C++] 解題筆記
Given a linked list, swap every two adjacent nodes and return its head.
You may not modify the values in the list's nodes, only nodes itself may be changed.
EX:
Given 1->2->3->4, you should return the list as 2->1->4->3.
2020年7月5日 星期日
LeetCode 23. Merge k Sorted Lists [Hard] [C++] 解題筆記
[ 1->4->5, 1->3->4, 2->6 ] Output: 1->1->2->3->4->4->5->6