Leetcode--easy系列3
#26 Remove Duplicates from Sorted Array
Given a sorted array, 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 in place with constant memory.
For example,
Given input array nums = [1,1,2]
,
Your function should return length = 2
, with the first two elements of nums being 1
and 2
respectively.
It doesn't matter what you leave beyond the new length.
------这个题提交了好几次才AC,主要是函数不仅要返回不反复数的个数count。还要求将不反复的数放置在原始数组的前count个位置。阅读理解非常重要啊。
- int removeDuplicates(int* nums, int numsSize) {
- int count=1;//14ms
- int 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];//不仅要求出不反复个数,还要将不反复元素放在前面
- 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 you leave beyond the new length.
从数组中去除指定元素。返回剩余元素个数count。-------同一时候,原来数组中前count个元素即为原来数组的剩余元素。否则不会AC。
写了2种方法,法一时间复杂度和空间复杂度都比較高,但便于理解。法二使用了双指针,i和count分别指向原来的元素和非指定删除值的元素
- //0ms
- int 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;
- }
- //0ms
- int 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 is not part of haystack.
经典的串的模式匹配问题,主要有BF和KMP算法,详细解析可见本博客相关博客《串模式匹配之BF和KMP算法》
- //BF
- int 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;
- else
- k=-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 are filled with the character '.'
.
A partially filled sudoku which is valid.
推断给定数独是否是一个有效数独:仅仅考虑没有反复数字。最直观的解法是推断每一列,每一行。每个3*3的方块内不包括反复的数字
直接贴代码
- 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;
- else
- return 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 < 10; 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 < 10; k++)
- if(hash[k] > 1)
- return false;
- memset(hash,0,sizeof(hash));
- }
- return true;
- }
简化后例如以下:
- 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;
- //判定每一行每一列是否包括反复元素
- for(i = 0; i < 9; i++)
- {
- if(!isParticallyValid(board,i,0,i,8))
- return false;
- if(!isParticallyValid(board,0,i,8,i))
- return false;
- }
- //判定3*3的方块内是否包括反复元素
- 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))
- return false;
- }
- }
- return true;
- }
Leetcode--easy系列3的更多相关文章
- hdu 2049 不easy系列之(4)——考新郎
不easy系列之(4)--考新郎 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- LeetCode——single-number系列
LeetCode--single-number系列 Question 1 Given an array of integers, every element appears twice except ...
- HDU 2045不easy系列之三LELE的RPG难题(趋向于DP的递推)
不easy系列之(3)-- LELE的RPG难题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- hdu1465不easy系列之中的一个(错排)
版权声明:本文为博主原创文章,未经博主同意不得转载. vasttian https://blog.csdn.net/u012860063/article/details/37512659 转载请注明出 ...
- Leetcode算法系列(链表)之删除链表倒数第N个节点
Leetcode算法系列(链表)之删除链表倒数第N个节点 难度:中等给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点.示例:给定一个链表: 1->2->3->4-&g ...
- Leetcode算法系列(链表)之两数相加
Leetcode算法系列(链表)之两数相加 难度:中等给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字.如果,我们将 ...
- leetcode easy problem set
*勿以浮沙筑高台* 持续更新........ 题目网址:https://leetcode.com/problemset/all/?difficulty=Easy 1. Two Sum [4m ...
- [Leetcode] Sum 系列
Sum 系列题解 Two Sum题解 题目来源:https://leetcode.com/problems/two-sum/description/ Description Given an arra ...
- LeetCode 笔记系列16.3 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]
题目:Given a string S and a string T, find the minimum window in S which will contain all the characte ...
- 决战Leetcode: easy part(51-96)
本博客是个人原创的针对leetcode上的problem的解法,所有solution都基本通过了leetcode的官方Judging,个别未通过的例外情况会在相应部分作特别说明. 欢迎互相交流! em ...
随机推荐
- Lightoj-1356 Prime Independence(质因子分解)(Hopcroft-Karp优化的最大匹配)
题意: 找出一个集合中的最大独立集,任意两数字之间不能是素数倍数的关系. 思路: 最大独立集,必然是二分图. 最大数字50w,考虑对每个数质因子分解,然后枚举所有除去一个质因子后的数是否存在,存在则建 ...
- SPOJ IITWPC4F - Gopu and the Grid Problem (双线段树区间修改 区间查询)
Gopu and the Grid Problem Gopu is interested in the integer co-ordinates of the X-Y plane (0<=x,y ...
- 21、Flask实战第21天:常用的Flask钩子函数
在Flask中钩子函数是使用特定的装饰器装饰的函数.为什么叫钩子函数呢?是因为钩子函数可以在正常执行的代码中,插入一段自己想要执行的代码.那么这种函数就叫做钩子函数. before_first_req ...
- ASP.NET Core 2.2 基础知识(十二) 发送 HTTP 请求
可以注册 IHttpClientFactory 并将其用于配置和创建应用中的 HttpClient 实例. 这能带来以下好处: 提供一个中心位置,用于命名和配置逻辑 HttpClient 实例. 例如 ...
- vc ini配置文件读写
ini文件(即Initialization file),这种类型的文件中通常存放的是一个程序的初始化信息.ini文件由若干个节(Section)组成,每个Section由若干键(Key)组成,每个Ke ...
- Java里的浅复制与深复制
1.浅复制与深复制概念 ⑴浅复制(浅克隆) 被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用仍然指向原来的对象.换言之,浅复制仅仅复制所考虑的对象,而不 复制它所引用的对象. ...
- 【动态规划】bzoj1649 [Usaco2006 Dec]Cow Roller Coaster
很像背包. 这种在一个数轴上进行操作的题常常需要对区间排序. f[i][j]表示距离到i时,花费为j时的权值之和. f[x[i]+l[i]][j+c[i]]=max{f[x[i]][j]+w[i]}( ...
- Postman Json测试接口
当传递Json数据时: 1.必须添加http头:content-type:application/json,否则会报错(后台取不到相对应的值) 注意:如果服务端只支持UTF-8,但程序未对提交数据进行 ...
- Spring整合jdbc-jdbc模板api详解
1, package com.songyan.jdbc2; public class User { private int id; private String name; public int ge ...
- JavaScript对JSON数据进行排序
var ajson= { "result":[ { "cid":1, "name":"aaa", "price ...