leetcode 题解代码整理 26-30题
来源:互联网 发布:excel找出a列重复数据 编辑:程序博客网 时间:2024/06/05 14:34
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.
class Solution {public: int removeDuplicates(vector<int>& nums) { if (nums.size()==0) return 0; int temp=1; for (int i=1;i<nums.size();i++) { if (nums[i]==nums[i-1]); else nums[temp++]=nums[i]; } return temp; }};
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.
删除数组中指定元素
class Solution {public: int removeElement(vector<int>& nums, int val) { int mark=0; for (int i=0;i<nums.size();i++) if (nums[i]==val); else nums[mark++]=nums[i]; return mark; }};
Implement strStr()
Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
模拟strstr函数class Solution {public: int strStr(string haystack, string needle) { int a=haystack.length(); int b=needle.length(); int j,i; for (i=0;i+b<=a;i++) { for (j=0;j<b;j++) if (haystack[i+j]!=needle[j]) break; if (j==b) return i; } return -1; }};
Divide Two Integers
Divide two integers without using multiplication, division and mod operator.
If it is overflow, return MAX_INT.
不用除法写除法 --# 注意处理越界class Solution {public: int divide(int dividend, int divisor) { int x=1; if (dividend<0) x*=-1; if (divisor<0) x*=-1; long long big=abs((long long )dividend); long long small=abs((long long )divisor); long long cnt=1; long long mark=small; while (mark<big) { mark<<=1; cnt<<=1; } long long ans=0; while (mark>=small) { if (mark<=big) { big-=mark; ans+=cnt; } cnt>>=1; mark>>=1; } ans*=x; if (ans==INT_MAX+1ll) ans--; return ans; }};
Substring with Concatenation of All Words
You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring(s) in s that is a concatenation of each word in wordsexactly once and without any intervening characters.
For example, given:
s: "barfoothefoobarman"
words: ["foo", "bar"]
You should return the indices: [0,9]
.
(order does not matter).
class Solution {public: vector<int> findSubstring(string s, vector<string>& words) { vector<int>ans; if (words.size()==0) return ans; int wordNum=words.size(); int wordLen=words[0].length(); int wordsLen=wordNum*wordLen; map<string,int>word; for (int i=0;i<wordNum;i++) { if (word.find(words[i])==word.end()) word[words[i]]=1; else word[words[i]]++; } int i,j; for (i=0;i<=s.length()-wordsLen;i++) { map<string,int>cur; for (j=i;j<i+wordsLen;j+=wordLen) { string temp=s.substr(j,wordLen); if (cur.find(temp)==cur.end()) cur[temp]=1; else cur[temp]++; if (word.find(temp)==word.end()) break; if (cur[temp]>word[temp]) break; } if (j>=i+wordsLen) ans.push_back(i); } return ans; }};
- leetcode 题解代码整理 26-30题
- leetcode 题解代码整理 1-5题
- leetcode 题解代码整理 6-10题
- leetcode 题解代码整理 11-15题
- leetcode 题解代码整理 16-20题
- leetcode 题解代码整理 21-25题
- leetcode 题解代码整理 31-35题
- leetcode 题解代码整理 36-40题
- leetcode 题解 整理
- leetcode 26题题解
- LeetCode题解整理版(一)
- LeetCode题解整理版(二)
- LeetCode题解整理版(二)
- LeetCode题解整理版(一)
- leetcode水题题解
- LeetCode代码整理(一)
- LeetCode代码整理(二)
- LeetCode代码整理(三)
- RIP基础理论
- OSPF基础理论
- 通过SeekBar对ImageView进行缩放,旋转
- 集成百度地图的时候的常见错误及其处理方法
- 文章标题
- leetcode 题解代码整理 26-30题
- EIGRP基础理论
- 面向对象的CSS的作用和注意事项
- IP 路由常见问题合集(上)
- 最长递增子序列 LIS 时间复杂度O(nlogn)的Java实现
- HTML常用的表单创建
- 迅雷防踢补丁:一个刷流量木马的简单分析
- 360云 Ubuntu安装Dokcer
- 素数