-
Notifications
You must be signed in to change notification settings - Fork 1
Description
Convert Binary Number in a Linked List to Integer
Given head which is a reference node to a singly-linked list. The value of each node in the linked list is either 0 or 1. The linked list holds the binary representation of a number.
Return the decimal value of the number in the linked list.
Example 1:
Input: head = [1,0,1]
Output: 5
Explanation: (101) in base 2 = (5) in base 10
Example 2:
Input: head = [0]
Output: 0
Example 3:
Input: head = [1]
Output: 1
Example 4:
Input: head = [1,0,0,1,0,0,1,1,1,0,0,0,0,0,0]
Output: 18880
Example 5:
Input: head = [0,0]
Output: 0
Constraints:
- The Linked List is not empty.
- Number of nodes will not exceed 30.
- Each node's value is either 0 or 1.
class Solution {
public:
int getDecimalValue(ListNode* head) {
if(head == NULL)
return 0;
if(head->next == NULL)
return head->val;
int num = 0;
while (head){
num *= 2;
num += head->val;
head = head -> next;
}
return num;
}
};
Sequential Digits
An integer has sequential digits if and only if each digit in the number is one more than the previous digit.
Return a sorted list of all the integers in the range [low, high]
inclusive that have sequential digits.
Example 1:
Input: low = 100, high = 300
Output: [123,234]
Example 2:
Input: low = 1000, high = 13000
Output: [1234,2345,3456,4567,5678,6789,12345]
Constraints:
- 10 <= low <= high <= 10^9
算法思路:一共36个数,通过打表即可解决。
class Solution {
public List<Integer> sequentialDigits(int low, int high) {
int[] list = {12, 23, 34, 45, 56, 67, 78, 89,
123, 234, 345, 456, 567, 678, 789,
1234, 2345, 3456, 4567, 5678, 6789,
12345, 23456, 34567, 45678, 56789,
123456, 234567, 345678, 456789,
1234567, 2345678, 3456789,
12345678, 23456789,
123456789};
List result = new LinkedList();
for(int i = 0; i < list.length; i++){
if(list[i] <= high && list[i] >= low){
result.add(list[i]);
}
}
return result;
}
}
Maximum Side Length of a Square with Sum Less than or Equal to Threshold
Given a m x n matrix mat and an integer threshold. Return the maximum side-length of a square with a sum less than or equal to threshold or return 0 if there is no such square.
Example 1:
Input: mat = [[1,1,3,2,4,3,2],[1,1,3,2,4,3,2],[1,1,3,2,4,3,2]], threshold = 4
Output: 2
Explanation: The maximum side length of square with sum less than 4 is 2 as shown.
Example 2:
Input: mat = [[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2],[2,2,2,2,2]], threshold = 1
Output: 0
Example 3:
Input: mat = [[1,1,1,1],[1,0,0,0],[1,0,0,0],[1,0,0,0]], threshold = 6
Output: 3
Example 4:
Input: mat = [[18,70],[61,1],[25,85],[14,40],[11,96],[97,96],[63,45]], threshold = 40184
Output: 2
Constraints:
1 <= m, n <= 300
m == mat.length
n == mat[i].length
0 <= mat[i][j] <= 10000
0 <= threshold <= 10^5
class Solution {
public int maxSideLength(int[][] mat, int threshold) {
int result = 0;
for(int i = 0; i < mat.length; i++){
for(int j = 0; j < mat[0].length; j++){
if(j != 0){
mat[i][j] += mat[i][j - 1];
}
int len = 0;
int maxlen = Math.min(i, j) + 1;
while (len < maxlen){
int area = 0;
for(int k = 0; k < len + 1; k++){
int prefix = j - len - 1 < 0 ? 0 : mat[i - k][j - len - 1];
area += mat[i - k][j] - prefix;
}
if(area > threshold) {
break;
}
len++;
}
result = len > result ? len : result;
}
}
return result;
}
}