//My solution
//12ms:67; 10.7mb:100
class Solution {
public:
vector printNumbers(int n) {
int endn=pow(10,n);
vector re(endn-1);
for(int i=1;i
遍历循环输出即可:
先找到等于val值的节点位置,将前一节点的下一节点改为下下节点
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
//My solution
//12ms:83, O(n); 8.8mb:100, O(1)
class Solution {
public:
ListNode* deleteNode(ListNode* head, int val) {
ListNode* p = head;
if(head->val==val)
return head->next;
while(p->next->val!=val)
{
p=p->next;
}
p->next=p->next->next;
return head;
}
};
1.首先对可能出现的状况列状态转换图(具体可参考数字电子电路)
2.根据状态转换图列状态转换表
3.确定最终状态(完成状态)和失败状态(未完成状态)
//My solution
//有限状态机
//4ms:75; 7mb:100;
class Solution {
public:
//状态转换表
int state[10][6]={{1 ,2 ,10 ,3 ,-1,-1},
{-1,-1,10 ,3 ,-1,-1},
{8 ,-1,9 ,4 ,5 ,-1},
{8 ,-1,9,4 ,5, -1},
{-1,6 ,-1,7 ,-1,-1},
{-1,-1,-1,7 ,-1,-1},
{8 ,-1,-1,7 ,-1,-1},
{8 ,-1,-1,-1,-1,-1},
{8 ,-1,-1,4 ,5,-1},
{-1,-1,-1,4 ,-1,-1}};
bool isNumber(string s) {
int curState=1;
int dot=0;
//状态映射
map digStr{{' ', 0},{'s', 1}, {'.',2},{'d',3},{'e', 4},{'q', 5}};
char c;
for(int i=0;i='0'&&s[i]<='9')
c='d';
else if(s[i]!=' '&&s[i]!='e'&&s[i]!='.')
c='q';
else
c=s[i];
if(c=='.')
dot++;
//状态转换
curState=state[curState-1][digStr.at(c)];
if(curState==-1)
return false;
}
if(curState==3||curState==4||curState==7||curState==8||curState==9)
if(dot<2)
return true;
return false;
}
};
1.设置前后两个指针;
2.从0索引开始,判断是否为奇数:
当是奇数时,后指针不变,前指针向后移动;
当是偶数时,将此位置的数与后指针位置的数相交换,前指针不变,后指针向前移动;
直到前指针与后指针重合为止。
//My solution
//44ms:56; 18mb:100
class Solution {
public:
vector exchange(vector& nums) {
int i=0, n=nums.size(),k=0;
for(i=0;i<(n-1);)
{
if(nums[i]%2==0)
{
k=nums[i];
nums[i]=nums[n-1];
nums[n-1]=k;
n--;
}
else
i++;
}
return nums;
}
};