1、Remove Element && Remove Duplicates from Sorted Array
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.
去掉所有的给定值,而且order可以改变,那这个题目就是一个指针放在数组的尾部,一个指针遍历,然后和数组尾部进行交换。 class Solution {
public:
int removeElement(int A[], int n, int elem) {
int last = n-1;
int first = 0;
int len = 0;
while(first<=last){
if(A[first]==elem){
int temp = A[first];
A[first] = A[last];
A[last] = temp;
last--;
}
else{
first++;
}
}
return first;
}
};
class Solution {
public:
int removeElement(int A[], int n, int elem) {
int last = n-1;
int first = 0;
int len = 0;
while(first<=last){
if(A[first]==elem){
int temp = A[first];
A[first] = A[last];
A[last] = temp;
last--;
}
else{
first++;
}
}
return first;
}
};
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 A = [1,1,2]
,
Your function should return length = 2
, and A is now [1,2]
.
int removeDuplicates(int A[], int n) {
if(n<=0||A==NULL)
return 0;
int newn = n;
int oldone = A[0];
int index = 1;
for(int i =1;i<n;i++){
if(oldone == A[i]){
newn--;
}
else{
oldone = A[i];
A[index++]=A[i];
}
}
return newn;
}
2、Palindrome Number &&
Reverse Integer
Determine whether an integer is a palindrome. Do this without extra space.
Could negative integers be palindromes? (ie, -1)
If you are thinking of converting the integer to string, note the restriction of using extra space.
You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
There is a more generic way of solving this problem.
class Solution {
public:
bool isPalindrome(int x) {
if(x<0){
return false;
}
int y = 0,z = x;
while(z/10!=0){
y =y*10+z%10;
z = z/10;
}
y =y*10+z%10;
if(x==y){
return true;
}
else{
return false;
}
}
};
class Solution {
public:
bool isPalindrome(int x) {
if(x<0){
return false;
}
int y = 0,z = x;
while(z/10!=0){
y =y*10+z%10;
z = z/10;
}
y =y*10+z%10;
if(x==y){
return true;
}
else{
return false;
}
}
};
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).
class Solution {
public:
int reverse(int x) {
if( x/10 == 0){
return x;
}
int result = 0;
int y = abs(x);
while(y!=0){
result = result*10+y%10;
y = y/10;
}
return (x>0?result:-result);
}
};
class Solution {
public:
int reverse(int x) {
if( x/10 == 0){
return x;
}
int result = 0;
int y = abs(x);
while(y!=0){
result = result*10+y%10;
y = y/10;
}
return (x>0?result:-result);
}
};
3、Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.
动态规划
class Solution {
public:
/*
vector 的存取速度显然比数组要慢很多啊
*/
string longestPalindrome(string s) {
int num = s.length();
bool table[1000][1000] = {false};
for (int i = 0; i < num; i++) {
table[i][i] = true;
}
int left = 0,right = 1;
for(int i = 0;i<num-1;i++){
table[i][i+1] = s[i]==s[i+1];
if(table[i][i+1]){
left = i;
right = 2;
}
}
for(int i = 2;i<num;i++){
for(int j = 0;j<num-i;j++){
table[j][j+i] = table[j+1][j+i-1]&&s[j]==s[j+i];
if(table[j][j+i]){
left = j;
right = i+1;
}
}
}
return s.substr(left,right);
}
};
class Solution {
public:
/*
vector 的存取速度显然比数组要慢很多啊
*/
string longestPalindrome(string s) {
int num = s.length();
bool table[1000][1000] = {false};
for (int i = 0; i < num; i++) {
table[i][i] = true;
}
int left = 0,right = 1;
for(int i = 0;i<num-1;i++){
table[i][i+1] = s[i]==s[i+1];
if(table[i][i+1]){
left = i;
right = 2;
}
}
for(int i = 2;i<num;i++){
for(int j = 0;j<num-i;j++){
table[j][j+i] = table[j+1][j+i-1]&&s[j]==s[j+i];
if(table[j][j+i]){
left = j;
right = i+1;
}
}
}
return s.substr(left,right);
}
};
4、Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
ListNode * head = NULL;
ListNode * p = NULL;
int addnum = 0;
while(l1!=NULL&&l2!=NULL){
int num = (l1->val+l2->val+addnum)%10;
addnum = (l1->val+l2->val+addnum)/10;
if(head == NULL){
head = new ListNode(num);
p = head;
}
else{
p->next = new ListNode(num);
p = p->next;
}
l1 = l1->next;
l2 = l2->next;
}
while(l1!=NULL){
int num = (l1->val+addnum)%10;
addnum = (l1->val+addnum)/10;
if(head == NULL){
head = new ListNode(num);
p = head;
}
else{
p->next = new ListNode(num);
p = p->next;
}
l1 = l1->next;
}
while(l2!=NULL){
int num = (l2->val+addnum)%10;
addnum = (l2->val+addnum)/10;
if(head == NULL){
head = new ListNode(num);
p = head;
}
else{
p->next = new ListNode(num);
p = p->next;
}
l2 = l2->next;
}
if(addnum!=0){
if(head == NULL){
head = new ListNode(addnum);
}
else{
p->next = new ListNode(addnum);
}
}
return head;
}
};
-------------------------------------------------------------------------------------------
以下为转
5、Substring with Concatenation of All Words
You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters.
For example, given:
S: "barfoothefoobarman"
L: ["foo", "bar"]
You should return the indices: [0,9]
.
(order does not matter).
vector<int> findSubstring(string S, vector<string> &L) {
int l_size = L.size();
if (l_size <= 0) {
return vector<int>();
}
vector<int> result;
map<string, int> word_count;
int word_size = L[0].size();
int i, j;
for (i = 0; i < l_size; ++i) {
++word_count[L[i]];
}
map<string, int> counting;
for (i = 0; i <= (int)S.length() - (l_size * word_size); ++i) {
counting.clear();
for (j = 0; j < l_size; ++j) {
string word = S.substr(i + j * word_size, word_size);
if (word_count.find(word) != word_count.end()) {
++counting[word];
if (counting[word] > word_count[word]) {
break;
}
}
else {
break;
}
}
if (j == l_size) {
result.push_back(i);
}
}
return result;
}
Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
历遍字符串,当当前字符出现过的时候,子串开始位置+1,否则更新locs数组中的hash值为当前位置。
<pre name="code" class="cpp">int lengthOfLongestSubstring(string s) {
int locs[256];//保存字符上一次出现的位置
memset(locs, -1, sizeof(locs));
int idx = -1, max = 0;//idx为当前子串的开始位置-1
for (int i = 0; i < s.size(); i++)
{
if (locs[s[i]] > idx)//如果当前字符出现过,那么当前子串的起始位置为这个字符上一次出现的位置+1
{
idx = locs[s[i]];
}
if (i - idx > max)
{
max = i - idx;
}
locs[s[i]] = i;
}
return max;
}
<pre name="code" class="cpp">int lengthOfLongestSubstring(string s) {
int locs[256];//保存字符上一次出现的位置
memset(locs, -1, sizeof(locs));
int idx = -1, max = 0;//idx为当前子串的开始位置-1
for (int i = 0; i < s.size(); i++)
{
if (locs[s[i]] > idx)//如果当前字符出现过,那么当前子串的起始位置为这个字符上一次出现的位置+1
{
idx = locs[s[i]];
}
if (i - idx > max)
{
max = i - idx;
}
locs[s[i]] = i;
}
return max;
}
6、
Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.
You may not alter the values in the nodes, only nodes itself may be changed.
Only constant memory is allowed.
For example,
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
1. 完全逆转一个链表: 每遍历到一个节点, 就将该节点放在链表首位
2. 在(1) 的基础上添加大小为 j 的窗口
<pre name="code" class="cpp">ListNode *reverseKGroup(ListNode *head, int k) {
ListNode *newHead = new ListNode(0);
newHead->next = head;
int cnt = 0;
ListNode *cur_node = head;
ListNode *last_tail = newHead;
while(cur_node) {
cnt++;
if(cnt == k) {
ListNode *cp = cur_node->next;
cur_node->next = NULL;
last_tail = reverseList(last_tail->next, last_tail);
last_tail->next = cp;
cur_node = cp;
cnt = 0;
continue;
}
cur_node = cur_node->next;
}
return newHead->next;
}
ListNode *reverseList(ListNode*head, ListNode*last_tail) {
ListNode *next_node = head->next;
ListNode *res = head;
while(next_node) {
ListNode *tmp = next_node->next;
next_node->next = head;
head = next_node;
next_node = tmp;
}
last_tail->next = head;
return res;
}