LeetCode 字符串(一)

6. ZigZag Conversion

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)

P   A   H   N
A P L S I I G
Y   I   R
And then read line by line: "PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:

string convert(string s, int numRows);
Example 1:

Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"
Example 2:

Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:

P     I    N
A   L S  I G
Y A   H R
P     I

 观察规律:

0     6     C
1   5 7   B D
2 4   8 A   E
3     9     F
0,6处于相同位置,和行数有关:Total = 2*(numRows- 1)

中间上升位置4,5,A,B规律为  j + 2*numRows-2 - 2*i    j表示列数,i表示行数
//这简直了 数学规律啊啊啊   参考了大神们的思路   虐心
class Solution {
public:
    string convert(string s, int numRows)
    {
        int nLen = s.size();
        if(numRows<=1)
        {
            return s;
        }
        string nResult = "";
        int num = 2*numRows-2;
        for(int i=0;i

 

344. Reverse String

Write a function that reverses a string. The input string is given as an array of characters char[].

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

You may assume all the characters consist of printable ascii characters.

Example 1:

Input: ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]

思路:双指针进行字符交换

class Solution {
public:
	void reverseString(vector& s)
	{//双指针
		int left = 0;
		int right = s.size()-1;
		while(left

 657. Robot Return to Origin

There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves.

The move sequence is represented by a string, and the character moves[i] represents its ith move. Valid moves are R (right), L (left), U (up), and D (down). If the robot returns to the origin after it finishes all of its moves, return true. Otherwise, return false.

Note: The way that the robot is "facing" is irrelevant. "R" will always make the robot move to the right once, "L" will always make it move left, etc. Also, assume that the magnitude of the robot's movement is the same for each move.

Example 1:

Input: "UD"
Output: true 
Explanation: The robot moves up once, and then down once. All moves have the same magnitude, so it ended up at the origin where it started. Therefore, we return true.

思路:回到原点--> 一个U 对应一个D;一个L对应一个R;

也就是判断 两组(U与D,L与R)的个数;

class Solution {
public:
	bool judgeCircle(string moves)
	{
		int nCount1=0,nCount2=0;
		for(char c : moves)
		{
			if(c=='U')nCount1++;
			else if(c=='D')nCount1--;
			else if(c=='L')nCount2++;
			else if(c=='R')nCount2--;
		}
		//判断nCount1,nCount2是否同时为零
		if(nCount1==0 && nCount2==0)
			return true;
		else 
			return false;
		//return nCount1 && nCount2;
	}
};
//for(char c:moves)就是定义一个遍历字符c,让它分别等于字符串数组moves里面的各个字符,然后执行下面的语句,当c被赋值为moves里面所有字符各一次后,就会退出这个循环.

709. To Lower Case

Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.
Example 1:

Input: "Hello"
Output: "hello"

class Solution 
{
public:
	string toLowerCase(string str) 
	{
		if(str.size()<=0)
			return str;
		for(int i=0;i='A' && c<='Z')
			{
				c +=32;
				str[i] = c;
			}

		}
		return str;
	}
};
//大神的代码
class Solution {
public:
    string toLowerCase(string str) {
        for (char &c : str) 
        {//遍历字符 c 等于 str 每个字符
            if (c >= 'A' && c <= 'Z') c += 32;
        }
        return str;
    }
};

804. Unique Morse Code Words

International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a" maps to ".-", "b" maps to "-...", "c" maps to "-.-.", and so on.

For convenience, the full table for the 26 letters of the English alphabet is given below:

[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, "cba" can be written as "-.-..--...", (which is the concatenation "-.-." + "-..." + ".-"). We'll call such a concatenation, the transformation of a word.

Return the number of different transformations among all words we have.

Example:
Input: words = ["gin", "zen", "gig", "msg"]
Output: 2
Explanation: 
The transformation of each word is:
"gin" -> "--...-."
"zen" -> "--...-."
"gig" -> "--...--."
"msg" -> "--...--."

你可能感兴趣的:(LeetCode笔记)