Given a string s and an integer k, reverse the first k characters for every 2k characters counting from the start of the string.
If there are fewer than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and leave the other as original.
class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.nextLine(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < s.length(); i++) { if (Character.isDigit(s.charAt(i))) { sb.append("number"); }else sb.append(s.charAt(i)); } System.out.println(sb); } }
Given an input string s, reverse the order of the words.
A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space.
Return a string of the words in reverse order concatenated by a single space.
Note that s may contain leading or trailing spaces or multiple spaces between two words. The returned string should only have a single space separating the words. Do not include any extra spaces.
Example 1:
1 2
Input: s = "the sky is blue" Output: "blue is sky the"
Example 2:
1 2 3
Input: s = " hello world " Output: "world hello" Explanation: Your reversed string should not contain leading or trailing spaces.
public String reverseWords(String s) { StringBuilder temp = removeSpace(s); reverse(temp, 0 , temp.length()-1); //reverse each single word int start = 0; int end = 0; for( int i = 0 ; i < temp.length(); i ++){ if(temp.charAt(i)!= ' ' ){ end = i; } if (temp.charAt(i) == ' ' || i == temp.length() - 1) { reverse(temp, start, end); start = i + 1; } } return temp.toString(); } }
The count-and-say sequence is a sequence of digit strings defined by the recursive formula:
countAndSay(1) = "1"
countAndSay(n) is the way you would “say” the digit string from countAndSay(n-1), which is then converted into a different digit string.
To determine how you “say” a digit string, split it into the minimal number of substrings such that each substring contains exactly one unique digit. Then for each substring, say the number of digits, then say the digit. Finally, concatenate every said digit.
For example, the saying and conversion for digit string "3322251":
Given a positive integer n, return thenthterm of the count-and-say sequence.
Example 1:
1 2 3
Input: n = 1 Output: "1" Explanation: This is the base case.
Example 2:
1 2 3 4 5 6 7 8
Input: n = 4 Output: "1211" Explanation: countAndSay(1) = "1" countAndSay(2) = say "1" = one 1 = "11" countAndSay(3) = say "11" = two 1's = "21" countAndSay(4) = say "21" = one 2 + one 1 = "12" + "11" = "1211"
Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++’s atoi function).
The algorithm for myAtoi(string s) is as follows:
Read in and ignore any leading whitespace.
Check if the next character (if not already at the end of the string) is '-' or '+'. Read this character in if it is either. This determines if the final result is negative or positive respectively. Assume the result is positive if neither is present.
Read in next the characters until the next non-digit character or the end of the input is reached. The rest of the string is ignored.
Convert these digits into an integer (i.e. "123" -> 123, "0032" -> 32). If no digits were read, then the integer is 0. Change the sign as necessary (from step 2).
If the integer is out of the 32-bit signed integer range [-231, 231 - 1], then clamp the integer so that it remains in the range. Specifically, integers less than -231 should be clamped to -231, and integers greater than 231 - 1 should be clamped to 231 - 1.
Return the integer as the final result.
Note:
Only the space character ' ' is considered a whitespace character.
Do not ignore any characters other than the leading whitespace or the rest of the string after the digits.
Example 1:
1 2 3 4 5 6 7 8 9 10 11
Input: s = "42" Output: 42 Explanation: The underlined characters are what is read in, the caret is the current reader position. Step 1: "42" (no characters read because there is no leading whitespace) ^ Step 2: "42" (no characters read because there is neither a '-' nor '+') ^ Step 3: "42" ("42" is read in) ^ The parsed integer is 42. Since 42 is in the range [-231, 231 - 1], the final result is 42.
Example 2:
1 2 3 4 5 6 7 8 9 10 11
Input: s = " -42" Output: -42 Explanation: Step 1: " -42" (leading whitespace is read and ignored) ^ Step 2: " -42" ('-' is read, so the result should be negative) ^ Step 3: " -42" ("42" is read in) ^ The parsed integer is -42. Since -42 is in the range [-231, 231 - 1], the final result is -42.
Example 3:
1 2 3 4 5 6 7 8 9 10 11
Input: s = "4193 with words" Output: 4193 Explanation: Step 1: "4193 with words" (no characters read because there is no leading whitespace) ^ Step 2: "4193 with words" (no characters read because there is neither a '-' nor '+') ^ Step 3: "4193 with words" ("4193" is read in; reading stops because the next character is a non-digit) ^ The parsed integer is 4193. Since 4193 is in the range [-231, 231 - 1], the final result is 4193.
//wrong: // class Solution { // public int myAtoi(String s) { // //s = s.replaceAll(" ", ""); // if (s.isEmpty()) return 0; // System.out.println(s); // int end = 0; // int start = 0; // int mark = 0; // // start is the starting index of the number // //end is the first non digit char after the number // // mark is to keep record of if we found the number yet // for(int i = 0 ; i < s.length(); i++){ // if(Character.isDigit(s.charAt(i)) && mark == 0){ // start = i; // mark++; // } // if(!Character.isDigit(s.charAt(i)) && mark != 0 ){ // end = i; // mark = 0; // break; // } // } // if(mark != 0) end = s.length(); // System.out.println("mark " + mark); // System.out.println("start " + start); // System.out.println("end " + end); // int pos = 0; //pos = 0 or 1 -> positive, pos = 2 -> negative // if(start > 1 ) return 0; // if(s.charAt(0) == '.') return 0; // for(int i = 0 ; i < start; i++){ // if(s.charAt(i) == '+' && pos == 0){ // pos = 1; // } // if(s.charAt(i) == '-' && pos == 0){ // pos = 2; // } // } // long res = 0; // for(int i = start; i < end; i++){ // res = s.charAt(i) - '0' + res*10; // } // if(pos == 2) res = -res;
// // Check for overflow again // if (res > Integer.MAX_VALUE) { // return Integer.MAX_VALUE; // } else if (res < Integer.MIN_VALUE) { // return Integer.MIN_VALUE; // } else { // return (int) res; // } // } // }
logic is not clear, too complicated, ignore some special cases
class Solution { public int myAtoi(String s) { s = s.trim(); // remove leading and trailing spaces if (s.isEmpty()) { return 0; }
int ans = 0, i = 0; boolean neg = s.charAt(0) == '-'; boolean pos = s.charAt(0) == '+'; // i is to point the starting index of the number // if no - or +, it will still move to the next index if (neg || pos) { i++; }
while (i < s.length() && Character.isDigit(s.charAt(i))) { int digit = s.charAt(i) - '0';