#26 Remove duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such, all element appear only once and return the new L Ength.
Do the allocate extra space for another array, and you must does this on place with constant memory.
For example,
Given input array nums = [1,1,2]
,
Your function should return length = 2
, with the first of the elements of nums being and 1
2
Respectivel Y. It doesn ' t matter what are you leave beyond the new length.
------This question has been submitted several times AC, the main function is not only to return the number of repetitions count, but also to place the number of non-repetition in the first count of the original array position, reading comprehension is very important ah.
int RemoveDuplicates (int* nums, int numssize) {int count=1;//14msint i;if (numssize==0) return 0;if (numssize==1) return 1; for (i=1;i<=numssize-1;i++) {if (nums[i]!=nums[i-1]) {Nums[count] = nums[i];//Not only the number of repetitions is required, but also the non-repeating element in front of the count++;}} return count;}
#27 Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn ' t matter what are you leave beyond the new length.
Removes the specified element from the array, returning the number of remaining elements count. -------at the same time, the first count elements in the original array are the remaining elements of the original array, otherwise they will not be AC.
Wrote 2 methods, the method of time complexity and space complexity are relatively high, but easy to understand; Method two uses a double pointer, I and count points to both the original element and the element that is not the specified delete value
0msint removeelement (int* nums, int numssize, int val) { <span style= "White-space:pre" ></span>int i,j = 0,count = 0;int *a;a = (int *) malloc (sizeof (int) *numssize); for (i=0; i < numssize; i++) {if (nums[i] = = val) count++;else A[j++]=nums[i];} for (i=0; i < J; i++) nums[i] = A[i];return numssize-count;}
0msint removeelement (int* nums, int numssize, int val) { <span style= "White-space:pre" ></span>int i = 0 , Count = 0;<span style= "White-space:pre" ></span>while (i < numssize) {if (nums[i] = = val) i++;else nums[ count++] = nums[i++];} return count;}
#28 Implement strStr ()
Returns the index of the first occurrence of needle in haystack, or-1 if needle are not part of haystack.
Classic string pattern matching problem, mainly has BF and KMP algorithm, concrete analysis can be seen in this blog related blog "string pattern matching BF and KMP algorithm"
Bfint strStr (char* haystack, char* needle) { int i=0,j=0,k;int len1 = strlen (haystack); int len2 = strlen (needle); if ( len2==0) return 0;if (len1==0&&len2!=0) return-1; while (I<len1 && j<len2) {if (Haystack[i]==needle[j]) {i++;j++;} else{i=i-j+1;j=0;}} if (j>=len2) K=i-len2;elsek=-1;return K;}
#36 Valid Sudoku
Determine if a Sudoku is valid, according To:sudoku puzzles-the Rules.
The Sudoku board could be partially filled, where empty cells is filled with the character ‘.‘
.
A partially filled sudoku which is valid.
To determine whether a given Sudoku is a valid Sudoku: Only consider the absence of duplicate numbers, the most intuitive solution is to judge each column, each row, each 3*3 block contains no duplicate numbers
Direct paste code
BOOL Isvalidsudoku (char** board, int boardrowsize, int boardcolsize) {int hash[10];int i,j,k,m,n;char small[3][3]; memset (hash,0,sizeof (hash)); if (boardrowsize%3! = 0 | | boardcolsize%3! = 0) return false;for (i=0; i < boardrowsize; i++) for (j=0;j<boardcolsize;j++) {if (board[i][j] = = '. ' | | (Board[i][j] <= ' 9 ' && board[i][j] >= ' 1 ')) Continue;elsereturn false;} for (i = 0; i < boardrowsize; i++) {memset (hash,0,sizeof (hash)), for (j = 0; J < Boardcolsize; J + +) if (board[i][j]! = '.') HASH[BOARD[I][J]-' 0 ']++;for (k = 1;k < 10;k++) if (Hash[k] > 1) return false;} for (j = 0; J < Boardcolsize; J + +) {memset (hash,0,sizeof (hash)); for (i = 0; i < boardrowsize; i++) if (board[i][j]! = '.') HASH[BOARD[I][J]-' 0 ']++;for (k = 1; k <; k++) if (Hash[k] > 1) return false;} memset (hash,0,sizeof (hash)); for (i = 0; i < boardrowsize, i = i+3) for (j = 0; J < boardcolsize; j = j+3) {Small[0][0] = BOARD[I][J];SMALL[0][1] = board[i][j+1];small[0][2] = board[i][j+2];small[1][0] = board[i+1][j];small[1][1] = board[i+1][j+1];small[1][2] = board[i+1][j+2];small[2][0] = board[i+2][j];small[2][1 ] = board[i+2][j+1];small[2][2] = board[i+2][j+2];for (m=0; m < 3; m++) for (n = 0; n < 3; n++) {if (small[m][n]! = '. ') Hash[small[m][n]-' 0 ']++;} for (k=1; k < k++) if (Hash[k] > 1) return False;memset (hash,0,sizeof (hash));} return true;}
The following are simplified:
BOOL Isparticallyvalid (char** board,int x1,int y1,int x2,int y2) {int hash[10],i,j;memset (hash,0,sizeof (hash)); for (i = X1; I <= x2; i++) {for (j = y1; j <= Y2; j + +) {if (board[i][j]! = '. ') { hash[board[i][j]-' 0 ']++; if (hash[board[i][j]-' 0 '] > 1) return false;}}} return true;} BOOL Isvalidsudoku (char** board, int boardrowsize, int boardcolsize) {int i,j;//determines whether each column of each row contains a repeating element for (i = 0; i < 9; i++) { if (!isparticallyvalid (board,i,0,i,8)) return false;if (!isparticallyvalid (Board,0,i,8,i)) return false;} Determine if the 3*3 block contains duplicate elements for (i = 0; i < 3; i++) {for (j = 0; J < 3; J + +) {if (!isparticallyvalid (board,i*3,j*3,i*3+2,j*3+2)) re Turn false;}} return true;}
Leetcode--easy Series 3