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]
.
Example 1:
Input: nums = [5,7,7,8,8,10]
, target = 8
Output: [3,4]
Example 2:
Input: nums = [5,7,7,8,8,10]
, target = 6 Output: [-1,-1]
Answer Code:
#include<stdio.h> #include<iostream> #include<vector> #include<algorithm> #include<stack> #include<string> using namespace std; class Solution { public: vector<int> searchRange(vector<int>& nums, int target) { int n=nums.size(); cout<<"n:"<<n<<endl; vector<int> res; if(n==1) { if(target==nums[0]) { res.push_back(0); res.push_back(0); return res; } else { res.push_back(-1); res.push_back(-1); return res; } } int left=0,right=n-1; int flag=0,mid=0; while(left<=right) { mid=(left+right)/2; cout<<"mid:"<<mid<<endl; if(nums[mid]==target) { flag=1; break; } if(nums[mid]>target) { right=mid-1; } else if(nums[mid]<target) { left=mid+1; //cout<<"left:"<<left<<endl; } } if(flag==0) { res.push_back(-1); res.push_back(-1); return res; } else if(flag==1) { int temp=mid,temp1=mid; //cout<<"mid:"<<mid<<endl; cout<<"n:"<<n<<endl; while(temp>0&&nums[temp]==nums[temp-1]) { temp--; } while(temp1<n-1&&nums[temp1]==nums[temp1+1]) { temp1++; } res.push_back(temp); res.push_back(temp1); } return res; } }; int main() { int target=4; vector<int> a,res; a.push_back(1); a.push_back(4); // a.push_back(2); // a.push_back(8); // a.push_back(8); // a.push_back(10); Solution s; res=s.searchRange(a,target); for(auto t:res) { cout<<t<<endl; } return 0; }
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.
Example 1:
Input: [1,3,5,6], 5 Output: 2
Example 2:
Input: [1,3,5,6], 2 Output: 1
Example 3:
Input: [1,3,5,6], 7 Output: 4
Example 4:
Input: [1,3,5,6], 0 Output: 0
#include<stdio.h> #include<iostream> #include<vector> #include<algorithm> #include<stack> #include<string> using namespace std; class Solution { public: int searchInsert(vector<int>& nums, int target) { int n=nums.size(),res=0; if(nums[n-1]<target) return n; while(nums[res]<=target&&res<n-1) { if(nums[res]==target) return res; res++; } return res; } }; int main() { int target=7,res=0; vector<int> a; a.push_back(1); a.push_back(3); a.push_back(5); a.push_back(6); // a.push_back(8); // a.push_back(10); Solution s; res=s.searchInsert(a,target); cout<<res<<endl; return 0; }
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
- Each row must contain the digits
1-9
without repetition. - Each column must contain the digits
1-9
without repetition. - Each of the 9
3x3
sub-boxes of the grid must contain the digits1-9
without repetition.
A partially filled sudoku which is valid.
The Sudoku board could be partially filled, where empty cells are filled with the character '.'
.
Example 1:
Input: [ ["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
Example 2:
Input: [ ["8","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: false Explanation: Same as Example 1, except with the 5 in the top left corner being modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
ATTENTION:
The most important code is memset(map,0,sizeof(map));
I use this to reset the map array at right place.
Only in the right place ,can we make it right.
Especially,when we loop column and 3*3,this code need to be different place.
The judge way is to determine when to start a new count
#include<stdio.h> #include<iostream> #include<vector> #include<algorithm> #include<stack> #include<string> #include<memory> #include<memory.h> using namespace std; class Solution { public: bool isValidSudoku(vector<vector<char>>& board) { if(board.size()!=9||board[0].size()!=9) return false; int map[10]={0},num=0; for(int i=0;i<9;i++) { memset(map,0,sizeof(map)); for(int j=0;j<9;j++) { if(board[i][j]=='.') continue; if(board[i][j]>='0'&&board[i][j]<='9') { num=board[i][j]-'0'; } if(map[num]==0) { map[num]++; } else { return false; } } } for(int j=0;j<9;j++) { memset(map,0,sizeof(map)); for(int i=0;i<9;i++) { if(board[i][j]=='.') continue; if(board[i][j]>='0'&&board[i][j]<='9') { num=board[i][j]-'0'; } if(map[num]==0) { map[num]=1; } else { return false; } } } for(int i=0;i<9;i+=3) { for(int j=0;j<9;j+=3) { int k=i+3,p=j+3; memset(map,0,sizeof(map)); for(int t=i;t<k;t++) { for(int l=j;l<p;l++) { if(board[t][l]=='.') continue; if(board[t][l]>='0'&&board[t][l]<='9') { num=board[t][l]-'0'; } if(map[num]==0) { map[num]++; } else { return false; } } } } } return true; } }; int main() { bool res=0; // a.push_back(8); // a.push_back(10); vector<vector<char>> board={ {'.','.','4','.','.','.','6','3','.'}, {'.','.','.','.','.','.','.','.','.'}, {'5','.','.','.','.','.','.','9','.'}, {'.','.','.','5','6','.','.','.','.'}, {'4','.','3','.','.','.','.','.','1'}, {'.','.','.','7','.','.','.','.','.'}, {'.','.','.','5','.','.','.','.','.'}, {'.','.','.','.','.','.','.','.','.'}, {'.','.','.','.','.','.','.','.','.'} }; Solution s; res=s.isValidSudoku(board); cout<<res<<endl; return 0; }